@rushstack/eslint-plugin
Version:
An ESLint plugin providing supplementary rules for use with the @rushstack/eslint-config package
44 lines • 2.14 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
export const MESSAGE_ID_CHUNK_NAME = 'error-import-requires-chunk-name';
export const MESSAGE_ID_SINGLE_CHUNK_NAME = 'error-import-requires-single-chunk-name';
const importRequiresChunkNameRule = {
defaultOptions: [],
meta: {
type: 'problem',
messages: {
[MESSAGE_ID_CHUNK_NAME]: 'Usage of "import(...)" for code splitting requires a /* webpackChunkName: \'...\' */ comment',
[MESSAGE_ID_SINGLE_CHUNK_NAME]: 'Usage of "import(...)" for code splitting cannot specify multiple /* webpackChunkName: \'...\' */ comments'
},
schema: [],
docs: {
description: 'Requires that calls to "import(...)" for code splitting include a Webpack chunk name',
url: 'https://www.npmjs.com/package/@rushstack/eslint-plugin'
}
},
create: (context) => {
const sourceCode = context.sourceCode;
const webpackChunkNameRegex = /^webpackChunkName\s*:\s*('[^']+'|"[^"]+")$/;
return {
ImportExpression: (node) => {
const nodeComments = sourceCode.getCommentsInside(node);
const webpackChunkNameEntries = [];
for (const comment of nodeComments) {
const webpackChunkNameMatches = comment.value
.split(',')
.map((c) => c.trim())
.filter((c) => !!c.match(webpackChunkNameRegex));
webpackChunkNameEntries.push(...webpackChunkNameMatches);
}
if (webpackChunkNameEntries.length === 0) {
context.report({ node, messageId: MESSAGE_ID_CHUNK_NAME });
}
else if (webpackChunkNameEntries.length !== 1) {
context.report({ node, messageId: MESSAGE_ID_SINGLE_CHUNK_NAME });
}
}
};
}
};
export { importRequiresChunkNameRule };
//# sourceMappingURL=import-requires-chunk-name.js.map