Skip to content

Commit 227d9a2

Browse files
straubljharb
authored andcommitted
[Fix] dynamic-import-chunkname: allow single quotes to match Webpack support
Fixes #1130.
1 parent 569d726 commit 227d9a2

File tree

4 files changed

+67
-31
lines changed

4 files changed

+67
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1010
- [`default`]/TypeScript: avoid crash on `export =` with a MemberExpression ([#1841], thanks [@ljharb])
1111
- [`extensions`]/importType: Fix @/abc being treated as scoped module ([#1854], thanks [@3nuc])
1212
- allow using rest operator in named export ([#1878], thanks [@foray1010])
13+
- [`dynamic-import-chunkname`]: allow single quotes to match Webpack support ([#1848], thanks [@straub])
1314

1415
### Changed
1516
- [`export`]: add tests for a name collision with `export * from` ([#1704], thanks @tomprats)
@@ -732,6 +733,7 @@ for info on changes for earlier releases.
732733

733734
[#1878]: https://github.com/benmosher/eslint-plugin-import/pull/1878
734735
[#1854]: https://github.com/benmosher/eslint-plugin-import/issues/1854
736+
[#1848]: https://github.com/benmosher/eslint-plugin-import/pull/1848
735737
[#1841]: https://github.com/benmosher/eslint-plugin-import/issues/1841
736738
[#1836]: https://github.com/benmosher/eslint-plugin-import/pull/1836
737739
[#1835]: https://github.com/benmosher/eslint-plugin-import/pull/1835
@@ -1271,3 +1273,4 @@ for info on changes for earlier releases.
12711273
[@3nuc]: https://github.com/3nuc
12721274
[@foray1010]: https://github.com/foray1010
12731275
[@tomprats]: https://github.com/tomprats
1276+
[@straub]: https://github.com/straub

docs/rules/dynamic-import-chunkname.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ import(
3939
'someModule',
4040
);
4141

42-
// using single quotes instead of double quotes
43-
import(
44-
/* webpackChunkName: 'someModule' */
45-
'someModule',
46-
);
47-
4842
// invalid syntax for webpack comment
4943
import(
5044
/* totally not webpackChunkName: "someModule" */
@@ -78,6 +72,12 @@ The following patterns are valid:
7872
/* webpackChunkName: "someModule", webpackPrefetch: true */
7973
'someModule',
8074
);
75+
76+
// using single quotes instead of double quotes
77+
import(
78+
/* webpackChunkName: 'someModule' */
79+
'someModule',
80+
);
8181
```
8282

8383
## When Not To Use It

src/rules/dynamic-import-chunkname.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ module.exports = {
3030
const { webpackChunknameFormat = '[0-9a-zA-Z-_/.]+' } = config || {}
3131

3232
const paddedCommentRegex = /^ (\S[\s\S]+\S) $/
33-
const commentStyleRegex = /^( \w+: ("[^"]*"|\d+|false|true),?)+ $/
34-
const chunkSubstrFormat = ` webpackChunkName: "${webpackChunknameFormat}",? `
33+
const commentStyleRegex = /^( \w+: (["'][^"']*["']|\d+|false|true),?)+ $/
34+
const chunkSubstrFormat = ` webpackChunkName: ["']${webpackChunknameFormat}["'],? `
3535
const chunkSubstrRegex = new RegExp(chunkSubstrFormat)
3636

3737
function run(node, arg) {

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

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const noLeadingCommentError = 'dynamic imports require a leading comment with th
2121
const nonBlockCommentError = 'dynamic imports require a /* foo */ style comment, not a // foo comment'
2222
const noPaddingCommentError = 'dynamic imports require a block comment padded with spaces - /* foo */'
2323
const invalidSyntaxCommentError = 'dynamic imports require a "webpack" comment with valid syntax'
24-
const commentFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: "${commentFormat}",? */`
25-
const pickyCommentFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: "${pickyCommentFormat}",? */`
24+
const commentFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${commentFormat}["'],? */`
25+
const pickyCommentFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${pickyCommentFormat}["'],? */`
2626

2727
ruleTester.run('dynamic-import-chunkname', rule, {
2828
valid: [
@@ -132,6 +132,14 @@ ruleTester.run('dynamic-import-chunkname', rule, {
132132
options,
133133
parser,
134134
},
135+
{
136+
code: `import(
137+
/* webpackChunkName: 'someModule' */
138+
'someModule'
139+
)`,
140+
options,
141+
parser,
142+
},
135143
{
136144
code: `import(
137145
/* webpackChunkName: "someModule" */
@@ -192,17 +200,33 @@ ruleTester.run('dynamic-import-chunkname', rule, {
192200
},
193201
{
194202
code: `import(
195-
/* webpackChunkName: 'someModule' */
203+
/* webpackChunkName: "someModule' */
196204
'someModule'
197205
)`,
198206
options,
199207
parser,
200208
output: `import(
201-
/* webpackChunkName: 'someModule' */
209+
/* webpackChunkName: "someModule' */
202210
'someModule'
203211
)`,
204212
errors: [{
205-
message: commentFormatError,
213+
message: invalidSyntaxCommentError,
214+
type: 'CallExpression',
215+
}],
216+
},
217+
{
218+
code: `import(
219+
/* webpackChunkName: 'someModule" */
220+
'someModule'
221+
)`,
222+
options,
223+
parser,
224+
output: `import(
225+
/* webpackChunkName: 'someModule" */
226+
'someModule'
227+
)`,
228+
errors: [{
229+
message: invalidSyntaxCommentError,
206230
type: 'CallExpression',
207231
}],
208232
},
@@ -421,21 +445,6 @@ ruleTester.run('dynamic-import-chunkname', rule, {
421445
type: 'CallExpression',
422446
}],
423447
},
424-
{
425-
code: `dynamicImport(
426-
/* webpackChunkName: 'someModule' */
427-
'someModule'
428-
)`,
429-
options,
430-
output: `dynamicImport(
431-
/* webpackChunkName: 'someModule' */
432-
'someModule'
433-
)`,
434-
errors: [{
435-
message: commentFormatError,
436-
type: 'CallExpression',
437-
}],
438-
},
439448
{
440449
code: `dynamicImport(
441450
/* webpackChunkName "someModule" */
@@ -578,6 +587,14 @@ context('TypeScript', () => {
578587
type: nodeType,
579588
}],
580589
},
590+
{
591+
code: `import(
592+
/* webpackChunkName: 'someModule' */
593+
'test'
594+
)`,
595+
options,
596+
parser: typescriptParser,
597+
},
581598
],
582599
invalid: [
583600
{
@@ -624,17 +641,33 @@ context('TypeScript', () => {
624641
},
625642
{
626643
code: `import(
627-
/* webpackChunkName: 'someModule' */
644+
/* webpackChunkName "someModule' */
628645
'someModule'
629646
)`,
630647
options,
631648
parser: typescriptParser,
632649
output: `import(
633-
/* webpackChunkName: 'someModule' */
650+
/* webpackChunkName "someModule' */
634651
'someModule'
635652
)`,
636653
errors: [{
637-
message: commentFormatError,
654+
message: invalidSyntaxCommentError,
655+
type: nodeType,
656+
}],
657+
},
658+
{
659+
code: `import(
660+
/* webpackChunkName 'someModule" */
661+
'someModule'
662+
)`,
663+
options,
664+
parser: typescriptParser,
665+
output: `import(
666+
/* webpackChunkName 'someModule" */
667+
'someModule'
668+
)`,
669+
errors: [{
670+
message: invalidSyntaxCommentError,
638671
type: nodeType,
639672
}],
640673
},

0 commit comments

Comments
 (0)