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 InitFragment = require("./InitFragment");
|
---|
9 | const {
|
---|
10 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
11 | JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
---|
12 | JAVASCRIPT_MODULE_TYPE_ESM
|
---|
13 | } = require("./ModuleTypeConstants");
|
---|
14 | const RuntimeGlobals = require("./RuntimeGlobals");
|
---|
15 | const WebpackError = require("./WebpackError");
|
---|
16 | const ConstDependency = require("./dependencies/ConstDependency");
|
---|
17 | const BasicEvaluatedExpression = require("./javascript/BasicEvaluatedExpression");
|
---|
18 | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
---|
19 | const {
|
---|
20 | toConstantDependency,
|
---|
21 | evaluateToString
|
---|
22 | } = require("./javascript/JavascriptParserHelpers");
|
---|
23 | const ChunkNameRuntimeModule = require("./runtime/ChunkNameRuntimeModule");
|
---|
24 | const GetFullHashRuntimeModule = require("./runtime/GetFullHashRuntimeModule");
|
---|
25 |
|
---|
26 | /** @typedef {import("./Compiler")} Compiler */
|
---|
27 | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
|
---|
28 | /** @typedef {import("./Module").BuildInfo} BuildInfo */
|
---|
29 | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
---|
30 | /** @typedef {import("./javascript/JavascriptParser").Range} Range */
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @param {boolean | undefined} module true if ES module
|
---|
34 | * @param {string} importMetaName `import.meta` name
|
---|
35 | * @returns {Record<string, {expr: string, req: string[] | null, type?: string, assign: boolean}>} replacements
|
---|
36 | */
|
---|
37 | function getReplacements(module, importMetaName) {
|
---|
38 | return {
|
---|
39 | __webpack_require__: {
|
---|
40 | expr: RuntimeGlobals.require,
|
---|
41 | req: [RuntimeGlobals.require],
|
---|
42 | type: "function",
|
---|
43 | assign: false
|
---|
44 | },
|
---|
45 | __webpack_public_path__: {
|
---|
46 | expr: RuntimeGlobals.publicPath,
|
---|
47 | req: [RuntimeGlobals.publicPath],
|
---|
48 | type: "string",
|
---|
49 | assign: true
|
---|
50 | },
|
---|
51 | __webpack_base_uri__: {
|
---|
52 | expr: RuntimeGlobals.baseURI,
|
---|
53 | req: [RuntimeGlobals.baseURI],
|
---|
54 | type: "string",
|
---|
55 | assign: true
|
---|
56 | },
|
---|
57 | __webpack_modules__: {
|
---|
58 | expr: RuntimeGlobals.moduleFactories,
|
---|
59 | req: [RuntimeGlobals.moduleFactories],
|
---|
60 | type: "object",
|
---|
61 | assign: false
|
---|
62 | },
|
---|
63 | __webpack_chunk_load__: {
|
---|
64 | expr: RuntimeGlobals.ensureChunk,
|
---|
65 | req: [RuntimeGlobals.ensureChunk],
|
---|
66 | type: "function",
|
---|
67 | assign: true
|
---|
68 | },
|
---|
69 | __non_webpack_require__: {
|
---|
70 | expr: module
|
---|
71 | ? `__WEBPACK_EXTERNAL_createRequire(${importMetaName}.url)`
|
---|
72 | : "require",
|
---|
73 | req: null,
|
---|
74 | type: undefined, // type is not known, depends on environment
|
---|
75 | assign: true
|
---|
76 | },
|
---|
77 | __webpack_nonce__: {
|
---|
78 | expr: RuntimeGlobals.scriptNonce,
|
---|
79 | req: [RuntimeGlobals.scriptNonce],
|
---|
80 | type: "string",
|
---|
81 | assign: true
|
---|
82 | },
|
---|
83 | __webpack_hash__: {
|
---|
84 | expr: `${RuntimeGlobals.getFullHash}()`,
|
---|
85 | req: [RuntimeGlobals.getFullHash],
|
---|
86 | type: "string",
|
---|
87 | assign: false
|
---|
88 | },
|
---|
89 | __webpack_chunkname__: {
|
---|
90 | expr: RuntimeGlobals.chunkName,
|
---|
91 | req: [RuntimeGlobals.chunkName],
|
---|
92 | type: "string",
|
---|
93 | assign: false
|
---|
94 | },
|
---|
95 | __webpack_get_script_filename__: {
|
---|
96 | expr: RuntimeGlobals.getChunkScriptFilename,
|
---|
97 | req: [RuntimeGlobals.getChunkScriptFilename],
|
---|
98 | type: "function",
|
---|
99 | assign: true
|
---|
100 | },
|
---|
101 | __webpack_runtime_id__: {
|
---|
102 | expr: RuntimeGlobals.runtimeId,
|
---|
103 | req: [RuntimeGlobals.runtimeId],
|
---|
104 | assign: false
|
---|
105 | },
|
---|
106 | "require.onError": {
|
---|
107 | expr: RuntimeGlobals.uncaughtErrorHandler,
|
---|
108 | req: [RuntimeGlobals.uncaughtErrorHandler],
|
---|
109 | type: undefined, // type is not known, could be function or undefined
|
---|
110 | assign: true // is never a pattern
|
---|
111 | },
|
---|
112 | __system_context__: {
|
---|
113 | expr: RuntimeGlobals.systemContext,
|
---|
114 | req: [RuntimeGlobals.systemContext],
|
---|
115 | type: "object",
|
---|
116 | assign: false
|
---|
117 | },
|
---|
118 | __webpack_share_scopes__: {
|
---|
119 | expr: RuntimeGlobals.shareScopeMap,
|
---|
120 | req: [RuntimeGlobals.shareScopeMap],
|
---|
121 | type: "object",
|
---|
122 | assign: false
|
---|
123 | },
|
---|
124 | __webpack_init_sharing__: {
|
---|
125 | expr: RuntimeGlobals.initializeSharing,
|
---|
126 | req: [RuntimeGlobals.initializeSharing],
|
---|
127 | type: "function",
|
---|
128 | assign: true
|
---|
129 | }
|
---|
130 | };
|
---|
131 | }
|
---|
132 |
|
---|
133 | const PLUGIN_NAME = "APIPlugin";
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * @typedef {object} APIPluginOptions
|
---|
137 | * @property {boolean} [module] the output filename
|
---|
138 | */
|
---|
139 |
|
---|
140 | class APIPlugin {
|
---|
141 | /**
|
---|
142 | * @param {APIPluginOptions} [options] options
|
---|
143 | */
|
---|
144 | constructor(options = {}) {
|
---|
145 | this.options = options;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Apply the plugin
|
---|
150 | * @param {Compiler} compiler the compiler instance
|
---|
151 | * @returns {void}
|
---|
152 | */
|
---|
153 | apply(compiler) {
|
---|
154 | compiler.hooks.compilation.tap(
|
---|
155 | PLUGIN_NAME,
|
---|
156 | (compilation, { normalModuleFactory }) => {
|
---|
157 | const importMetaName = /** @type {string} */ (
|
---|
158 | compilation.outputOptions.importMetaName
|
---|
159 | );
|
---|
160 | const REPLACEMENTS = getReplacements(
|
---|
161 | this.options.module,
|
---|
162 | importMetaName
|
---|
163 | );
|
---|
164 |
|
---|
165 | compilation.dependencyTemplates.set(
|
---|
166 | ConstDependency,
|
---|
167 | new ConstDependency.Template()
|
---|
168 | );
|
---|
169 |
|
---|
170 | compilation.hooks.runtimeRequirementInTree
|
---|
171 | .for(RuntimeGlobals.chunkName)
|
---|
172 | .tap(PLUGIN_NAME, chunk => {
|
---|
173 | compilation.addRuntimeModule(
|
---|
174 | chunk,
|
---|
175 | new ChunkNameRuntimeModule(/** @type {string} */ (chunk.name))
|
---|
176 | );
|
---|
177 | return true;
|
---|
178 | });
|
---|
179 |
|
---|
180 | compilation.hooks.runtimeRequirementInTree
|
---|
181 | .for(RuntimeGlobals.getFullHash)
|
---|
182 | .tap(PLUGIN_NAME, (chunk, set) => {
|
---|
183 | compilation.addRuntimeModule(chunk, new GetFullHashRuntimeModule());
|
---|
184 | return true;
|
---|
185 | });
|
---|
186 |
|
---|
187 | const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
|
---|
188 |
|
---|
189 | hooks.renderModuleContent.tap(
|
---|
190 | PLUGIN_NAME,
|
---|
191 | (source, module, renderContext) => {
|
---|
192 | if (/** @type {BuildInfo} */ (module.buildInfo).needCreateRequire) {
|
---|
193 | const needPrefix =
|
---|
194 | renderContext.runtimeTemplate.supportNodePrefixForCoreModules();
|
---|
195 | const chunkInitFragments = [
|
---|
196 | new InitFragment(
|
---|
197 | `import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "${
|
---|
198 | needPrefix ? "node:" : ""
|
---|
199 | }module";\n`,
|
---|
200 | InitFragment.STAGE_HARMONY_IMPORTS,
|
---|
201 | 0,
|
---|
202 | "external module node-commonjs"
|
---|
203 | )
|
---|
204 | ];
|
---|
205 |
|
---|
206 | renderContext.chunkInitFragments.push(...chunkInitFragments);
|
---|
207 | }
|
---|
208 |
|
---|
209 | return source;
|
---|
210 | }
|
---|
211 | );
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * @param {JavascriptParser} parser the parser
|
---|
215 | */
|
---|
216 | const handler = parser => {
|
---|
217 | for (const key of Object.keys(REPLACEMENTS)) {
|
---|
218 | const info = REPLACEMENTS[key];
|
---|
219 | parser.hooks.expression.for(key).tap(PLUGIN_NAME, expression => {
|
---|
220 | const dep = toConstantDependency(parser, info.expr, info.req);
|
---|
221 |
|
---|
222 | if (key === "__non_webpack_require__" && this.options.module) {
|
---|
223 | /** @type {BuildInfo} */
|
---|
224 | (parser.state.module.buildInfo).needCreateRequire = true;
|
---|
225 | }
|
---|
226 |
|
---|
227 | return dep(expression);
|
---|
228 | });
|
---|
229 | if (info.assign === false) {
|
---|
230 | parser.hooks.assign.for(key).tap(PLUGIN_NAME, expr => {
|
---|
231 | const err = new WebpackError(`${key} must not be assigned`);
|
---|
232 | err.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
233 | throw err;
|
---|
234 | });
|
---|
235 | }
|
---|
236 | if (info.type) {
|
---|
237 | parser.hooks.evaluateTypeof
|
---|
238 | .for(key)
|
---|
239 | .tap(PLUGIN_NAME, evaluateToString(info.type));
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | parser.hooks.expression
|
---|
244 | .for("__webpack_layer__")
|
---|
245 | .tap(PLUGIN_NAME, expr => {
|
---|
246 | const dep = new ConstDependency(
|
---|
247 | JSON.stringify(parser.state.module.layer),
|
---|
248 | /** @type {Range} */ (expr.range)
|
---|
249 | );
|
---|
250 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
251 | parser.state.module.addPresentationalDependency(dep);
|
---|
252 | return true;
|
---|
253 | });
|
---|
254 | parser.hooks.evaluateIdentifier
|
---|
255 | .for("__webpack_layer__")
|
---|
256 | .tap(PLUGIN_NAME, expr =>
|
---|
257 | (parser.state.module.layer === null
|
---|
258 | ? new BasicEvaluatedExpression().setNull()
|
---|
259 | : new BasicEvaluatedExpression().setString(
|
---|
260 | parser.state.module.layer
|
---|
261 | )
|
---|
262 | ).setRange(/** @type {Range} */ (expr.range))
|
---|
263 | );
|
---|
264 | parser.hooks.evaluateTypeof
|
---|
265 | .for("__webpack_layer__")
|
---|
266 | .tap(PLUGIN_NAME, expr =>
|
---|
267 | new BasicEvaluatedExpression()
|
---|
268 | .setString(
|
---|
269 | parser.state.module.layer === null ? "object" : "string"
|
---|
270 | )
|
---|
271 | .setRange(/** @type {Range} */ (expr.range))
|
---|
272 | );
|
---|
273 |
|
---|
274 | parser.hooks.expression
|
---|
275 | .for("__webpack_module__.id")
|
---|
276 | .tap(PLUGIN_NAME, expr => {
|
---|
277 | /** @type {BuildInfo} */
|
---|
278 | (parser.state.module.buildInfo).moduleConcatenationBailout =
|
---|
279 | "__webpack_module__.id";
|
---|
280 | const dep = new ConstDependency(
|
---|
281 | `${parser.state.module.moduleArgument}.id`,
|
---|
282 | /** @type {Range} */ (expr.range),
|
---|
283 | [RuntimeGlobals.moduleId]
|
---|
284 | );
|
---|
285 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
286 | parser.state.module.addPresentationalDependency(dep);
|
---|
287 | return true;
|
---|
288 | });
|
---|
289 |
|
---|
290 | parser.hooks.expression
|
---|
291 | .for("__webpack_module__")
|
---|
292 | .tap(PLUGIN_NAME, expr => {
|
---|
293 | /** @type {BuildInfo} */
|
---|
294 | (parser.state.module.buildInfo).moduleConcatenationBailout =
|
---|
295 | "__webpack_module__";
|
---|
296 | const dep = new ConstDependency(
|
---|
297 | parser.state.module.moduleArgument,
|
---|
298 | /** @type {Range} */ (expr.range),
|
---|
299 | [RuntimeGlobals.module]
|
---|
300 | );
|
---|
301 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
302 | parser.state.module.addPresentationalDependency(dep);
|
---|
303 | return true;
|
---|
304 | });
|
---|
305 | parser.hooks.evaluateTypeof
|
---|
306 | .for("__webpack_module__")
|
---|
307 | .tap(PLUGIN_NAME, evaluateToString("object"));
|
---|
308 | };
|
---|
309 |
|
---|
310 | normalModuleFactory.hooks.parser
|
---|
311 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
312 | .tap(PLUGIN_NAME, handler);
|
---|
313 | normalModuleFactory.hooks.parser
|
---|
314 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
315 | .tap(PLUGIN_NAME, handler);
|
---|
316 | normalModuleFactory.hooks.parser
|
---|
317 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
---|
318 | .tap(PLUGIN_NAME, handler);
|
---|
319 | }
|
---|
320 | );
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | module.exports = APIPlugin;
|
---|