Sub topic 9
How Bundlers Split Code at import()
This chapter assumes you already know how import() returns a promise for a module namespace object, and how computed specifiers resolve. You should also have used a bundler such as webpack, Vite, Rollup, or esbuild at least once.
The Bundler's View of import()
A bundler reads your source files and builds the module graph before any code runs. A static import declaration is part of that graph, so the bundler puts the imported module into the same output file as the importer. An import() expression marks a split point instead. The bundler puts the target module, and any dependencies that only it uses, into a separate file called a chunk. At runtime, the call fetches that chunk over the network.
// main.js
import { setupNav } from './nav.js'; // bundled into the main chunk
setupNav();
document.querySelector('#show-chart').addEventListener('click', async () => {
const { renderChart } = await import('./chart.js'); // becomes its own chunk
renderChart(document.querySelector('#chart'));
});After a build, you get something like main-a1b2.js and chart-c3d4.js. The bundler rewrites import('./chart.js') to load the hashed chunk file. It still resolves to the same namespace object.
Shared Dependencies
Suppose two lazy chunks both import the same utility. Bundlers do not copy it into each chunk. They move it into a shared chunk that both load. If the main chunk already includes the module statically, the lazy chunks reuse that copy. As a result, one import() call can trigger more than one network request.
What the Bundler Can and Cannot Analyze
The bundler can only split code it can find at build time. The specifier you write decides what it can find.
| Specifier form | Example | Build-time result |
|---|---|---|
| String literal | `import('./chart.js')` | One chunk for that module |
| Template with a fixed prefix | ``import(`./locales/${lang}.js`)`` | One chunk per matching file in `./locales/` |
| Fully dynamic variable | `import(path)` | Not bundled; left as a runtime request, often with a warning |
Template literals work because the bundler expands the fixed parts into a pattern. Webpack includes every file that matches the pattern. Vite uses Rollup's dynamic-import-vars rules: the path must start with ./ or ../, must end with a file extension, and a variable can only stand in for a single path segment. A specifier like import(path), where path is fully dynamic, gives the bundler nothing to match. The file then has to exist at that URL at runtime, outside the bundle.
Naming and Hinting Chunks
Webpack reads "magic comments" inside the call:
const { renderChart } = await import(
/* webpackChunkName: "chart" */
/* webpackPrefetch: true */
'./chart.js'
);webpackChunkName controls the output file name. webpackPrefetch adds a <link rel="prefetch"> so the browser downloads the chunk while it is idle, before the user clicks. Vite and Rollup take chunk names from the file name, and you configure grouping through build.rollupOptions.output.manualChunks. Vite also preloads a lazy chunk's shared dependencies automatically when the import starts.
Common Mistakes
- Statically importing the same module elsewhere. If any static import in the main graph reaches
chart.js, the module lands in the main chunk and the split does nothing. Most bundlers warn about this. - Splitting too small. Each chunk costs a request. Split at route or feature boundaries, not around every helper function.
- Wrong deploy paths. Chunks load relative to the configured public path (
output.publicPathin webpack,basein Vite). If that path is wrong, the import rejects at runtime with a network error.
Exercise
A project uses import('./pages/' + pageName) and the production build contains no page chunks. The pages fail to load at runtime. What is the likely cause, and how do you fix it?
Show answer
The bundler could not build a match pattern from the expression, so it did not bundle the pages, and they are not served at that path. Rewrite it as a template literal with a fixed prefix and extension, such as import(./pages/${pageName}.js), so the bundler can emit one chunk per page file.
Exercise
Both admin.js and reports.js are loaded with import(), and both import table.js. Neither the entry nor its static imports use table.js. How many copies of table.js will a typical bundler emit?
Show answer
One. The bundler moves table.js into a shared chunk, and both lazy chunks load it when needed.