2015-07-01 06:19:52 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-01-12 04:03:10 +08:00
|
|
|
"use strict";
|
2018-01-16 04:26:47 +08:00
|
|
|
|
2017-01-12 04:03:10 +08:00
|
|
|
const ParserHelpers = require("../ParserHelpers");
|
2018-01-16 04:26:47 +08:00
|
|
|
const WebpackError = require("../WebpackError");
|
2017-01-12 04:03:10 +08:00
|
|
|
|
|
|
|
class SystemPlugin {
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
apply(compiler) {
|
2017-12-06 22:01:25 +08:00
|
|
|
compiler.hooks.compilation.tap("SystemPlugin", (compilation, {
|
|
|
|
normalModuleFactory
|
|
|
|
}) => {
|
2017-12-14 17:22:27 +08:00
|
|
|
const handler = (parser, parserOptions) => {
|
2017-01-12 04:03:10 +08:00
|
|
|
if(typeof parserOptions.system !== "undefined" && !parserOptions.system)
|
|
|
|
return;
|
2015-07-01 06:19:52 +08:00
|
|
|
|
2018-01-16 04:26:47 +08:00
|
|
|
const shouldWarn = typeof parserOptions.system === "undefined";
|
|
|
|
|
2017-11-08 18:32:05 +08:00
|
|
|
const setNotSupported = name => {
|
2017-12-19 21:35:30 +08:00
|
|
|
parser.hooks.evaluateTypeof.for(name).tap("SystemPlugin", ParserHelpers.evaluateToString("undefined"));
|
|
|
|
parser.hooks.expression.for(name).tap("SystemPlugin",
|
2017-12-16 00:08:49 +08:00
|
|
|
ParserHelpers.expressionIsUnsupported(parser, name + " is not supported by webpack.")
|
2017-01-12 04:03:10 +08:00
|
|
|
);
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
2017-01-12 04:03:10 +08:00
|
|
|
|
2017-12-19 21:35:30 +08:00
|
|
|
parser.hooks.typeof.for("System.import").tap("SystemPlugin", ParserHelpers.toConstantDependency(parser, JSON.stringify("function")));
|
|
|
|
parser.hooks.evaluateTypeof.for("System.import").tap("SystemPlugin", ParserHelpers.evaluateToString("function"));
|
|
|
|
parser.hooks.typeof.for("System").tap("SystemPlugin", ParserHelpers.toConstantDependency(parser, JSON.stringify("object")));
|
|
|
|
parser.hooks.evaluateTypeof.for("System").tap("SystemPlugin", ParserHelpers.evaluateToString("object"));
|
2017-01-12 04:03:10 +08:00
|
|
|
|
|
|
|
setNotSupported("System.set");
|
|
|
|
setNotSupported("System.get");
|
|
|
|
setNotSupported("System.register");
|
2017-12-19 21:35:30 +08:00
|
|
|
|
|
|
|
parser.hooks.expression.for("System").tap("SystemPlugin", () => {
|
2017-02-24 21:23:24 +08:00
|
|
|
const systemPolyfillRequire = ParserHelpers.requireFileAsExpression(
|
2017-11-08 18:32:05 +08:00
|
|
|
parser.state.module.context, require.resolve("../../buildin/system.js"));
|
|
|
|
return ParserHelpers.addParsedVariableToModule(parser, "System", systemPolyfillRequire);
|
2017-02-23 18:40:57 +08:00
|
|
|
});
|
2018-01-16 04:26:47 +08:00
|
|
|
|
|
|
|
parser.hooks.call.for("System.import").tap("SystemPlugin", expr => {
|
|
|
|
if(shouldWarn) {
|
|
|
|
parser.state.module.warnings.push(new SystemImportDeprecationWarning(parser.state.module, expr.loc));
|
|
|
|
}
|
|
|
|
|
|
|
|
return parser.hooks.importCall.call(expr);
|
|
|
|
});
|
2017-12-14 17:22:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
normalModuleFactory.hooks.parser.for("javascript/auto").tap("SystemPlugin", handler);
|
|
|
|
normalModuleFactory.hooks.parser.for("javascript/dynamic").tap("SystemPlugin", handler);
|
2016-09-14 18:04:42 +08:00
|
|
|
});
|
2017-01-12 04:03:10 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-16 04:26:47 +08:00
|
|
|
|
|
|
|
class SystemImportDeprecationWarning extends WebpackError {
|
|
|
|
constructor(module, loc) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "SystemImportDeprecationWarning";
|
|
|
|
this.message = "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.origin = this.module = module;
|
|
|
|
this.originLoc = loc;
|
|
|
|
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 04:03:10 +08:00
|
|
|
module.exports = SystemPlugin;
|