Skip to content

Commit

Permalink
Allow empty chunk name when webpackMode: 'eager' is set
Browse files Browse the repository at this point in the history
  • Loading branch information
amsardesai committed Apr 24, 2024
1 parent f77ceb6 commit 2b626fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/rules/dynamic-import-chunkname.md
Expand Up @@ -86,6 +86,12 @@ The following patterns are valid:
/* webpackChunkName: 'someModule' */
'someModule',
);

// chunk name is ignored when using eager mode
import(
/* webpackMode: "eager" */,
'someModule',
);
```

### `allowEmpty: true`
Expand Down
8 changes: 7 additions & 1 deletion src/rules/dynamic-import-chunkname.js
Expand Up @@ -38,6 +38,7 @@ module.exports = {
const commentStyleRegex = /^( ((webpackChunkName: .+)|((webpackPrefetch|webpackPreload): (true|false|-?[0-9]+))|(webpackIgnore: (true|false))|((webpackInclude|webpackExclude): \/.*\/)|(webpackMode: ["'](lazy|lazy-once|eager|weak)["'])|(webpackExports: (['"]\w+['"]|\[(['"]\w+['"], *)+(['"]\w+['"]*)\]))),?)+ $/;
const chunkSubstrFormat = ` webpackChunkName: ["']${webpackChunknameFormat}["'],? `;
const chunkSubstrRegex = new RegExp(chunkSubstrFormat);
const eagerModeRegex = /webpackMode: ["']eager["']/;

function run(node, arg) {
const sourceCode = context.getSourceCode();
Expand All @@ -54,6 +55,7 @@ module.exports = {
}

let isChunknamePresent = false;
let isEagerModePresent = false;

for (const comment of leadingComments) {
if (comment.type !== 'Block') {
Expand Down Expand Up @@ -92,12 +94,16 @@ module.exports = {
return;
}

if (eagerModeRegex.test(comment.value)) {
isEagerModePresent = true;
}

if (chunkSubstrRegex.test(comment.value)) {
isChunknamePresent = true;
}
}

if (!isChunknamePresent && !allowEmpty) {
if (!isChunknamePresent && !allowEmpty && !isEagerModePresent) {
context.report({
node,
message:
Expand Down
8 changes: 8 additions & 0 deletions tests/src/rules/dynamic-import-chunkname.js
Expand Up @@ -419,6 +419,14 @@ ruleTester.run('dynamic-import-chunkname', rule, {
options,
parser,
},
{
code: `import(
/* webpackMode: 'eager' */
'someModule'
)`,
options,
parser,
},
...SYNTAX_CASES,
],

Expand Down

0 comments on commit 2b626fb

Please sign in to comment.