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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9keW5hbWljLWltcG9ydC1jaHVua25hbWUuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsInNjaGVtYSIsInByb3BlcnRpZXMiLCJpbXBvcnRGdW5jdGlvbnMiLCJ1bmlxdWVJdGVtcyIsIml0ZW1zIiwid2VicGFja0NodW5rbmFtZUZvcm1hdCIsImNyZW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udHMpIHtcbiAgICAgICAgICBpZiAoY29tbWVudC50eXBlICE9PSAnQmxvY2snKSB7XG4gICAgICAgICAgICBjb250ZXh0LnJlcG9ydCh7XG4gICAgICAgICAgICAgIG5vZGUsXG4gICAgICAgICAgICAgIG1lc3NhZ2U6ICdkeW5hbWljIGltcG9ydHMgcmVxdWlyZSBhIC8qIGZvbyAqLyBzdHlsZSBjb21tZW50LCBub3QgYSAvLyBmb28gY29tbWVudCcsXG4gICAgICAgICAgICB9KVxuICAgICAgICAgICAgcmV0dXJuXG4gICAgICAgICAgfVxuXG4gICAgICAgICAgaWYgKCFwYWRkZWRDb21tZW50UmVnZXgudGVzdChjb21tZW50LnZhbHVlKSkge1xuICAgICAgICAgICAgY29udGV4dC5yZXBvcnQoe1xuICAgICAgICAgICAgICBub2RlLFxuICAgICAgICAgICAgICBtZXNzYWdlOiBgZHluYW1pYyBpbXBvcnRzIHJlcXVpcmUgYSBibG9jayBjb21tZW50IHBhZGRlZCB3aXRoIHNwYWNlcyAtIC8qIGZvbyAqL2AsXG4gICAgICAgICAgICB9KVxuICAgICAgICAgICAgcmV0dXJuXG4gICAgICAgICAgfVxuXG4gICAgICAgICAgdHJ5IHtcbiAgICAgICAgICAgIC8vIGp1c3QgbGlrZSB3ZWJwYWNrIGl0c2VsZiBkb2VzXG4gICAgICAgICAgICB2bS5ydW5Jbk5ld0NvbnRleHQoYChmdW5jdGlvbigpe3JldHVybiB7JHtjb21tZW50LnZhbHVlfX19KSgpYClcbiAgICAgICAgICB9XG4gICAgICAgICAgY2F0Y2ggKGVycm9yKSB7XG4gICAgICAgICAgICBjb250ZXh0LnJlcG9ydCh7XG4gICAgICAgICAgICAgIG5vZGUsXG4gICAgICAgICAgICAgIG1lc3NhZ2U6IGBkeW5hbWljIGltcG9ydHMgcmVxdWlyZSBhIFwid2VicGFja1wiIGNvbW1lbnQgd2l0aCB2YWxpZCBzeW50YXhgLFxuICAgICAgICAgICAgfSlcbiAgICAgICAgICAgIHJldHVyblxuICAgICAgICAgIH1cblxuICAgICAgICAgIGlmICghY29tbWVudFN0eWxlUmVnZXgudGVzdChjb21tZW50LnZhbHVlKSkge1xuICAgICAgICAgICAgY29udGV4dC5yZXBvcnQoe1xuICAgICAgICAgICAgICBub2RlLFxuICAgICAgICAgICAgICBtZXNzYWdlOlxuICAgICAgICAgICAgICAgIGBkeW5hbWljIGltcG9ydHMgcmVxdWlyZSBhIGxlYWRpbmcgY29tbWVudCBpbiB0aGUgZm9ybSAvKiR7Y2h1bmtTdWJzdHJGb3JtYXR9Ki9gLFxuICAgICAgICAgICAgfSlcbiAgICAgICAgICAgIHJldHVyblxuICAgICAgICAgIH1cblxuICAgICAgICAgIGlmIChjaHVua1N1YnN0clJlZ2V4LnRlc3QoY29tbWVudC52YWx1ZSkpIHtcbiAgICAgICAgICAgIGlzQ2h1bmtuYW1lUHJlc2VudCA9IHRydWVcbiAgICAgICAgICB9XG4gICAgICAgIH1cblxuICAgICAgICBpZiAoIWlzQ2h1bmtuYW1lUHJlc2VudCkge1xuICAgICAgICAgIGNvbnRleHQucmVwb3J0KHtcbiAgICAgICAgICAgIG5vZGUsXG4gICAgICAgICAgICBtZXNzYWdlOlxuICAgICAgICAgICAgICBgZHluYW1pYyBpbXBvcnRzIHJlcXVpcmUgYSBsZWFkaW5nIGNvbW1lbnQgaW4gdGhlIGZvcm0gLyoke2NodW5rU3Vic3RyRm9ybWF0fSovYCxcbiAgICAgICAgICB9KVxuICAgICAgICB9XG4gICAgICB9LFxuICAgIH1cbiAgfSxcbn1cbiJdfQ==
|