Skip to content

Commit

Permalink
Add allowEagerMode option to allow webpackMode: 'eager' comments
Browse files Browse the repository at this point in the history
  • Loading branch information
amsardesai committed Apr 24, 2024
1 parent f77ceb6 commit db3c637
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/rules/dynamic-import-chunkname.md
Expand Up @@ -120,6 +120,30 @@ import(
);
```

### `allowEagerMode: true`

If you want chunk names to be optional when `webpackMode: "eager"` is set, you can set `allowEagerMode: true` in the rule config.

Given `{ "allowEagerMode": true }`:

<!-- markdownlint-disable-next-line MD024 -- duplicate header -->
### valid

The following patterns are valid:

```javascript
import('someModule');

import(
/* webpackMode: "eager" */
'someModule',
);
import(
/* webpackMode: "eager", webpackChunkName: "someModule" */
'someModule',
);
```

## When Not To Use It

If you don't care that webpack will autogenerate chunk names and may blow up browser caches and bundle size reports.
17 changes: 15 additions & 2 deletions src/rules/dynamic-import-chunkname.js
Expand Up @@ -25,19 +25,23 @@ module.exports = {
webpackChunknameFormat: {
type: 'string',
},
allowEagerMode: {
type: 'boolean',
},
},
}],
},

create(context) {
const config = context.options[0];
const { importFunctions = [], allowEmpty = false } = config || {};
const { importFunctions = [], allowEmpty = false, allowEagerMode = false } = config || {};
const { webpackChunknameFormat = '([0-9a-zA-Z-_/.]|\\[(request|index)\\])+' } = config || {};

const paddedCommentRegex = /^ (\S[\s\S]+\S) $/;
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 +58,7 @@ module.exports = {
}

let isChunknamePresent = false;
let isEagerModePresent = false;

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

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

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

if (!isChunknamePresent && !allowEmpty) {
if (
!isChunknamePresent
&& !allowEmpty
&& (allowEagerMode ? !isEagerModePresent : true)
) {
context.report({
node,
message:
Expand Down
27 changes: 27 additions & 0 deletions tests/src/rules/dynamic-import-chunkname.js
Expand Up @@ -19,6 +19,9 @@ const allowEmptyOptions = [{
const multipleImportFunctionOptions = [{
importFunctions: ['dynamicImport', 'definitelyNotStaticImport'],
}];
const allowEagerModeOptions = [{
allowEagerMode: true,
}];
const parser = parsers.BABEL_OLD;

const noLeadingCommentError = 'dynamic imports require a leading comment with the webpack chunkname';
Expand Down Expand Up @@ -419,6 +422,14 @@ ruleTester.run('dynamic-import-chunkname', rule, {
options,
parser,
},
{
code: `import(
/* webpackMode: 'eager' */
'someModule'
)`,
options: allowEagerModeOptions,
parser,
},
...SYNTAX_CASES,
],

Expand Down Expand Up @@ -867,6 +878,22 @@ ruleTester.run('dynamic-import-chunkname', rule, {
type: 'CallExpression',
}],
},
{
code: `import(
/* webpackMode: "eager" */
'someModule'
)`,
options,
parser,
output: `import(
/* webpackMode: "eager" */
'someModule'
)`,
errors: [{
message: chunkNameFormatError,
type: 'CallExpression',
}],
},
{
code: `dynamicImport(
/* webpackChunkName "someModule" */
Expand Down

0 comments on commit db3c637

Please sign in to comment.