2016-12-03 18:36:10 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
"use strict";
|
2016-12-03 18:36:10 +08:00
|
|
|
|
2018-07-30 23:08:51 +08:00
|
|
|
const CommentCompilationWarning = require("../CommentCompilationWarning");
|
|
|
|
const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
|
|
|
|
const ContextDependencyHelpers = require("./ContextDependencyHelpers");
|
2017-10-16 18:52:04 +08:00
|
|
|
const ImportContextDependency = require("./ImportContextDependency");
|
2017-01-12 01:58:05 +08:00
|
|
|
const ImportDependenciesBlock = require("./ImportDependenciesBlock");
|
2018-10-18 05:09:21 +08:00
|
|
|
const ImportDependency = require("./ImportDependency");
|
2017-05-05 00:37:25 +08:00
|
|
|
const ImportEagerDependency = require("./ImportEagerDependency");
|
2018-07-30 23:08:51 +08:00
|
|
|
const ImportWeakDependency = require("./ImportWeakDependency");
|
2016-12-03 18:36:10 +08:00
|
|
|
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
|
|
|
|
/** @typedef {import("../ContextModule").ContextMode} ContextMode */
|
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
class ImportParserPlugin {
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options;
|
|
|
|
}
|
2016-12-03 18:36:10 +08:00
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
apply(parser) {
|
2018-01-16 04:26:47 +08:00
|
|
|
parser.hooks.importCall.tap("ImportParserPlugin", expr => {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (expr.arguments.length !== 1) {
|
2018-02-25 09:00:20 +08:00
|
|
|
throw new Error(
|
|
|
|
"Incorrect number of arguments provided to 'import(module: string) -> Promise'."
|
|
|
|
);
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2017-03-26 17:07:52 +08:00
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
const param = parser.evaluateExpression(expr.arguments[0]);
|
2017-04-10 17:35:32 +08:00
|
|
|
|
2017-03-26 17:07:52 +08:00
|
|
|
let chunkName = null;
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @type {ContextMode} */
|
2017-05-05 00:37:25 +08:00
|
|
|
let mode = "lazy";
|
2017-10-14 05:31:15 +08:00
|
|
|
let include = null;
|
2017-10-13 05:35:23 +08:00
|
|
|
let exclude = null;
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @type {RawChunkGroupOptions} */
|
2018-04-16 16:27:22 +08:00
|
|
|
const groupOptions = {};
|
2017-03-26 17:07:52 +08:00
|
|
|
|
2018-05-29 19:17:36 +08:00
|
|
|
const {
|
|
|
|
options: importOptions,
|
|
|
|
errors: commentErrors
|
|
|
|
} = parser.parseCommentOptions(expr.range);
|
|
|
|
|
|
|
|
if (commentErrors) {
|
|
|
|
for (const e of commentErrors) {
|
|
|
|
const { comment } = e;
|
|
|
|
parser.state.module.warnings.push(
|
2018-05-30 09:24:36 +08:00
|
|
|
new CommentCompilationWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
|
2018-05-29 19:17:36 +08:00
|
|
|
comment.loc
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-05-29 15:29:23 +08:00
|
|
|
}
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (importOptions) {
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackIgnore !== undefined) {
|
2018-04-12 23:33:22 +08:00
|
|
|
if (typeof importOptions.webpackIgnore !== "boolean") {
|
|
|
|
parser.state.module.warnings.push(
|
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-04-12 23:33:22 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
2018-09-22 05:24:18 +08:00
|
|
|
// Do not instrument `import()` if `webpackIgnore` is `true`
|
2018-04-12 23:33:22 +08:00
|
|
|
if (importOptions.webpackIgnore) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackChunkName !== undefined) {
|
2018-04-12 23:33:22 +08:00
|
|
|
if (typeof importOptions.webpackChunkName !== "string") {
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.state.module.warnings.push(
|
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2018-04-12 23:33:22 +08:00
|
|
|
} else {
|
|
|
|
chunkName = importOptions.webpackChunkName;
|
|
|
|
}
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackMode !== undefined) {
|
2018-04-12 23:33:22 +08:00
|
|
|
if (typeof importOptions.webpackMode !== "string") {
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.state.module.warnings.push(
|
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackMode\` expected a string, but received: ${importOptions.webpackMode}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2018-04-12 23:33:22 +08:00
|
|
|
} else {
|
|
|
|
mode = importOptions.webpackMode;
|
|
|
|
}
|
2017-03-26 17:07:52 +08:00
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackPrefetch !== undefined) {
|
2018-04-16 23:43:13 +08:00
|
|
|
if (importOptions.webpackPrefetch === true) {
|
2018-04-17 00:00:34 +08:00
|
|
|
groupOptions.prefetchOrder = 0;
|
2018-04-16 23:43:13 +08:00
|
|
|
} else if (typeof importOptions.webpackPrefetch === "number") {
|
2018-04-17 00:00:34 +08:00
|
|
|
groupOptions.prefetchOrder = importOptions.webpackPrefetch;
|
2018-04-16 23:43:13 +08:00
|
|
|
} else {
|
2018-04-16 16:27:22 +08:00
|
|
|
parser.state.module.warnings.push(
|
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackPrefetch\` expected true or a number, but received: ${importOptions.webpackPrefetch}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-04-16 16:27:22 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackPreload !== undefined) {
|
2018-04-16 23:43:13 +08:00
|
|
|
if (importOptions.webpackPreload === true) {
|
2018-04-17 00:00:34 +08:00
|
|
|
groupOptions.preloadOrder = 0;
|
2018-04-16 23:43:13 +08:00
|
|
|
} else if (typeof importOptions.webpackPreload === "number") {
|
2018-04-17 00:00:34 +08:00
|
|
|
groupOptions.preloadOrder = importOptions.webpackPreload;
|
2018-04-16 23:43:13 +08:00
|
|
|
} else {
|
2018-04-16 16:27:22 +08:00
|
|
|
parser.state.module.warnings.push(
|
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackPreload\` expected true or a number, but received: ${importOptions.webpackPreload}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-04-16 16:27:22 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-03-26 17:07:52 +08:00
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackInclude !== undefined) {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
!importOptions.webpackInclude ||
|
|
|
|
importOptions.webpackInclude.constructor.name !== "RegExp"
|
|
|
|
) {
|
|
|
|
parser.state.module.warnings.push(
|
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackInclude\` expected a regular expression, but received: ${importOptions.webpackInclude}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2017-10-12 05:30:54 +08:00
|
|
|
} else {
|
2017-10-14 05:31:15 +08:00
|
|
|
include = new RegExp(importOptions.webpackInclude);
|
2017-10-12 05:30:54 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackExclude !== undefined) {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
!importOptions.webpackExclude ||
|
|
|
|
importOptions.webpackExclude.constructor.name !== "RegExp"
|
|
|
|
) {
|
|
|
|
parser.state.module.warnings.push(
|
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackExclude\` expected a regular expression, but received: ${importOptions.webpackExclude}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2017-10-13 05:35:23 +08:00
|
|
|
} else {
|
|
|
|
exclude = new RegExp(importOptions.webpackExclude);
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 17:07:52 +08:00
|
|
|
}
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (param.isString()) {
|
|
|
|
if (mode !== "lazy" && mode !== "eager" && mode !== "weak") {
|
|
|
|
parser.state.module.warnings.push(
|
|
|
|
new UnsupportedFeatureWarning(
|
2018-06-04 16:10:23 +08:00
|
|
|
`\`webpackMode\` expected 'lazy', 'eager' or 'weak', but received: ${mode}.`,
|
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (mode === "eager") {
|
2018-08-07 20:20:53 +08:00
|
|
|
const dep = new ImportEagerDependency(param.string, expr.range);
|
2017-05-05 00:37:25 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (mode === "weak") {
|
2018-08-07 20:20:53 +08:00
|
|
|
const dep = new ImportWeakDependency(param.string, expr.range);
|
2017-07-26 20:49:37 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
2017-05-05 00:37:25 +08:00
|
|
|
} else {
|
2018-02-25 09:00:20 +08:00
|
|
|
const depBlock = new ImportDependenciesBlock(
|
2019-06-19 19:16:05 +08:00
|
|
|
{
|
|
|
|
...groupOptions,
|
2018-04-16 16:27:22 +08:00
|
|
|
name: chunkName
|
2019-06-19 19:16:05 +08:00
|
|
|
},
|
2018-02-25 09:00:20 +08:00
|
|
|
expr.loc,
|
2018-10-18 04:58:30 +08:00
|
|
|
param.string,
|
|
|
|
expr.range
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2018-10-18 05:09:21 +08:00
|
|
|
const dep = new ImportDependency(param.string);
|
|
|
|
dep.loc = expr.loc;
|
|
|
|
depBlock.addDependency(dep);
|
2017-05-05 00:37:25 +08:00
|
|
|
parser.state.current.addBlock(depBlock);
|
|
|
|
}
|
2017-01-12 01:58:05 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
mode !== "lazy" &&
|
|
|
|
mode !== "lazy-once" &&
|
|
|
|
mode !== "eager" &&
|
|
|
|
mode !== "weak"
|
|
|
|
) {
|
|
|
|
parser.state.module.warnings.push(
|
|
|
|
new UnsupportedFeatureWarning(
|
2018-06-04 16:10:23 +08:00
|
|
|
`\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
|
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2017-10-16 20:49:51 +08:00
|
|
|
mode = "lazy";
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (mode === "weak") {
|
2017-10-16 20:49:51 +08:00
|
|
|
mode = "async-weak";
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
const dep = ContextDependencyHelpers.create(
|
|
|
|
ImportContextDependency,
|
|
|
|
expr.range,
|
|
|
|
param,
|
|
|
|
expr,
|
|
|
|
this.options,
|
|
|
|
{
|
|
|
|
chunkName,
|
2018-04-16 16:27:22 +08:00
|
|
|
groupOptions,
|
2018-02-25 09:00:20 +08:00
|
|
|
include,
|
|
|
|
exclude,
|
|
|
|
mode,
|
|
|
|
namespaceObject: parser.state.module.buildMeta.strictHarmonyModule
|
|
|
|
? "strict"
|
|
|
|
: true
|
2018-10-30 19:39:43 +08:00
|
|
|
},
|
|
|
|
parser
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
|
|
|
if (!dep) return;
|
2017-01-12 01:58:05 +08:00
|
|
|
dep.loc = expr.loc;
|
|
|
|
dep.optional = !!parser.scope.inTry;
|
|
|
|
parser.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-16 04:26:47 +08:00
|
|
|
});
|
2017-01-12 01:58:05 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-29 15:29:23 +08:00
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
module.exports = ImportParserPlugin;
|