webpack/lib/dependencies/SystemPlugin.js

139 lines
3.5 KiB
JavaScript
Raw 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";
2018-07-03 16:24:29 +08:00
const {
evaluateToString,
expressionIsUnsupported,
toConstantDependency
} = require("../JavascriptParserHelpers");
const RuntimeGlobals = require("../RuntimeGlobals");
const WebpackError = require("../WebpackError");
const ConstDependency = require("./ConstDependency");
const SystemRuntimeModule = require("./SystemRuntimeModule");
2019-06-12 19:29:17 +08:00
/** @typedef {import("../Compiler")} Compiler */
class SystemPlugin {
constructor(options) {
this.options = options;
}
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(
"SystemPlugin",
(compilation, { normalModuleFactory }) => {
compilation.hooks.runtimeRequirementInModule
.for(RuntimeGlobals.system)
.tap("SystemPlugin", (module, set) => {
set.add(RuntimeGlobals.require);
});
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.system)
.tap("SystemPlugin", (chunk, set) => {
compilation.addRuntimeModule(chunk, new SystemRuntimeModule());
});
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
const setNotSupported = name => {
parser.hooks.evaluateTypeof
.for(name)
2018-07-03 16:24:29 +08:00
.tap("SystemPlugin", evaluateToString("undefined"));
2018-02-25 09:00:20 +08:00
parser.hooks.expression
.for(name)
.tap(
"SystemPlugin",
2018-07-03 16:24:29 +08:00
expressionIsUnsupported(
2018-02-25 09:00:20 +08:00
parser,
name + " is not supported by webpack."
)
);
};
parser.hooks.typeof
.for("System.import")
.tap(
"SystemPlugin",
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")
2018-07-03 16:24:29 +08:00
.tap("SystemPlugin", evaluateToString("function"));
2018-02-25 09:00:20 +08:00
parser.hooks.typeof
.for("System")
.tap(
"SystemPlugin",
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")
2018-07-03 16:24:29 +08:00
.tap("SystemPlugin", evaluateToString("object"));
2018-02-25 09:00:20 +08:00
setNotSupported("System.set");
setNotSupported("System.get");
setNotSupported("System.register");
2018-07-25 02:05:29 +08:00
parser.hooks.expression.for("System").tap("SystemPlugin", expr => {
const dep = new ConstDependency(RuntimeGlobals.system, expr.range, [
RuntimeGlobals.system
]);
2018-07-25 03:57:48 +08:00
dep.loc = expr.loc;
2018-07-27 21:57:03 +08:00
parser.state.module.addDependency(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("SystemPlugin", expr => {
2018-06-26 16:59:23 +08:00
parser.state.module.warnings.push(
new SystemImportDeprecationWarning(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",
source: expr.arguments[0],
loc: expr.loc,
range: expr.range
});
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/auto")
.tap("SystemPlugin", handler);
normalModuleFactory.hooks.parser
.for("javascript/dynamic")
.tap("SystemPlugin", handler);
}
);
}
}
class SystemImportDeprecationWarning extends WebpackError {
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;
Error.captureStackTrace(this, this.constructor);
}
}
module.exports = SystemPlugin;