webpack/lib/dependencies/SystemPlugin.js

169 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
const {
JAVASCRIPT_MODULE_TYPE_AUTO,
JAVASCRIPT_MODULE_TYPE_DYNAMIC
} = require("../ModuleTypeConstants");
const RuntimeGlobals = require("../RuntimeGlobals");
const WebpackError = require("../WebpackError");
2018-07-03 16:24:29 +08:00
const {
evaluateToString,
expressionIsUnsupported,
toConstantDependency
} = require("../javascript/JavascriptParserHelpers");
const makeSerializable = require("../util/makeSerializable");
const ConstDependency = require("./ConstDependency");
const SystemRuntimeModule = require("./SystemRuntimeModule");
2023-05-22 08:03:05 +08:00
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
2019-06-12 19:29:17 +08:00
/** @typedef {import("../Compiler")} Compiler */
2023-06-17 02:24:34 +08:00
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
2023-05-22 08:03:05 +08:00
/** @typedef {import("../javascript/JavascriptParser")} Parser */
2023-06-17 02:24:34 +08:00
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
2019-06-12 19:29:17 +08:00
const PLUGIN_NAME = "SystemPlugin";
class SystemPlugin {
2019-06-12 19:29:17 +08:00
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap(
PLUGIN_NAME,
2018-02-25 09:00:20 +08:00
(compilation, { normalModuleFactory }) => {
compilation.hooks.runtimeRequirementInModule
.for(RuntimeGlobals.system)
.tap(PLUGIN_NAME, (module, set) => {
set.add(RuntimeGlobals.requireScope);
});
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.system)
2025-07-08 22:46:17 +08:00
.tap(PLUGIN_NAME, (chunk, _set) => {
compilation.addRuntimeModule(chunk, new SystemRuntimeModule());
});
2023-05-22 08:03:05 +08:00
/**
* @param {Parser} parser parser parser
* @param {JavascriptParserOptions} parserOptions parserOptions
* @returns {void}
*/
2018-02-25 09:00:20 +08:00
const handler = (parser, parserOptions) => {
2018-08-21 17:02:16 +08:00
if (parserOptions.system === undefined || !parserOptions.system) {
2018-02-25 09:00:20 +08:00
return;
2018-06-26 16:59:23 +08:00
}
2018-02-25 09:00:20 +08:00
2023-06-17 02:24:34 +08:00
/**
* @param {string} name name
*/
const setNotSupported = (name) => {
2018-02-25 09:00:20 +08:00
parser.hooks.evaluateTypeof
.for(name)
.tap(PLUGIN_NAME, evaluateToString("undefined"));
2018-02-25 09:00:20 +08:00
parser.hooks.expression
.for(name)
.tap(
PLUGIN_NAME,
2018-07-03 16:24:29 +08:00
expressionIsUnsupported(
2018-02-25 09:00:20 +08:00
parser,
2024-07-31 10:39:30 +08:00
`${name} is not supported by webpack.`
2018-02-25 09:00:20 +08:00
)
);
};
parser.hooks.typeof
.for("System.import")
.tap(
PLUGIN_NAME,
2018-07-03 16:24:29 +08:00
toConstantDependency(parser, JSON.stringify("function"))
2018-02-25 09:00:20 +08:00
);
parser.hooks.evaluateTypeof
.for("System.import")
.tap(PLUGIN_NAME, evaluateToString("function"));
2018-02-25 09:00:20 +08:00
parser.hooks.typeof
.for("System")
.tap(
PLUGIN_NAME,
2018-07-03 16:24:29 +08:00
toConstantDependency(parser, JSON.stringify("object"))
2018-02-25 09:00:20 +08:00
);
parser.hooks.evaluateTypeof
.for("System")
.tap(PLUGIN_NAME, evaluateToString("object"));
2018-02-25 09:00:20 +08:00
setNotSupported("System.set");
setNotSupported("System.get");
setNotSupported("System.register");
parser.hooks.expression.for("System").tap(PLUGIN_NAME, (expr) => {
2023-06-17 02:24:34 +08:00
const dep = new ConstDependency(
RuntimeGlobals.system,
/** @type {Range} */ (expr.range),
[RuntimeGlobals.system]
);
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
2018-07-25 02:05:29 +08:00
return true;
2018-02-25 09:00:20 +08:00
});
parser.hooks.call.for("System.import").tap(PLUGIN_NAME, (expr) => {
parser.state.module.addWarning(
2023-06-17 02:24:34 +08:00
new SystemImportDeprecationWarning(
/** @type {DependencyLocation} */ (expr.loc)
)
2018-06-26 16:59:23 +08:00
);
2018-02-25 09:00:20 +08:00
2019-08-14 20:42:07 +08:00
return parser.hooks.importCall.call({
type: "ImportExpression",
2023-05-22 08:03:05 +08:00
source:
/** @type {import("estree").Literal} */
(expr.arguments[0]),
2019-08-14 20:42:07 +08:00
loc: expr.loc,
2024-11-01 00:31:45 +08:00
range: expr.range,
options: null
2019-08-14 20:42:07 +08:00
});
2018-02-25 09:00:20 +08:00
});
2017-11-08 18:32:05 +08:00
};
2018-02-25 09:00:20 +08:00
normalModuleFactory.hooks.parser
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
.tap(PLUGIN_NAME, handler);
2018-02-25 09:00:20 +08:00
normalModuleFactory.hooks.parser
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
.tap(PLUGIN_NAME, handler);
2018-02-25 09:00:20 +08:00
}
);
}
}
class SystemImportDeprecationWarning extends WebpackError {
2023-06-17 02:24:34 +08:00
/**
* @param {DependencyLocation} loc location
*/
constructor(loc) {
super(
"System.import() is deprecated and will be removed soon. Use import() instead.\n" +
"For more info visit https://webpack.js.org/guides/code-splitting/"
);
this.name = "SystemImportDeprecationWarning";
this.loc = loc;
}
}
makeSerializable(
SystemImportDeprecationWarning,
"webpack/lib/dependencies/SystemPlugin",
"SystemImportDeprecationWarning"
);
module.exports = SystemPlugin;
module.exports.SystemImportDeprecationWarning = SystemImportDeprecationWarning;