import/dynamic-import-chunkname
Require a webpackChunkName comment in every dynamic import()
Also called import-x/dynamic-import-chunkname
webpack puts the target of each import() in its own file. Without a webpackChunkName comment, a production build names that file with a number, such as 472.js, which tells you nothing in a network tab or a bundle report.
The comment goes inside the parentheses, before the path. It must be a block comment with one space after /* and one before */. An import with webpackMode: "eager" doesn't make a chunk, so it doesn't need a name, and giving it one is reported too.
Configure#
Off by default. To turn it on:
[lint.rules]
"import/dynamic-import-chunkname" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
[lint.rules]
"import/dynamic-import-chunkname" = ["error", {
importFunctions = [], # list of strings
allowEmpty = false,
# webpackChunknameFormat = <string>,
}]Examples#
const settings = await import("./pages/settings.js")dynamic imports require a leading comment with the webpack chunkname;const settings = await import(dynamic imports require a /* foo */ style comment, not a // foo comment
// webpackChunkName: "settings"
"./pages/settings.js"
);const settings = await import(/*webpackChunkName: "settings"*/ "./pages/settings.js")dynamic imports require a block comment padded with spaces - /* foo */;const settings = await import(dynamic imports using eager mode do not need a webpackChunkName
/* webpackChunkName: "settings", webpackMode: "eager" */
"./pages/settings.js"
);const settings = await import(/* webpackChunkName: "settings" */ "./pages/settings.js");
const admin = await import(
/* webpackChunkName: "admin", webpackPrefetch: true */
"./pages/admin.js"
);
const icons = await import(/* webpackMode: "eager" */ "./icons.js");Options#
| Option | Default | What it does |
|---|---|---|
importFunctions | [] | Names of functions that are checked like import(). |
allowEmpty | false | Allow an import without a chunk name. A malformed comment is still reported. |
webpackChunknameFormat | "([0-9a-zA-Z-_/.]|\[(request|index)\])+" | A regular expression that the chunk name must match. |
{ "importFunctions": ["lazyLoad"] }1 problemconst settings = lazyLoad("./pages/settings.js")dynamic imports require a leading comment with the webpack chunkname;{ "allowEmpty": true }No problemsconst settings = await import("./pages/settings.js");{ "allowEmpty": true }1 problemconst settings = await import(/*webpackPrefetch: true*/ "./pages/settings.js")dynamic imports require a block comment padded with spaces - /* foo */;{ "webpackChunknameFormat": "page-[a-z]+" }1 problemconst settings = await import(/* webpackChunkName: "settings" */ "./pages/settings.js")dynamic imports require a leading comment in the form /*webpackChunkName: ["']page-[a-z]+["'],? */;{ "webpackChunknameFormat": "page-[a-z]+" }No problemsconst settings = await import(/* webpackChunkName: "page-settings" */ "./pages/settings.js");When not to use it#
The comment only matters to webpack and to bundlers that read webpack's comments. If you bundle with bun build, leave this rule off.