Skip to content

Commit

Permalink
show webpackChunkName format error only when webpackChunkName invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
MhMadHamster committed Apr 3, 2022
1 parent 926af0d commit 6969c3f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/rules/dynamic-import-chunkname.js
Expand Up @@ -30,7 +30,7 @@ module.exports = {
const { webpackChunknameFormat = '([0-9a-zA-Z-_/.]|\\[(request|index)\\])+' } = config || {};

const paddedCommentRegex = /^ (\S[\s\S]+\S) $/;
const commentStyleRegex = /^( ((webpackChunkName: ["']([0-9a-zA-Z-_/.]|\[(request|index)\])+["'])|((webpackPrefetch|webpackPreload): (true|false|-?[0-9]+))|(webpackIgnore: (true|false))|((webpackInclude|webpackExclude): \/.*\/)|(webpackMode: ["'](lazy|lazy-once|eager|weak)["'])|(webpackExports: (['"]\w+['"]|\[(['"]\w+['"], *)+(['"]\w+['"]*)\]))),?)+ $/;
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);

Expand Down Expand Up @@ -83,7 +83,7 @@ module.exports = {
context.report({
node,
message:
`dynamic imports require a leading comment in the form /*${chunkSubstrFormat}*/`,
`dynamic imports require a "webpack" comment with valid syntax`,
});
return;
}
Expand Down
107 changes: 102 additions & 5 deletions tests/src/rules/dynamic-import-chunkname.js
Expand Up @@ -21,8 +21,9 @@ const noLeadingCommentError = 'dynamic imports require a leading comment with th
const nonBlockCommentError = 'dynamic imports require a /* foo */ style comment, not a // foo comment';
const noPaddingCommentError = 'dynamic imports require a block comment padded with spaces - /* foo */';
const invalidSyntaxCommentError = 'dynamic imports require a "webpack" comment with valid syntax';
const commentFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${commentFormat}["'],? */`;
const pickyCommentFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${pickyCommentFormat}["'],? */`;
const commentFormatError = `dynamic imports require a "webpack" comment with valid syntax`;
const chunkNameFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${commentFormat}["'],? */`;
const pickyChunkNameFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${pickyCommentFormat}["'],? */`;

ruleTester.run('dynamic-import-chunkname', rule, {
valid: [
Expand Down Expand Up @@ -511,6 +512,54 @@ ruleTester.run('dynamic-import-chunkname', rule, {
type: 'CallExpression',
}],
},
{
code: `import(
/* webpackChunkName: true */
'someModule'
)`,
options,
parser,
output: `import(
/* webpackChunkName: true */
'someModule'
)`,
errors: [{
message: chunkNameFormatError,
type: 'CallExpression',
}],
},
{
code: `import(
/* webpackChunkName: "my-module-[id]" */
'someModule'
)`,
options,
parser,
output: `import(
/* webpackChunkName: "my-module-[id]" */
'someModule'
)`,
errors: [{
message: chunkNameFormatError,
type: 'CallExpression',
}],
},
{
code: `import(
/* webpackChunkName: ["request"] */
'someModule'
)`,
options,
parser,
output: `import(
/* webpackChunkName: ["request"] */
'someModule'
)`,
errors: [{
message: chunkNameFormatError,
type: 'CallExpression',
}],
},
{
code: `import(
/*webpackChunkName: "someModule"*/
Expand Down Expand Up @@ -621,7 +670,7 @@ ruleTester.run('dynamic-import-chunkname', rule, {
'someModule'
)`,
errors: [{
message: pickyCommentFormatError,
message: pickyChunkNameFormatError,
type: 'CallExpression',
}],
},
Expand Down Expand Up @@ -911,7 +960,7 @@ ruleTester.run('dynamic-import-chunkname', rule, {
'someModule'
)`,
errors: [{
message: pickyCommentFormatError,
message: pickyChunkNameFormatError,
type: 'CallExpression',
}],
},
Expand Down Expand Up @@ -1433,6 +1482,54 @@ context('TypeScript', () => {
type: nodeType,
}],
},
{
code: `import(
/* webpackChunkName: true */
'someModule'
)`,
options,
parser: typescriptParser,
output: `import(
/* webpackChunkName: true */
'someModule'
)`,
errors: [{
message: chunkNameFormatError,
type: nodeType,
}],
},
{
code: `import(
/* webpackChunkName: "my-module-[id]" */
'someModule'
)`,
options,
parser: typescriptParser,
output: `import(
/* webpackChunkName: "my-module-[id]" */
'someModule'
)`,
errors: [{
message: chunkNameFormatError,
type: nodeType,
}],
},
{
code: `import(
/* webpackChunkName: ["request"] */
'someModule'
)`,
options,
parser: typescriptParser,
output: `import(
/* webpackChunkName: ["request"] */
'someModule'
)`,
errors: [{
message: chunkNameFormatError,
type: nodeType,
}],
},
{
code: `import(
/* webpackChunkName: "someModule123" */
Expand All @@ -1445,7 +1542,7 @@ context('TypeScript', () => {
'someModule'
)`,
errors: [{
message: pickyCommentFormatError,
message: pickyChunkNameFormatError,
type: nodeType,
}],
},
Expand Down

0 comments on commit 6969c3f

Please sign in to comment.