webpack/lib/dependencies/SystemPlugin.js

133 lines
3.2 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,
2018-07-25 02:05:29 +08:00
getModulePath,
2018-07-03 16:24:29 +08:00
toConstantDependency
} = require("../JavascriptParserHelpers");
const WebpackError = require("../WebpackError");
2018-07-25 02:05:29 +08:00
const ProvidedDependency = require("./ProvidedDependency");
class SystemPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap(
"SystemPlugin",
(compilation, { normalModuleFactory }) => {
2018-07-25 02:05:29 +08:00
compilation.dependencyFactories.set(
ProvidedDependency,
normalModuleFactory
);
compilation.dependencyTemplates.set(
ProvidedDependency,
new ProvidedDependency.Template()
);
2018-02-25 09:00:20 +08:00
const handler = (parser, parserOptions) => {
if (
2018-06-26 16:59:23 +08:00
typeof parserOptions.system === "undefined" ||
2018-02-25 09:00:20 +08:00
!parserOptions.system
2018-06-26 16:59:23 +08:00
) {
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 => {
2018-07-25 03:57:48 +08:00
const dep = new ProvidedDependency(
getModulePath(
parser.state.module.context,
require.resolve("../../buildin/system")
),
"System",
null,
expr.range
2018-02-25 09:00:20 +08:00
);
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(parser.state.module, expr.loc)
);
2018-02-25 09:00:20 +08:00
return parser.hooks.importCall.call(expr);
});
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(module, 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.module = module;
this.loc = loc;
Error.captureStackTrace(this, this.constructor);
}
}
module.exports = SystemPlugin;