| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const {
|
|---|
| 9 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
|---|
| 10 | JAVASCRIPT_MODULE_TYPE_DYNAMIC
|
|---|
| 11 | } = require("../ModuleTypeConstants");
|
|---|
| 12 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 13 | const WebpackError = require("../errors/WebpackError");
|
|---|
| 14 | const {
|
|---|
| 15 | evaluateToString,
|
|---|
| 16 | expressionIsUnsupported,
|
|---|
| 17 | toConstantDependency
|
|---|
| 18 | } = require("../javascript/JavascriptParserHelpers");
|
|---|
| 19 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 20 | const ConstDependency = require("./ConstDependency");
|
|---|
| 21 | const SystemRuntimeModule = require("./SystemRuntimeModule");
|
|---|
| 22 |
|
|---|
| 23 | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
|---|
| 24 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 25 | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
|---|
| 26 | /** @typedef {import("../javascript/JavascriptParser")} Parser */
|
|---|
| 27 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 28 |
|
|---|
| 29 | const PLUGIN_NAME = "SystemPlugin";
|
|---|
| 30 |
|
|---|
| 31 | class SystemPlugin {
|
|---|
| 32 | /**
|
|---|
| 33 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 34 | * @param {Compiler} compiler the compiler instance
|
|---|
| 35 | * @returns {void}
|
|---|
| 36 | */
|
|---|
| 37 | apply(compiler) {
|
|---|
| 38 | compiler.hooks.compilation.tap(
|
|---|
| 39 | PLUGIN_NAME,
|
|---|
| 40 | (compilation, { normalModuleFactory }) => {
|
|---|
| 41 | compilation.hooks.runtimeRequirementInModule
|
|---|
| 42 | .for(RuntimeGlobals.system)
|
|---|
| 43 | .tap(PLUGIN_NAME, (module, set) => {
|
|---|
| 44 | set.add(RuntimeGlobals.requireScope);
|
|---|
| 45 | });
|
|---|
| 46 |
|
|---|
| 47 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 48 | .for(RuntimeGlobals.system)
|
|---|
| 49 | .tap(PLUGIN_NAME, (chunk, _set) => {
|
|---|
| 50 | compilation.addRuntimeModule(chunk, new SystemRuntimeModule());
|
|---|
| 51 | });
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Handles the hook callback for this code path.
|
|---|
| 55 | * @param {Parser} parser parser parser
|
|---|
| 56 | * @param {JavascriptParserOptions} parserOptions parserOptions
|
|---|
| 57 | * @returns {void}
|
|---|
| 58 | */
|
|---|
| 59 | const handler = (parser, parserOptions) => {
|
|---|
| 60 | if (parserOptions.system === undefined || !parserOptions.system) {
|
|---|
| 61 | return;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Sets not supported.
|
|---|
| 66 | * @param {string} name name
|
|---|
| 67 | */
|
|---|
| 68 | const setNotSupported = (name) => {
|
|---|
| 69 | parser.hooks.evaluateTypeof
|
|---|
| 70 | .for(name)
|
|---|
| 71 | .tap(PLUGIN_NAME, evaluateToString("undefined"));
|
|---|
| 72 | parser.hooks.expression
|
|---|
| 73 | .for(name)
|
|---|
| 74 | .tap(
|
|---|
| 75 | PLUGIN_NAME,
|
|---|
| 76 | expressionIsUnsupported(
|
|---|
| 77 | parser,
|
|---|
| 78 | `${name} is not supported by webpack.`
|
|---|
| 79 | )
|
|---|
| 80 | );
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | parser.hooks.typeof
|
|---|
| 84 | .for("System.import")
|
|---|
| 85 | .tap(
|
|---|
| 86 | PLUGIN_NAME,
|
|---|
| 87 | toConstantDependency(parser, JSON.stringify("function"))
|
|---|
| 88 | );
|
|---|
| 89 | parser.hooks.evaluateTypeof
|
|---|
| 90 | .for("System.import")
|
|---|
| 91 | .tap(PLUGIN_NAME, evaluateToString("function"));
|
|---|
| 92 | parser.hooks.typeof
|
|---|
| 93 | .for("System")
|
|---|
| 94 | .tap(
|
|---|
| 95 | PLUGIN_NAME,
|
|---|
| 96 | toConstantDependency(parser, JSON.stringify("object"))
|
|---|
| 97 | );
|
|---|
| 98 | parser.hooks.evaluateTypeof
|
|---|
| 99 | .for("System")
|
|---|
| 100 | .tap(PLUGIN_NAME, evaluateToString("object"));
|
|---|
| 101 |
|
|---|
| 102 | setNotSupported("System.set");
|
|---|
| 103 | setNotSupported("System.get");
|
|---|
| 104 | setNotSupported("System.register");
|
|---|
| 105 |
|
|---|
| 106 | parser.hooks.expression.for("System").tap(PLUGIN_NAME, (expr) => {
|
|---|
| 107 | const dep = new ConstDependency(
|
|---|
| 108 | RuntimeGlobals.system,
|
|---|
| 109 | /** @type {Range} */ (expr.range),
|
|---|
| 110 | [RuntimeGlobals.system]
|
|---|
| 111 | );
|
|---|
| 112 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|---|
| 113 | parser.state.module.addPresentationalDependency(dep);
|
|---|
| 114 | return true;
|
|---|
| 115 | });
|
|---|
| 116 |
|
|---|
| 117 | parser.hooks.call.for("System.import").tap(PLUGIN_NAME, (expr) => {
|
|---|
| 118 | parser.state.module.addWarning(
|
|---|
| 119 | new SystemImportDeprecationWarning(
|
|---|
| 120 | /** @type {DependencyLocation} */ (expr.loc)
|
|---|
| 121 | )
|
|---|
| 122 | );
|
|---|
| 123 |
|
|---|
| 124 | return parser.hooks.importCall.call({
|
|---|
| 125 | type: "ImportExpression",
|
|---|
| 126 | source:
|
|---|
| 127 | /** @type {import("estree").Literal} */
|
|---|
| 128 | (expr.arguments[0]),
|
|---|
| 129 | loc: expr.loc,
|
|---|
| 130 | range: expr.range,
|
|---|
| 131 | options: null
|
|---|
| 132 | });
|
|---|
| 133 | });
|
|---|
| 134 | };
|
|---|
| 135 |
|
|---|
| 136 | normalModuleFactory.hooks.parser
|
|---|
| 137 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
|---|
| 138 | .tap(PLUGIN_NAME, handler);
|
|---|
| 139 | normalModuleFactory.hooks.parser
|
|---|
| 140 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
|---|
| 141 | .tap(PLUGIN_NAME, handler);
|
|---|
| 142 | }
|
|---|
| 143 | );
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | class SystemImportDeprecationWarning extends WebpackError {
|
|---|
| 148 | /**
|
|---|
| 149 | * Creates an instance of SystemImportDeprecationWarning.
|
|---|
| 150 | * @param {DependencyLocation} loc location
|
|---|
| 151 | */
|
|---|
| 152 | constructor(loc) {
|
|---|
| 153 | super(
|
|---|
| 154 | "System.import() is deprecated and will be removed soon. Use import() instead.\n" +
|
|---|
| 155 | "For more info visit https://webpack.js.org/guides/code-splitting/"
|
|---|
| 156 | );
|
|---|
| 157 |
|
|---|
| 158 | this.name = "SystemImportDeprecationWarning";
|
|---|
| 159 |
|
|---|
| 160 | this.loc = loc;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | makeSerializable(
|
|---|
| 165 | SystemImportDeprecationWarning,
|
|---|
| 166 | "webpack/lib/dependencies/SystemPlugin",
|
|---|
| 167 | "SystemImportDeprecationWarning"
|
|---|
| 168 | );
|
|---|
| 169 |
|
|---|
| 170 | module.exports = SystemPlugin;
|
|---|
| 171 | module.exports.SystemImportDeprecationWarning = SystemImportDeprecationWarning;
|
|---|