Skip to content

Commit e9489d8

Browse files
JiangWeixianljharb
authored andcommitted
[New] dynamic-import-chunkname: add allowEmpty option to allow empty leading comments
1 parent 6f0668c commit e9489d8

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

Diff for: docs/rules/dynamic-import-chunkname.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ You can also configure the regex format you'd like to accept for the webpackChun
1515
{
1616
"dynamic-import-chunkname": [2, {
1717
importFunctions: ["dynamicImport"],
18-
webpackChunknameFormat: "[a-zA-Z0-57-9-/_]+"
18+
webpackChunknameFormat: "[a-zA-Z0-57-9-/_]+",
19+
allowEmpty: false
1920
}]
2021
}
2122
```
@@ -87,6 +88,38 @@ The following patterns are valid:
8788
);
8889
```
8990

91+
### `allowEmpty: true`
92+
93+
If you want to allow dynamic imports without a webpackChunkName, you can set `allowEmpty: true` in the rule config. This will allow dynamic imports without a leading comment, or with a leading comment that does not contain a webpackChunkName.
94+
95+
Given `{ "allowEmpty": true }`:
96+
97+
<!-- markdownlint-disable-next-line MD024 -- duplicate header -->
98+
### valid
99+
100+
The following patterns are valid:
101+
102+
```javascript
103+
import('someModule');
104+
105+
import(
106+
/* webpackChunkName: "someModule" */
107+
'someModule',
108+
);
109+
```
110+
<!-- markdownlint-disable-next-line MD024 -- duplicate header -->
111+
### invalid
112+
113+
The following patterns are invalid:
114+
115+
```javascript
116+
// incorrectly formatted comment
117+
import(
118+
/*webpackChunkName:"someModule"*/
119+
'someModule',
120+
);
121+
```
122+
90123
## When Not To Use It
91124

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

Diff for: src/rules/dynamic-import-chunkname.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ module.exports = {
1919
type: 'string',
2020
},
2121
},
22+
allowEmpty: {
23+
type: 'boolean',
24+
},
2225
webpackChunknameFormat: {
2326
type: 'string',
2427
},
@@ -28,7 +31,7 @@ module.exports = {
2831

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

3437
const paddedCommentRegex = /^ (\S[\s\S]+\S) $/;
@@ -42,7 +45,7 @@ module.exports = {
4245
? sourceCode.getCommentsBefore(arg) // This method is available in ESLint >= 4.
4346
: sourceCode.getComments(arg).leading; // This method is deprecated in ESLint 7.
4447

45-
if (!leadingComments || leadingComments.length === 0) {
48+
if ((!leadingComments || leadingComments.length === 0) && !allowEmpty) {
4649
context.report({
4750
node,
4851
message: 'dynamic imports require a leading comment with the webpack chunkname',
@@ -94,7 +97,7 @@ module.exports = {
9497
}
9598
}
9699

97-
if (!isChunknamePresent) {
100+
if (!isChunknamePresent && !allowEmpty) {
98101
context.report({
99102
node,
100103
message:

Diff for: tests/src/rules/dynamic-import-chunkname.js

+30
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const pickyCommentOptions = [{
1212
importFunctions: ['dynamicImport'],
1313
webpackChunknameFormat: pickyCommentFormat,
1414
}];
15+
const allowEmptyOptions = [{
16+
importFunctions: ['dynamicImport'],
17+
allowEmpty: true,
18+
}];
1519
const multipleImportFunctionOptions = [{
1620
importFunctions: ['dynamicImport', 'definitelyNotStaticImport'],
1721
}];
@@ -83,6 +87,19 @@ ruleTester.run('dynamic-import-chunkname', rule, {
8387
)`,
8488
options,
8589
},
90+
{
91+
code: `import('test')`,
92+
options: allowEmptyOptions,
93+
parser,
94+
},
95+
{
96+
code: `import(
97+
/* webpackMode: "lazy" */
98+
'test'
99+
)`,
100+
options: allowEmptyOptions,
101+
parser,
102+
},
86103
{
87104
code: `import(
88105
/* webpackChunkName: "someModule" */
@@ -975,6 +992,19 @@ context('TypeScript', () => {
975992

976993
ruleTester.run('dynamic-import-chunkname', rule, {
977994
valid: [
995+
{
996+
code: `import('test')`,
997+
options: allowEmptyOptions,
998+
parser: typescriptParser,
999+
},
1000+
{
1001+
code: `import(
1002+
/* webpackMode: "lazy" */
1003+
'test'
1004+
)`,
1005+
options: allowEmptyOptions,
1006+
parser: typescriptParser,
1007+
},
9781008
{
9791009
code: `import(
9801010
/* webpackChunkName: "someModule" */

0 commit comments

Comments
 (0)