When it is time to deploy your app for production, simply run the vite build command. By default, it uses <root>/index.html as the build entry point, and produces an application bundle that is suitable to be served over a static hosting service. Check out the Deploying a Static Site for guides about popular services.
AEM & Vite integration
Not only the input is important here, also note:
- outDir: directory where Vite will write its output to. Will be used by vite-aem-clientlib-generator npm package (see later in this post).
- manifest: when set to true, the build will also generate a manifest.json file that contains a mapping of non-hashed asset filenames to their hashed versions, which can then be used by a server framework to render the correct asset links.
- fileNames: allows you to specify a directory structure and naming convention that will be used for the files written to the outDir. In the case of how I have setup the AEM & Vite integration it’s important that those paths mimic the exact location of chunks living in resources folders of proxy enabled clientlibs. For example: http://localhost:4502/etc.clientlibs/aem-vite-demo/clientlibs/clientlib-esmodule/resources/js/app.405d9e8c.js
The manifest has a Record<name, chunk> structure that contains the following information:
- file: proxy path pointing to the chunk file living in a /resources clientlib folder
- isEntry: true if the chunk needs to be loaded directly
- isDynamicEntry: true if the chunk is dynamically loaded within another chunk
- imports: list of chunk names that also need to be loaded directly
- dynamicImports: list of chunk names that are dynamically loaded within another chunk
- css: lists of proxy paths pointing to css files living in a /resources clientlib folder
I’ve created a CLI that can be used to inspect the manifest and create a modified clientlib. The clientlib will contain specific properties that can be used to generated the correct tags for the HTML.
The CLI can be called using vite-aem-lib generate, the first thing it will do is look for the configuration file: vite.lib.config.js, note that at this time only the CommonJS format is supported. A configuration file for a Vite enabled multi clientlib setup can look something like this:
This is what CLI does in a nutshell:
- Fetches and validates the vite.lib.config.js config file
- Loop over all the libraries
- Get the manifest living in the outDir
- Find the entry
- Build a list of resources: direct JS/CSS imports & dynamic JS imports
- Create a clientlib at the location provided in the config
- Push all chunks to the resources folder
- Create a .content.xml file containing information necessary to build the HTML tags.
Here is an example of how the .content.xml file could look like:
The .content.xml file has 3 custom properties that will be used during tag generation in the Java implementation:
- scripts: “<script type=\”module\” crossorigin src=\””+ file +”\”></script>”
- preloads: “<link rel=\”modulepreload\” href=\””+ file +”\”>”
- stylesheets: “<link rel=\”stylesheet\” href=\””+ file +”\”>”
Note that none of the dynamic imported chunks are listed in any of these properties, they will be loaded dynamically and are not used during tag generation.
You might also find the modulepreload rel of the style tag weird, at least it was for me at the beginning. We generate <link rel=”modulepreload”> directives for entry chunks and their direct imports. I hope the quotes below give you some insight:
Module-based development offers some real advantages in terms of cacheability, helping you reduce the number of bytes you need to ship to your users. The finer granularity of the code also helps with the loading story, by letting you prioritize the critical code in your application.
However, module dependencies introduce a loading problem, in that the browser needs to wait for a module to load before it finds out what its dependencies are. One way around this is by preloading the dependencies, so that the browser knows about all the files ahead of time and can keep the connection busy.
<link rel=”preload”> tells the browser to download and cache a resource (like a script or a stylesheet) as soon as possible. It’s helpful when you need that resource a few seconds after loading the page, and you want to speed it up.
The browser doesn’t do anything with the resource after downloading it. Scripts aren’t executed, stylesheets aren’t applied. It’s just cached — so that when something else needs it, it’s available immediately.
So is <link rel=”modulepreload”> just <link rel=”preload”> for modules?
In a nutshell, yes. By having a specific link type for preloading modules, we can write simple HTML without worrying about what credentials mode we’re using. The defaults just work.
I’ve created a demo project based on the AEM archetype, you can look into the repository to see the full code and try it out yourself. Here is a small demonstration on how a multi clientlib setup using Vite looks like:
What is missing?
- Legacy support, take a look at the Vite documentation for more info.
- modulepreload polyfill as not all browsers support the link relation yet.
I hoped you liked this 2 part integration series as much as I did.
Written by Jeroen Druwé