126 lines
11 KiB
JavaScript
126 lines
11 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var _vm = require('vm');
|
||
|
|
||
|
var _vm2 = _interopRequireDefault(_vm);
|
||
|
|
||
|
var _docsUrl = require('../docsUrl');
|
||
|
|
||
|
var _docsUrl2 = _interopRequireDefault(_docsUrl);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'suggestion',
|
||
|
docs: {
|
||
|
url: (0, _docsUrl2.default)('dynamic-import-chunkname')
|
||
|
},
|
||
|
schema: [{
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
importFunctions: {
|
||
|
type: 'array',
|
||
|
uniqueItems: true,
|
||
|
items: {
|
||
|
type: 'string'
|
||
|
}
|
||
|
},
|
||
|
webpackChunknameFormat: {
|
||
|
type: 'string'
|
||
|
}
|
||
|
}
|
||
|
}]
|
||
|
},
|
||
|
|
||
|
create: function (context) {
|
||
|
const config = context.options[0];
|
||
|
|
||
|
var _ref = config || {},
|
||
|
_ref$importFunctions = _ref.importFunctions;
|
||
|
|
||
|
const importFunctions = _ref$importFunctions === undefined ? [] : _ref$importFunctions;
|
||
|
|
||
|
var _ref2 = config || {},
|
||
|
_ref2$webpackChunknam = _ref2.webpackChunknameFormat;
|
||
|
|
||
|
const webpackChunknameFormat = _ref2$webpackChunknam === undefined ? '[0-9a-zA-Z-_/.]+' : _ref2$webpackChunknam;
|
||
|
|
||
|
|
||
|
const paddedCommentRegex = /^ (\S[\s\S]+\S) $/;
|
||
|
const commentStyleRegex = /^( \w+: ("[^"]*"|\d+|false|true),?)+ $/;
|
||
|
const chunkSubstrFormat = ` webpackChunkName: "${webpackChunknameFormat}",? `;
|
||
|
const chunkSubstrRegex = new RegExp(chunkSubstrFormat);
|
||
|
|
||
|
return {
|
||
|
CallExpression(node) {
|
||
|
if (node.callee.type !== 'Import' && importFunctions.indexOf(node.callee.name) < 0) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const sourceCode = context.getSourceCode();
|
||
|
const arg = node.arguments[0];
|
||
|
const leadingComments = sourceCode.getComments(arg).leading;
|
||
|
|
||
|
if (!leadingComments || leadingComments.length === 0) {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: 'dynamic imports require a leading comment with the webpack chunkname'
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
let isChunknamePresent = false;
|
||
|
|
||
|
for (const comment of leadingComments) {
|
||
|
if (comment.type !== 'Block') {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: 'dynamic imports require a /* foo */ style comment, not a // foo comment'
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!paddedCommentRegex.test(comment.value)) {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: `dynamic imports require a block comment padded with spaces - /* foo */`
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// just like webpack itself does
|
||
|
_vm2.default.runInNewContext(`(function(){return {${comment.value}}})()`);
|
||
|
} catch (error) {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: `dynamic imports require a "webpack" comment with valid syntax`
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!commentStyleRegex.test(comment.value)) {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: `dynamic imports require a leading comment in the form /*${chunkSubstrFormat}*/`
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (chunkSubstrRegex.test(comment.value)) {
|
||
|
isChunknamePresent = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!isChunknamePresent) {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: `dynamic imports require a leading comment in the form /*${chunkSubstrFormat}*/`
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3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