Skip to content

Commit a3a7176

Browse files
amsardesailjharb
authored andcommitted
[New] dynamic-import-chunkname: Allow empty chunk name when webpackMode: 'eager' is set; add suggestions to remove name in eager mode'
1 parent c0ac54b commit a3a7176

File tree

5 files changed

+268
-18
lines changed

5 files changed

+268
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
88

99
### Added
1010
- [`dynamic-import-chunkname`]: add `allowEmpty` option to allow empty leading comments ([#2942], thanks [@JiangWeixian])
11+
- [`dynamic-import-chunkname`]: Allow empty chunk name when webpackMode: 'eager' is set; add suggestions to remove name in eager mode ([#3004], thanks [@amsardesai])
1112

1213
### Changed
1314
- [Docs] `no-extraneous-dependencies`: Make glob pattern description more explicit ([#2944], thanks [@mulztob])
@@ -1115,6 +1116,7 @@ for info on changes for earlier releases.
11151116

11161117
[`memo-parser`]: ./memo-parser/README.md
11171118

1119+
[#3004]: https://github.com/import-js/eslint-plugin-import/pull/3004
11181120
[#2991]: https://github.com/import-js/eslint-plugin-import/pull/2991
11191121
[#2989]: https://github.com/import-js/eslint-plugin-import/pull/2989
11201122
[#2987]: https://github.com/import-js/eslint-plugin-import/pull/2987
@@ -1701,6 +1703,7 @@ for info on changes for earlier releases.
17011703
[@aladdin-add]: https://github.com/aladdin-add
17021704
[@alex-page]: https://github.com/alex-page
17031705
[@alexgorbatchev]: https://github.com/alexgorbatchev
1706+
[@amsardesai]: https://github.com/amsardesai
17041707
[@andreubotella]: https://github.com/andreubotella
17051708
[@AndrewLeedham]: https://github.com/AndrewLeedham
17061709
[@andyogo]: https://github.com/andyogo

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
7373
| Name                            | Description | 💼 | ⚠️ | 🚫 | 🔧 | 💡 ||
7474
| :------------------------------------------------------------------------------- | :------------------------------------------------------------------------- | :- | :---- | :- | :- | :- | :- |
7575
| [consistent-type-specifier-style](docs/rules/consistent-type-specifier-style.md) | Enforce or ban the use of inline type-only markers for named imports. | | | | 🔧 | | |
76-
| [dynamic-import-chunkname](docs/rules/dynamic-import-chunkname.md) | Enforce a leading comment with the webpackChunkName for dynamic imports. | | | | | | |
76+
| [dynamic-import-chunkname](docs/rules/dynamic-import-chunkname.md) | Enforce a leading comment with the webpackChunkName for dynamic imports. | | | | | 💡 | |
7777
| [exports-last](docs/rules/exports-last.md) | Ensure all exports appear after other statements. | | | | | | |
7878
| [extensions](docs/rules/extensions.md) | Ensure consistent use of file extension within the import path. | | | | | | |
7979
| [first](docs/rules/first.md) | Ensure all imports appear before other statements. | | | | 🔧 | | |

docs/rules/dynamic-import-chunkname.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# import/dynamic-import-chunkname
22

3+
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
4+
35
<!-- end auto-generated rule header -->
46

57
This rule reports any dynamic imports without a webpackChunkName specified in a leading block comment in the proper format.
@@ -56,6 +58,13 @@ import(
5658
// webpackChunkName: "someModule"
5759
'someModule',
5860
);
61+
62+
// chunk names are disallowed when eager mode is set
63+
import(
64+
/* webpackMode: "eager" */
65+
/* webpackChunkName: "someModule" */
66+
'someModule',
67+
)
5968
```
6069

6170
### valid

src/rules/dynamic-import-chunkname.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = {
2727
},
2828
},
2929
}],
30+
hasSuggestions: true,
3031
},
3132

3233
create(context) {
@@ -36,8 +37,10 @@ module.exports = {
3637

3738
const paddedCommentRegex = /^ (\S[\s\S]+\S) $/;
3839
const commentStyleRegex = /^( ((webpackChunkName: .+)|((webpackPrefetch|webpackPreload): (true|false|-?[0-9]+))|(webpackIgnore: (true|false))|((webpackInclude|webpackExclude): \/.*\/)|(webpackMode: ["'](lazy|lazy-once|eager|weak)["'])|(webpackExports: (['"]\w+['"]|\[(['"]\w+['"], *)+(['"]\w+['"]*)\]))),?)+ $/;
39-
const chunkSubstrFormat = ` webpackChunkName: ["']${webpackChunknameFormat}["'],? `;
40+
const chunkSubstrFormat = `webpackChunkName: ["']${webpackChunknameFormat}["'],? `;
4041
const chunkSubstrRegex = new RegExp(chunkSubstrFormat);
42+
const eagerModeFormat = `webpackMode: ["']eager["'],? `;
43+
const eagerModeRegex = new RegExp(eagerModeFormat);
4144

4245
function run(node, arg) {
4346
const sourceCode = context.getSourceCode();
@@ -54,6 +57,7 @@ module.exports = {
5457
}
5558

5659
let isChunknamePresent = false;
60+
let isEagerModePresent = false;
5761

5862
for (const comment of leadingComments) {
5963
if (comment.type !== 'Block') {
@@ -92,12 +96,55 @@ module.exports = {
9296
return;
9397
}
9498

99+
if (eagerModeRegex.test(comment.value)) {
100+
isEagerModePresent = true;
101+
}
102+
95103
if (chunkSubstrRegex.test(comment.value)) {
96104
isChunknamePresent = true;
97105
}
98106
}
99107

100-
if (!isChunknamePresent && !allowEmpty) {
108+
if (isChunknamePresent && isEagerModePresent) {
109+
context.report({
110+
node,
111+
message: 'dynamic imports using eager mode do not need a webpackChunkName',
112+
suggest: [
113+
{
114+
desc: 'Remove webpackChunkName',
115+
fix(fixer) {
116+
for (const comment of leadingComments) {
117+
if (chunkSubstrRegex.test(comment.value)) {
118+
const replacement = comment.value.replace(chunkSubstrRegex, '').trim().replace(/,$/, '');
119+
if (replacement === '') {
120+
return fixer.remove(comment);
121+
} else {
122+
return fixer.replaceText(comment, `/* ${replacement} */`);
123+
}
124+
}
125+
}
126+
},
127+
},
128+
{
129+
desc: 'Remove webpackMode',
130+
fix(fixer) {
131+
for (const comment of leadingComments) {
132+
if (eagerModeRegex.test(comment.value)) {
133+
const replacement = comment.value.replace(eagerModeRegex, '').trim().replace(/,$/, '');
134+
if (replacement === '') {
135+
return fixer.remove(comment);
136+
} else {
137+
return fixer.replaceText(comment, `/* ${replacement} */`);
138+
}
139+
}
140+
}
141+
},
142+
},
143+
],
144+
});
145+
}
146+
147+
if (!isChunknamePresent && !allowEmpty && !isEagerModePresent) {
101148
context.report({
102149
node,
103150
message:

0 commit comments

Comments
 (0)