Skip to content

Commit db3c637

Browse files
committed
Add allowEagerMode option to allow webpackMode: 'eager' comments
1 parent f77ceb6 commit db3c637

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

docs/rules/dynamic-import-chunkname.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,30 @@ import(
120120
);
121121
```
122122

123+
### `allowEagerMode: true`
124+
125+
If you want chunk names to be optional when `webpackMode: "eager"` is set, you can set `allowEagerMode: true` in the rule config.
126+
127+
Given `{ "allowEagerMode": true }`:
128+
129+
<!-- markdownlint-disable-next-line MD024 -- duplicate header -->
130+
### valid
131+
132+
The following patterns are valid:
133+
134+
```javascript
135+
import('someModule');
136+
137+
import(
138+
/* webpackMode: "eager" */
139+
'someModule',
140+
);
141+
import(
142+
/* webpackMode: "eager", webpackChunkName: "someModule" */
143+
'someModule',
144+
);
145+
```
146+
123147
## When Not To Use It
124148

125149
If you don't care that webpack will autogenerate chunk names and may blow up browser caches and bundle size reports.

src/rules/dynamic-import-chunkname.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,23 @@ module.exports = {
2525
webpackChunknameFormat: {
2626
type: 'string',
2727
},
28+
allowEagerMode: {
29+
type: 'boolean',
30+
},
2831
},
2932
}],
3033
},
3134

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

3740
const paddedCommentRegex = /^ (\S[\s\S]+\S) $/;
3841
const commentStyleRegex = /^( ((webpackChunkName: .+)|((webpackPrefetch|webpackPreload): (true|false|-?[0-9]+))|(webpackIgnore: (true|false))|((webpackInclude|webpackExclude): \/.*\/)|(webpackMode: ["'](lazy|lazy-once|eager|weak)["'])|(webpackExports: (['"]\w+['"]|\[(['"]\w+['"], *)+(['"]\w+['"]*)\]))),?)+ $/;
3942
const chunkSubstrFormat = ` webpackChunkName: ["']${webpackChunknameFormat}["'],? `;
4043
const chunkSubstrRegex = new RegExp(chunkSubstrFormat);
44+
const eagerModeRegex = /webpackMode: ["']eager["']/;
4145

4246
function run(node, arg) {
4347
const sourceCode = context.getSourceCode();
@@ -54,6 +58,7 @@ module.exports = {
5458
}
5559

5660
let isChunknamePresent = false;
61+
let isEagerModePresent = false;
5762

5863
for (const comment of leadingComments) {
5964
if (comment.type !== 'Block') {
@@ -92,12 +97,20 @@ module.exports = {
9297
return;
9398
}
9499

100+
if (eagerModeRegex.test(comment.value)) {
101+
isEagerModePresent = true;
102+
}
103+
95104
if (chunkSubstrRegex.test(comment.value)) {
96105
isChunknamePresent = true;
97106
}
98107
}
99108

100-
if (!isChunknamePresent && !allowEmpty) {
109+
if (
110+
!isChunknamePresent
111+
&& !allowEmpty
112+
&& (allowEagerMode ? !isEagerModePresent : true)
113+
) {
101114
context.report({
102115
node,
103116
message:

tests/src/rules/dynamic-import-chunkname.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const allowEmptyOptions = [{
1919
const multipleImportFunctionOptions = [{
2020
importFunctions: ['dynamicImport', 'definitelyNotStaticImport'],
2121
}];
22+
const allowEagerModeOptions = [{
23+
allowEagerMode: true,
24+
}];
2225
const parser = parsers.BABEL_OLD;
2326

2427
const noLeadingCommentError = 'dynamic imports require a leading comment with the webpack chunkname';
@@ -419,6 +422,14 @@ ruleTester.run('dynamic-import-chunkname', rule, {
419422
options,
420423
parser,
421424
},
425+
{
426+
code: `import(
427+
/* webpackMode: 'eager' */
428+
'someModule'
429+
)`,
430+
options: allowEagerModeOptions,
431+
parser,
432+
},
422433
...SYNTAX_CASES,
423434
],
424435

@@ -867,6 +878,22 @@ ruleTester.run('dynamic-import-chunkname', rule, {
867878
type: 'CallExpression',
868879
}],
869880
},
881+
{
882+
code: `import(
883+
/* webpackMode: "eager" */
884+
'someModule'
885+
)`,
886+
options,
887+
parser,
888+
output: `import(
889+
/* webpackMode: "eager" */
890+
'someModule'
891+
)`,
892+
errors: [{
893+
message: chunkNameFormatError,
894+
type: 'CallExpression',
895+
}],
896+
},
870897
{
871898
code: `dynamicImport(
872899
/* webpackChunkName "someModule" */

0 commit comments

Comments
 (0)