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 RuntimeGlobals = require("../RuntimeGlobals");
|
---|
9 | const WebpackError = require("../WebpackError");
|
---|
10 | const {
|
---|
11 | evaluateToString,
|
---|
12 | expressionIsUnsupported,
|
---|
13 | toConstantDependency
|
---|
14 | } = require("../javascript/JavascriptParserHelpers");
|
---|
15 | const makeSerializable = require("../util/makeSerializable");
|
---|
16 | const ConstDependency = require("./ConstDependency");
|
---|
17 | const SystemRuntimeModule = require("./SystemRuntimeModule");
|
---|
18 |
|
---|
19 | /** @typedef {import("../Compiler")} Compiler */
|
---|
20 |
|
---|
21 | class SystemPlugin {
|
---|
22 | /**
|
---|
23 | * Apply the plugin
|
---|
24 | * @param {Compiler} compiler the compiler instance
|
---|
25 | * @returns {void}
|
---|
26 | */
|
---|
27 | apply(compiler) {
|
---|
28 | compiler.hooks.compilation.tap(
|
---|
29 | "SystemPlugin",
|
---|
30 | (compilation, { normalModuleFactory }) => {
|
---|
31 | compilation.hooks.runtimeRequirementInModule
|
---|
32 | .for(RuntimeGlobals.system)
|
---|
33 | .tap("SystemPlugin", (module, set) => {
|
---|
34 | set.add(RuntimeGlobals.requireScope);
|
---|
35 | });
|
---|
36 |
|
---|
37 | compilation.hooks.runtimeRequirementInTree
|
---|
38 | .for(RuntimeGlobals.system)
|
---|
39 | .tap("SystemPlugin", (chunk, set) => {
|
---|
40 | compilation.addRuntimeModule(chunk, new SystemRuntimeModule());
|
---|
41 | });
|
---|
42 |
|
---|
43 | const handler = (parser, parserOptions) => {
|
---|
44 | if (parserOptions.system === undefined || !parserOptions.system) {
|
---|
45 | return;
|
---|
46 | }
|
---|
47 |
|
---|
48 | const setNotSupported = name => {
|
---|
49 | parser.hooks.evaluateTypeof
|
---|
50 | .for(name)
|
---|
51 | .tap("SystemPlugin", evaluateToString("undefined"));
|
---|
52 | parser.hooks.expression
|
---|
53 | .for(name)
|
---|
54 | .tap(
|
---|
55 | "SystemPlugin",
|
---|
56 | expressionIsUnsupported(
|
---|
57 | parser,
|
---|
58 | name + " is not supported by webpack."
|
---|
59 | )
|
---|
60 | );
|
---|
61 | };
|
---|
62 |
|
---|
63 | parser.hooks.typeof
|
---|
64 | .for("System.import")
|
---|
65 | .tap(
|
---|
66 | "SystemPlugin",
|
---|
67 | toConstantDependency(parser, JSON.stringify("function"))
|
---|
68 | );
|
---|
69 | parser.hooks.evaluateTypeof
|
---|
70 | .for("System.import")
|
---|
71 | .tap("SystemPlugin", evaluateToString("function"));
|
---|
72 | parser.hooks.typeof
|
---|
73 | .for("System")
|
---|
74 | .tap(
|
---|
75 | "SystemPlugin",
|
---|
76 | toConstantDependency(parser, JSON.stringify("object"))
|
---|
77 | );
|
---|
78 | parser.hooks.evaluateTypeof
|
---|
79 | .for("System")
|
---|
80 | .tap("SystemPlugin", evaluateToString("object"));
|
---|
81 |
|
---|
82 | setNotSupported("System.set");
|
---|
83 | setNotSupported("System.get");
|
---|
84 | setNotSupported("System.register");
|
---|
85 |
|
---|
86 | parser.hooks.expression.for("System").tap("SystemPlugin", expr => {
|
---|
87 | const dep = new ConstDependency(RuntimeGlobals.system, expr.range, [
|
---|
88 | RuntimeGlobals.system
|
---|
89 | ]);
|
---|
90 | dep.loc = expr.loc;
|
---|
91 | parser.state.module.addPresentationalDependency(dep);
|
---|
92 | return true;
|
---|
93 | });
|
---|
94 |
|
---|
95 | parser.hooks.call.for("System.import").tap("SystemPlugin", expr => {
|
---|
96 | parser.state.module.addWarning(
|
---|
97 | new SystemImportDeprecationWarning(expr.loc)
|
---|
98 | );
|
---|
99 |
|
---|
100 | return parser.hooks.importCall.call({
|
---|
101 | type: "ImportExpression",
|
---|
102 | source: expr.arguments[0],
|
---|
103 | loc: expr.loc,
|
---|
104 | range: expr.range
|
---|
105 | });
|
---|
106 | });
|
---|
107 | };
|
---|
108 |
|
---|
109 | normalModuleFactory.hooks.parser
|
---|
110 | .for("javascript/auto")
|
---|
111 | .tap("SystemPlugin", handler);
|
---|
112 | normalModuleFactory.hooks.parser
|
---|
113 | .for("javascript/dynamic")
|
---|
114 | .tap("SystemPlugin", handler);
|
---|
115 | }
|
---|
116 | );
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | class SystemImportDeprecationWarning extends WebpackError {
|
---|
121 | constructor(loc) {
|
---|
122 | super(
|
---|
123 | "System.import() is deprecated and will be removed soon. Use import() instead.\n" +
|
---|
124 | "For more info visit https://webpack.js.org/guides/code-splitting/"
|
---|
125 | );
|
---|
126 |
|
---|
127 | this.name = "SystemImportDeprecationWarning";
|
---|
128 |
|
---|
129 | this.loc = loc;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | makeSerializable(
|
---|
134 | SystemImportDeprecationWarning,
|
---|
135 | "webpack/lib/dependencies/SystemPlugin",
|
---|
136 | "SystemImportDeprecationWarning"
|
---|
137 | );
|
---|
138 |
|
---|
139 | module.exports = SystemPlugin;
|
---|
140 | module.exports.SystemImportDeprecationWarning = SystemImportDeprecationWarning;
|
---|