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("../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 | * Apply the plugin
|
---|
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 | * @param {Parser} parser parser parser
|
---|
55 | * @param {JavascriptParserOptions} parserOptions parserOptions
|
---|
56 | * @returns {void}
|
---|
57 | */
|
---|
58 | const handler = (parser, parserOptions) => {
|
---|
59 | if (parserOptions.system === undefined || !parserOptions.system) {
|
---|
60 | return;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * @param {string} name name
|
---|
65 | */
|
---|
66 | const setNotSupported = name => {
|
---|
67 | parser.hooks.evaluateTypeof
|
---|
68 | .for(name)
|
---|
69 | .tap(PLUGIN_NAME, evaluateToString("undefined"));
|
---|
70 | parser.hooks.expression
|
---|
71 | .for(name)
|
---|
72 | .tap(
|
---|
73 | PLUGIN_NAME,
|
---|
74 | expressionIsUnsupported(
|
---|
75 | parser,
|
---|
76 | `${name} is not supported by webpack.`
|
---|
77 | )
|
---|
78 | );
|
---|
79 | };
|
---|
80 |
|
---|
81 | parser.hooks.typeof
|
---|
82 | .for("System.import")
|
---|
83 | .tap(
|
---|
84 | PLUGIN_NAME,
|
---|
85 | toConstantDependency(parser, JSON.stringify("function"))
|
---|
86 | );
|
---|
87 | parser.hooks.evaluateTypeof
|
---|
88 | .for("System.import")
|
---|
89 | .tap(PLUGIN_NAME, evaluateToString("function"));
|
---|
90 | parser.hooks.typeof
|
---|
91 | .for("System")
|
---|
92 | .tap(
|
---|
93 | PLUGIN_NAME,
|
---|
94 | toConstantDependency(parser, JSON.stringify("object"))
|
---|
95 | );
|
---|
96 | parser.hooks.evaluateTypeof
|
---|
97 | .for("System")
|
---|
98 | .tap(PLUGIN_NAME, evaluateToString("object"));
|
---|
99 |
|
---|
100 | setNotSupported("System.set");
|
---|
101 | setNotSupported("System.get");
|
---|
102 | setNotSupported("System.register");
|
---|
103 |
|
---|
104 | parser.hooks.expression.for("System").tap(PLUGIN_NAME, expr => {
|
---|
105 | const dep = new ConstDependency(
|
---|
106 | RuntimeGlobals.system,
|
---|
107 | /** @type {Range} */ (expr.range),
|
---|
108 | [RuntimeGlobals.system]
|
---|
109 | );
|
---|
110 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
111 | parser.state.module.addPresentationalDependency(dep);
|
---|
112 | return true;
|
---|
113 | });
|
---|
114 |
|
---|
115 | parser.hooks.call.for("System.import").tap(PLUGIN_NAME, expr => {
|
---|
116 | parser.state.module.addWarning(
|
---|
117 | new SystemImportDeprecationWarning(
|
---|
118 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
119 | )
|
---|
120 | );
|
---|
121 |
|
---|
122 | return parser.hooks.importCall.call({
|
---|
123 | type: "ImportExpression",
|
---|
124 | source:
|
---|
125 | /** @type {import("estree").Literal} */
|
---|
126 | (expr.arguments[0]),
|
---|
127 | loc: expr.loc,
|
---|
128 | range: expr.range,
|
---|
129 | options: null
|
---|
130 | });
|
---|
131 | });
|
---|
132 | };
|
---|
133 |
|
---|
134 | normalModuleFactory.hooks.parser
|
---|
135 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
136 | .tap(PLUGIN_NAME, handler);
|
---|
137 | normalModuleFactory.hooks.parser
|
---|
138 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
139 | .tap(PLUGIN_NAME, handler);
|
---|
140 | }
|
---|
141 | );
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | class SystemImportDeprecationWarning extends WebpackError {
|
---|
146 | /**
|
---|
147 | * @param {DependencyLocation} loc location
|
---|
148 | */
|
---|
149 | constructor(loc) {
|
---|
150 | super(
|
---|
151 | "System.import() is deprecated and will be removed soon. Use import() instead.\n" +
|
---|
152 | "For more info visit https://webpack.js.org/guides/code-splitting/"
|
---|
153 | );
|
---|
154 |
|
---|
155 | this.name = "SystemImportDeprecationWarning";
|
---|
156 |
|
---|
157 | this.loc = loc;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | makeSerializable(
|
---|
162 | SystemImportDeprecationWarning,
|
---|
163 | "webpack/lib/dependencies/SystemPlugin",
|
---|
164 | "SystemImportDeprecationWarning"
|
---|
165 | );
|
---|
166 |
|
---|
167 | module.exports = SystemPlugin;
|
---|
168 | module.exports.SystemImportDeprecationWarning = SystemImportDeprecationWarning;
|
---|