Skip to content

Commit

Permalink
inline regex for magic comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MhMadHamster committed Dec 18, 2021
1 parent e127b32 commit cfcf2ff
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions src/rules/dynamic-import-chunkname.js
@@ -1,14 +1,6 @@
import vm from 'vm';
import docsUrl from '../docsUrl';

function generateRegexForMagicComments(magicComments) {
const preparedComments = magicComments.map(([comment, value]) => {
return `(${comment}: ${value})`;
});

return new RegExp(`^( (${preparedComments.join('|')}),?)+ $`);
}

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -36,18 +28,9 @@ module.exports = {
const config = context.options[0];
const { importFunctions = [] } = config || {};
const { webpackChunknameFormat = '([0-9a-zA-Z-_/.]|\\[(request|index)\\])+' } = config || {};
const webpackMagicComments = [
['webpackChunkName', `["']${webpackChunknameFormat}["']`],
['webpackPrefetch', '(true|false|-?[0-9]+)'],
['webpackPreload', '(true|false|-?[0-9]+)'],
['webpackIgnore', '(true|false)'],
['webpackInclude', '\\/.*\\/'],
['webpackExclude', '\\/.*\\/'],
['webpackMode', `["'](lazy|lazy-once|eager|weak)["']`],
['webpackExports', `(['"]\\w+['"]|\\[(['"]\\w+['"], *)+(['"]\\w+['"]*)\\])`],
];

const paddedCommentRegex = /^ (\S[\s\S]+\S) $/;
const commentStyleRegex = generateRegexForMagicComments(webpackMagicComments);
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 chunkSubstrFormat = ` webpackChunkName: ["']${webpackChunknameFormat}["'],? `;
const chunkSubstrRegex = new RegExp(chunkSubstrFormat);

Expand Down

0 comments on commit cfcf2ff

Please sign in to comment.