1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const RuntimeGlobals = require("./RuntimeGlobals");
|
9 | const WebpackError = require("./WebpackError");
|
10 | const ConstDependency = require("./dependencies/ConstDependency");
|
11 | const {
|
12 | toConstantDependency,
|
13 | evaluateToString
|
14 | } = require("./javascript/JavascriptParserHelpers");
|
15 | const ChunkNameRuntimeModule = require("./runtime/ChunkNameRuntimeModule");
|
16 | const GetFullHashRuntimeModule = require("./runtime/GetFullHashRuntimeModule");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const REPLACEMENTS = {
|
22 | __webpack_require__: {
|
23 | expr: RuntimeGlobals.require,
|
24 | req: [RuntimeGlobals.require],
|
25 | type: "function",
|
26 | assign: false
|
27 | },
|
28 | __webpack_public_path__: {
|
29 | expr: RuntimeGlobals.publicPath,
|
30 | req: [RuntimeGlobals.publicPath],
|
31 | type: "string",
|
32 | assign: true
|
33 | },
|
34 | __webpack_modules__: {
|
35 | expr: RuntimeGlobals.moduleFactories,
|
36 | req: [RuntimeGlobals.moduleFactories],
|
37 | type: "object",
|
38 | assign: false
|
39 | },
|
40 | __webpack_chunk_load__: {
|
41 | expr: RuntimeGlobals.ensureChunk,
|
42 | req: [RuntimeGlobals.ensureChunk],
|
43 | type: "function",
|
44 | assign: true
|
45 | },
|
46 | __non_webpack_require__: {
|
47 | expr: "require",
|
48 | req: null,
|
49 | type: undefined,
|
50 | assign: true
|
51 | },
|
52 | __webpack_nonce__: {
|
53 | expr: RuntimeGlobals.scriptNonce,
|
54 | req: [RuntimeGlobals.scriptNonce],
|
55 | type: "string",
|
56 | assign: true
|
57 | },
|
58 | __webpack_hash__: {
|
59 | expr: `${RuntimeGlobals.getFullHash}()`,
|
60 | req: [RuntimeGlobals.getFullHash],
|
61 | type: "string",
|
62 | assign: false
|
63 | },
|
64 | __webpack_chunkname__: {
|
65 | expr: RuntimeGlobals.chunkName,
|
66 | req: [RuntimeGlobals.chunkName],
|
67 | type: "string",
|
68 | assign: false
|
69 | },
|
70 | __webpack_get_script_filename__: {
|
71 | expr: RuntimeGlobals.getChunkScriptFilename,
|
72 | req: [RuntimeGlobals.getChunkScriptFilename],
|
73 | type: "function",
|
74 | assign: true
|
75 | },
|
76 | "require.onError": {
|
77 | expr: RuntimeGlobals.uncaughtErrorHandler,
|
78 | req: [RuntimeGlobals.uncaughtErrorHandler],
|
79 | type: undefined,
|
80 | assign: true
|
81 | },
|
82 | __system_context__: {
|
83 | expr: RuntimeGlobals.systemContext,
|
84 | req: [RuntimeGlobals.systemContext],
|
85 | type: "object",
|
86 | assign: false
|
87 | },
|
88 | __webpack_share_scopes__: {
|
89 | expr: RuntimeGlobals.shareScopeMap,
|
90 | req: [RuntimeGlobals.shareScopeMap],
|
91 | type: "object",
|
92 | assign: false
|
93 | },
|
94 | __webpack_init_sharing__: {
|
95 | expr: RuntimeGlobals.initializeSharing,
|
96 | req: [RuntimeGlobals.initializeSharing],
|
97 | type: "function",
|
98 | assign: true
|
99 | }
|
100 | };
|
101 |
|
102 |
|
103 | class APIPlugin {
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 | apply(compiler) {
|
110 | compiler.hooks.compilation.tap(
|
111 | "APIPlugin",
|
112 | (compilation, { normalModuleFactory }) => {
|
113 | compilation.dependencyTemplates.set(
|
114 | ConstDependency,
|
115 | new ConstDependency.Template()
|
116 | );
|
117 |
|
118 | compilation.hooks.runtimeRequirementInTree
|
119 | .for(RuntimeGlobals.chunkName)
|
120 | .tap("APIPlugin", chunk => {
|
121 | compilation.addRuntimeModule(
|
122 | chunk,
|
123 | new ChunkNameRuntimeModule(chunk.name)
|
124 | );
|
125 | return true;
|
126 | });
|
127 |
|
128 | compilation.hooks.runtimeRequirementInTree
|
129 | .for(RuntimeGlobals.getFullHash)
|
130 | .tap("APIPlugin", (chunk, set) => {
|
131 | compilation.addRuntimeModule(chunk, new GetFullHashRuntimeModule());
|
132 | return true;
|
133 | });
|
134 |
|
135 | const handler = parser => {
|
136 | Object.keys(REPLACEMENTS).forEach(key => {
|
137 | const info = REPLACEMENTS[key];
|
138 | parser.hooks.expression
|
139 | .for(key)
|
140 | .tap(
|
141 | "APIPlugin",
|
142 | toConstantDependency(parser, info.expr, info.req)
|
143 | );
|
144 | if (info.assign === false) {
|
145 | parser.hooks.assign.for(key).tap("APIPlugin", expr => {
|
146 | const err = new WebpackError(`${key} must not be assigned`);
|
147 | err.loc = expr.loc;
|
148 | throw err;
|
149 | });
|
150 | }
|
151 | if (info.type) {
|
152 | parser.hooks.evaluateTypeof
|
153 | .for(key)
|
154 | .tap("APIPlugin", evaluateToString(info.type));
|
155 | }
|
156 | });
|
157 | };
|
158 |
|
159 | normalModuleFactory.hooks.parser
|
160 | .for("javascript/auto")
|
161 | .tap("APIPlugin", handler);
|
162 | normalModuleFactory.hooks.parser
|
163 | .for("javascript/dynamic")
|
164 | .tap("APIPlugin", handler);
|
165 | normalModuleFactory.hooks.parser
|
166 | .for("javascript/esm")
|
167 | .tap("APIPlugin", handler);
|
168 | }
|
169 | );
|
170 | }
|
171 | }
|
172 |
|
173 | module.exports = APIPlugin;
|