You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
13 KiB
119 lines
13 KiB
2 years ago
|
'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 () {function create(context) {
|
||
|
var config = context.options[0];var _ref =
|
||
|
config || {},_ref$importFunctions = _ref.importFunctions,importFunctions = _ref$importFunctions === undefined ? [] : _ref$importFunctions;var _ref2 =
|
||
|
config || {},_ref2$webpackChunknam = _ref2.webpackChunknameFormat,webpackChunknameFormat = _ref2$webpackChunknam === undefined ? '[0-9a-zA-Z-_/.]+' : _ref2$webpackChunknam;
|
||
|
|
||
|
var paddedCommentRegex = /^ (\S[\s\S]+\S) $/;
|
||
|
var commentStyleRegex = /^( \w+: (["'][^"']*["']|\d+|false|true),?)+ $/;
|
||
|
var chunkSubstrFormat = ' webpackChunkName: ["\']' + String(webpackChunknameFormat) + '["\'],? ';
|
||
|
var chunkSubstrRegex = new RegExp(chunkSubstrFormat);
|
||
|
|
||
|
function run(node, arg) {
|
||
|
var sourceCode = context.getSourceCode();
|
||
|
var leadingComments = sourceCode.getCommentsBefore ?
|
||
|
sourceCode.getCommentsBefore(arg) // This method is available in ESLint >= 4.
|
||
|
: sourceCode.getComments(arg).leading; // This method is deprecated in ESLint 7.
|
||
|
|
||
|
if (!leadingComments || leadingComments.length === 0) {
|
||
|
context.report({
|
||
|
node: node,
|
||
|
message: 'dynamic imports require a leading comment with the webpack chunkname' });
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var isChunknamePresent = false;var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined;try {
|
||
|
|
||
|
for (var _iterator = leadingComments[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {var comment = _step.value;
|
||
|
if (comment.type !== 'Block') {
|
||
|
context.report({
|
||
|
node: node,
|
||
|
message: 'dynamic imports require a /* foo */ style comment, not a // foo comment' });
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!paddedCommentRegex.test(comment.value)) {
|
||
|
context.report({
|
||
|
node: node,
|
||
|
message: 'dynamic imports require a block comment padded with spaces - /* foo */' });
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// just like webpack itself does
|
||
|
_vm2['default'].runInNewContext('(function() {return {' + String(comment.value) + '}})()');
|
||
|
}
|
||
|
catch (error) {
|
||
|
context.report({
|
||
|
node: node,
|
||
|
message: 'dynamic imports require a "webpack" comment with valid syntax' });
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!commentStyleRegex.test(comment.value)) {
|
||
|
context.report({
|
||
|
node: node,
|
||
|
message: 'dynamic imports require a leading comment in the form /*' +
|
||
|
chunkSubstrFormat + '*/' });
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (chunkSubstrRegex.test(comment.value)) {
|
||
|
isChunknamePresent = true;
|
||
|
}
|
||
|
}} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator['return']) {_iterator['return']();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
|
||
|
|
||
|
if (!isChunknamePresent) {
|
||
|
context.report({
|
||
|
node: node,
|
||
|
message: 'dynamic imports require a leading comment in the form /*' +
|
||
|
chunkSubstrFormat + '*/' });
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
ImportExpression: function () {function ImportExpression(node) {
|
||
|
run(node, node.source);
|
||
|
}return ImportExpression;}(),
|
||
|
|
||
|
CallExpression: function () {function CallExpression(node) {
|
||
|
if (node.callee.type !== 'Import' && importFunctions.indexOf(node.callee.name) < 0) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
run(node, node.arguments[0]);
|
||
|
}return CallExpression;}() };
|
||
|
|
||
|
}return create;}() };
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9keW5hbWljLWltcG9ydC1jaHVua25hbWUuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsInNjaGVtYSIsInByb3BlcnRpZXMiLCJpbXBvcnRGdW5jdGlvbnMiLCJ1bmlxdWVJdGVtcyIsIml0ZW1zIiwid2VicGFja0NodW5rbmFtZUZvcm1hdCIsImNyZWF0ZSIsImNvbnRleHQiLCJjb25maWciLCJvcHRpb25zIiwicGFkZGVkQ29tbWVudFJlZ2V4IiwiY29tbWVudFN0eWxlUmVnZXgiLCJjaHVua1N1YnN0ckZvcm1hdCIsImNodW5rU3Vic3RyUmVnZXgiLCJSZWdFeHAiLCJydW4iLCJub2RlIiwiYXJnIiwic291cmNlQ29kZSIsImdldFNvdXJjZUNvZGUiLCJsZWFkaW5nQ29tbWVudHMiLCJnZXRDb21tZW50c0JlZm9yZSIsImdldENvbW1lbnRzIiwibGVhZGluZyIsImxlbmd0aCIsInJlcG9ydCIsIm1lc3NhZ2UiLCJpc0NodW5rbmFtZVByZXNlbnQiLCJjb21tZW50IiwidGVzdCIsInZhbHVlIiwidm0iLCJydW5Jbk5ld0NvbnRleHQ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