source: frontend/node_modules/webpack/lib/RuntimePlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 18.6 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("./RuntimeGlobals");
9const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirementsDependency");
10const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
11const AsyncModuleRuntimeModule = require("./runtime/AsyncModuleRuntimeModule");
12const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
13const BaseUriRuntimeModule = require("./runtime/BaseUriRuntimeModule");
14const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
15const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
16const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
17const CreateScriptRuntimeModule = require("./runtime/CreateScriptRuntimeModule");
18const CreateScriptUrlRuntimeModule = require("./runtime/CreateScriptUrlRuntimeModule");
19const DefinePropertyGettersRuntimeModule = require("./runtime/DefinePropertyGettersRuntimeModule");
20const EnsureChunkRuntimeModule = require("./runtime/EnsureChunkRuntimeModule");
21const GetChunkFilenameRuntimeModule = require("./runtime/GetChunkFilenameRuntimeModule");
22const GetMainFilenameRuntimeModule = require("./runtime/GetMainFilenameRuntimeModule");
23const GetTrustedTypesPolicyRuntimeModule = require("./runtime/GetTrustedTypesPolicyRuntimeModule");
24const GlobalRuntimeModule = require("./runtime/GlobalRuntimeModule");
25const HasOwnPropertyRuntimeModule = require("./runtime/HasOwnPropertyRuntimeModule");
26const LoadScriptRuntimeModule = require("./runtime/LoadScriptRuntimeModule");
27const {
28 MakeDeferredNamespaceObjectRuntimeModule,
29 MakeOptimizedDeferredNamespaceObjectRuntimeModule
30} = require("./runtime/MakeDeferredNamespaceObjectRuntime");
31const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule");
32const NonceRuntimeModule = require("./runtime/NonceRuntimeModule");
33const OnChunksLoadedRuntimeModule = require("./runtime/OnChunksLoadedRuntimeModule");
34const PublicPathRuntimeModule = require("./runtime/PublicPathRuntimeModule");
35const RelativeUrlRuntimeModule = require("./runtime/RelativeUrlRuntimeModule");
36const RuntimeIdRuntimeModule = require("./runtime/RuntimeIdRuntimeModule");
37const SetAnonymousDefaultNameRuntimeModule = require("./runtime/SetAnonymousDefaultNameRuntimeModule");
38const SystemContextRuntimeModule = require("./runtime/SystemContextRuntimeModule");
39const ToBinaryRuntimeModule = require("./runtime/ToBinaryRuntimeModule");
40const ShareRuntimeModule = require("./sharing/ShareRuntimeModule");
41const StringXor = require("./util/StringXor");
42const memoize = require("./util/memoize");
43
44/** @typedef {import("../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
45/** @typedef {import("./Chunk")} Chunk */
46/** @typedef {import("./Compiler")} Compiler */
47
48const getJavascriptModulesPlugin = memoize(() =>
49 require("./javascript/JavascriptModulesPlugin")
50);
51const getCssModulesPlugin = memoize(() => require("./css/CssModulesPlugin"));
52
53const GLOBALS_ON_REQUIRE = [
54 RuntimeGlobals.chunkName,
55 RuntimeGlobals.runtimeId,
56 RuntimeGlobals.compatGetDefaultExport,
57 RuntimeGlobals.createFakeNamespaceObject,
58 RuntimeGlobals.createScript,
59 RuntimeGlobals.createScriptUrl,
60 RuntimeGlobals.getTrustedTypesPolicy,
61 RuntimeGlobals.definePropertyGetters,
62 RuntimeGlobals.ensureChunk,
63 RuntimeGlobals.entryModuleId,
64 RuntimeGlobals.getFullHash,
65 RuntimeGlobals.global,
66 RuntimeGlobals.makeNamespaceObject,
67 RuntimeGlobals.moduleCache,
68 RuntimeGlobals.moduleFactories,
69 RuntimeGlobals.moduleFactoriesAddOnly,
70 RuntimeGlobals.interceptModuleExecution,
71 RuntimeGlobals.publicPath,
72 RuntimeGlobals.baseURI,
73 RuntimeGlobals.relativeUrl,
74 // TODO webpack 6 - rename to nonce, because we use it for CSS too
75 RuntimeGlobals.scriptNonce,
76 RuntimeGlobals.uncaughtErrorHandler,
77 RuntimeGlobals.asyncModule,
78 RuntimeGlobals.wasmInstances,
79 RuntimeGlobals.instantiateWasm,
80 RuntimeGlobals.shareScopeMap,
81 RuntimeGlobals.initializeSharing,
82 RuntimeGlobals.loadScript,
83 RuntimeGlobals.setAnonymousDefaultName,
84 RuntimeGlobals.systemContext,
85 RuntimeGlobals.onChunksLoaded,
86 RuntimeGlobals.makeOptimizedDeferredNamespaceObject,
87 RuntimeGlobals.makeDeferredNamespaceObject
88];
89
90const MODULE_DEPENDENCIES = {
91 [RuntimeGlobals.moduleLoaded]: [RuntimeGlobals.module],
92 [RuntimeGlobals.moduleId]: [RuntimeGlobals.module]
93};
94
95const TREE_DEPENDENCIES = {
96 [RuntimeGlobals.definePropertyGetters]: [RuntimeGlobals.hasOwnProperty],
97 [RuntimeGlobals.compatGetDefaultExport]: [
98 RuntimeGlobals.definePropertyGetters
99 ],
100 [RuntimeGlobals.createFakeNamespaceObject]: [
101 RuntimeGlobals.definePropertyGetters,
102 RuntimeGlobals.makeNamespaceObject,
103 RuntimeGlobals.require
104 ],
105 [RuntimeGlobals.makeOptimizedDeferredNamespaceObject]: [
106 RuntimeGlobals.require
107 ],
108 [RuntimeGlobals.makeDeferredNamespaceObject]: [
109 RuntimeGlobals.createFakeNamespaceObject,
110 RuntimeGlobals.require
111 ],
112 [RuntimeGlobals.initializeSharing]: [RuntimeGlobals.shareScopeMap],
113 [RuntimeGlobals.shareScopeMap]: [RuntimeGlobals.hasOwnProperty]
114};
115
116const FULLHASH_REGEXP = /\[(?:full)?hash(?::\d+)?\]/;
117
118const PLUGIN_NAME = "RuntimePlugin";
119
120class RuntimePlugin {
121 /**
122 * Applies the plugin by registering its hooks on the compiler.
123 * @param {Compiler} compiler the Compiler
124 * @returns {void}
125 */
126 apply(compiler) {
127 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
128 const globalChunkLoading = compilation.outputOptions.chunkLoading;
129 /**
130 * Checks whether this runtime plugin is chunk loading disabled for chunk.
131 * @param {Chunk} chunk chunk
132 * @returns {boolean} true, when chunk loading is disabled for the chunk
133 */
134 const isChunkLoadingDisabledForChunk = (chunk) => {
135 const options = chunk.getEntryOptions();
136 const chunkLoading =
137 options && options.chunkLoading !== undefined
138 ? options.chunkLoading
139 : globalChunkLoading;
140 return chunkLoading === false;
141 };
142 compilation.dependencyTemplates.set(
143 RuntimeRequirementsDependency,
144 new RuntimeRequirementsDependency.Template()
145 );
146 for (const req of GLOBALS_ON_REQUIRE) {
147 compilation.hooks.runtimeRequirementInModule
148 .for(req)
149 .tap(PLUGIN_NAME, (module, set) => {
150 set.add(RuntimeGlobals.requireScope);
151 });
152 compilation.hooks.runtimeRequirementInTree
153 .for(req)
154 .tap(PLUGIN_NAME, (module, set) => {
155 set.add(RuntimeGlobals.requireScope);
156 });
157 }
158 for (const req of Object.keys(TREE_DEPENDENCIES)) {
159 const deps =
160 TREE_DEPENDENCIES[/** @type {keyof TREE_DEPENDENCIES} */ (req)];
161 compilation.hooks.runtimeRequirementInTree
162 .for(req)
163 .tap(PLUGIN_NAME, (chunk, set) => {
164 for (const dep of deps) set.add(dep);
165 });
166 }
167 for (const req of Object.keys(MODULE_DEPENDENCIES)) {
168 const deps =
169 MODULE_DEPENDENCIES[/** @type {keyof MODULE_DEPENDENCIES} */ (req)];
170 compilation.hooks.runtimeRequirementInModule
171 .for(req)
172 .tap(PLUGIN_NAME, (chunk, set) => {
173 for (const dep of deps) set.add(dep);
174 });
175 }
176 compilation.hooks.runtimeRequirementInTree
177 .for(RuntimeGlobals.definePropertyGetters)
178 .tap(PLUGIN_NAME, (chunk) => {
179 compilation.addRuntimeModule(
180 chunk,
181 new DefinePropertyGettersRuntimeModule()
182 );
183 return true;
184 });
185 compilation.hooks.runtimeRequirementInTree
186 .for(RuntimeGlobals.makeNamespaceObject)
187 .tap(PLUGIN_NAME, (chunk) => {
188 compilation.addRuntimeModule(
189 chunk,
190 new MakeNamespaceObjectRuntimeModule()
191 );
192 return true;
193 });
194 compilation.hooks.runtimeRequirementInTree
195 .for(RuntimeGlobals.createFakeNamespaceObject)
196 .tap(PLUGIN_NAME, (chunk) => {
197 compilation.addRuntimeModule(
198 chunk,
199 new CreateFakeNamespaceObjectRuntimeModule()
200 );
201 return true;
202 });
203 compilation.hooks.runtimeRequirementInTree
204 .for(RuntimeGlobals.makeOptimizedDeferredNamespaceObject)
205 .tap("RuntimePlugin", (chunk, runtimeRequirement) => {
206 compilation.addRuntimeModule(
207 chunk,
208 new MakeOptimizedDeferredNamespaceObjectRuntimeModule(
209 runtimeRequirement.has(RuntimeGlobals.asyncModule)
210 )
211 );
212 return true;
213 });
214 compilation.hooks.runtimeRequirementInTree
215 .for(RuntimeGlobals.makeDeferredNamespaceObject)
216 .tap("RuntimePlugin", (chunk, runtimeRequirement) => {
217 compilation.addRuntimeModule(
218 chunk,
219 new MakeDeferredNamespaceObjectRuntimeModule(
220 runtimeRequirement.has(RuntimeGlobals.asyncModule)
221 )
222 );
223 return true;
224 });
225 compilation.hooks.runtimeRequirementInTree
226 .for(RuntimeGlobals.hasOwnProperty)
227 .tap(PLUGIN_NAME, (chunk) => {
228 compilation.addRuntimeModule(
229 chunk,
230 new HasOwnPropertyRuntimeModule()
231 );
232 return true;
233 });
234 compilation.hooks.runtimeRequirementInTree
235 .for(RuntimeGlobals.compatGetDefaultExport)
236 .tap(PLUGIN_NAME, (chunk) => {
237 compilation.addRuntimeModule(
238 chunk,
239 new CompatGetDefaultExportRuntimeModule()
240 );
241 return true;
242 });
243 compilation.hooks.runtimeRequirementInTree
244 .for(RuntimeGlobals.setAnonymousDefaultName)
245 .tap(PLUGIN_NAME, (chunk) => {
246 compilation.addRuntimeModule(
247 chunk,
248 new SetAnonymousDefaultNameRuntimeModule()
249 );
250 return true;
251 });
252 compilation.hooks.runtimeRequirementInTree
253 .for(RuntimeGlobals.runtimeId)
254 .tap(PLUGIN_NAME, (chunk) => {
255 compilation.addRuntimeModule(chunk, new RuntimeIdRuntimeModule());
256 return true;
257 });
258 compilation.hooks.runtimeRequirementInTree
259 .for(RuntimeGlobals.publicPath)
260 .tap(PLUGIN_NAME, (chunk, set) => {
261 const { outputOptions } = compilation;
262 const { publicPath: globalPublicPath, scriptType } = outputOptions;
263 const entryOptions = chunk.getEntryOptions();
264 const publicPath =
265 entryOptions && entryOptions.publicPath !== undefined
266 ? entryOptions.publicPath
267 : globalPublicPath;
268
269 if (publicPath === "auto") {
270 const module = new AutoPublicPathRuntimeModule();
271 if (
272 scriptType !== "module" &&
273 !outputOptions.environment.globalThis
274 ) {
275 set.add(RuntimeGlobals.global);
276 }
277
278 compilation.addRuntimeModule(chunk, module);
279 } else {
280 const module = new PublicPathRuntimeModule(publicPath);
281
282 if (
283 typeof publicPath !== "string" ||
284 FULLHASH_REGEXP.test(publicPath)
285 ) {
286 module.fullHash = true;
287 }
288
289 compilation.addRuntimeModule(chunk, module);
290 }
291 return true;
292 });
293 compilation.hooks.runtimeRequirementInTree
294 .for(RuntimeGlobals.global)
295 .tap(PLUGIN_NAME, (chunk) => {
296 compilation.addRuntimeModule(chunk, new GlobalRuntimeModule());
297 return true;
298 });
299 compilation.hooks.runtimeRequirementInTree
300 .for(RuntimeGlobals.asyncModule)
301 .tap(PLUGIN_NAME, (chunk) => {
302 const experiments = compilation.options.experiments;
303 compilation.addRuntimeModule(
304 chunk,
305 new AsyncModuleRuntimeModule(experiments.deferImport)
306 );
307 return true;
308 });
309 compilation.hooks.runtimeRequirementInTree
310 .for(RuntimeGlobals.systemContext)
311 .tap(PLUGIN_NAME, (chunk) => {
312 const entryOptions = chunk.getEntryOptions();
313 const libraryType =
314 entryOptions && entryOptions.library !== undefined
315 ? entryOptions.library.type
316 : /** @type {LibraryOptions} */
317 (compilation.outputOptions.library).type;
318
319 if (libraryType === "system") {
320 compilation.addRuntimeModule(
321 chunk,
322 new SystemContextRuntimeModule()
323 );
324 }
325 return true;
326 });
327 compilation.hooks.runtimeRequirementInTree
328 .for(RuntimeGlobals.getChunkScriptFilename)
329 .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
330 if (
331 typeof compilation.outputOptions.chunkFilename === "string" &&
332 FULLHASH_REGEXP.test(compilation.outputOptions.chunkFilename)
333 ) {
334 set.add(RuntimeGlobals.getFullHash);
335 }
336 compilation.addRuntimeModule(
337 chunk,
338 new GetChunkFilenameRuntimeModule(
339 "javascript",
340 "javascript",
341 RuntimeGlobals.getChunkScriptFilename,
342 (chunk) =>
343 getJavascriptModulesPlugin().chunkHasJs(chunk, chunkGraph) &&
344 (chunk.filenameTemplate ||
345 (chunk.canBeInitial()
346 ? compilation.outputOptions.filename
347 : compilation.outputOptions.chunkFilename)),
348 set.has(RuntimeGlobals.hmrDownloadUpdateHandlers)
349 )
350 );
351 return true;
352 });
353 compilation.hooks.runtimeRequirementInTree
354 .for(RuntimeGlobals.getChunkCssFilename)
355 .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
356 if (
357 typeof compilation.outputOptions.cssChunkFilename === "string" &&
358 FULLHASH_REGEXP.test(compilation.outputOptions.cssChunkFilename)
359 ) {
360 set.add(RuntimeGlobals.getFullHash);
361 }
362 compilation.addRuntimeModule(
363 chunk,
364 new GetChunkFilenameRuntimeModule(
365 "css",
366 "css",
367 RuntimeGlobals.getChunkCssFilename,
368 (chunk) => {
369 const cssModulePlugin = getCssModulesPlugin();
370
371 return (
372 cssModulePlugin.chunkHasCss(chunk, chunkGraph) &&
373 cssModulePlugin.getChunkFilenameTemplate(
374 chunk,
375 compilation.outputOptions
376 )
377 );
378 },
379 set.has(RuntimeGlobals.hmrDownloadUpdateHandlers)
380 )
381 );
382 return true;
383 });
384 compilation.hooks.runtimeRequirementInTree
385 .for(RuntimeGlobals.getChunkUpdateScriptFilename)
386 .tap(PLUGIN_NAME, (chunk, set) => {
387 if (
388 FULLHASH_REGEXP.test(
389 compilation.outputOptions.hotUpdateChunkFilename
390 )
391 ) {
392 set.add(RuntimeGlobals.getFullHash);
393 }
394 compilation.addRuntimeModule(
395 chunk,
396 new GetChunkFilenameRuntimeModule(
397 "javascript",
398 "javascript update",
399 RuntimeGlobals.getChunkUpdateScriptFilename,
400 (_chunk) => compilation.outputOptions.hotUpdateChunkFilename,
401 true
402 )
403 );
404 return true;
405 });
406 compilation.hooks.runtimeRequirementInTree
407 .for(RuntimeGlobals.getUpdateManifestFilename)
408 .tap(PLUGIN_NAME, (chunk, set) => {
409 if (
410 FULLHASH_REGEXP.test(
411 compilation.outputOptions.hotUpdateMainFilename
412 )
413 ) {
414 set.add(RuntimeGlobals.getFullHash);
415 }
416 compilation.addRuntimeModule(
417 chunk,
418 new GetMainFilenameRuntimeModule(
419 "update manifest",
420 RuntimeGlobals.getUpdateManifestFilename,
421 compilation.outputOptions.hotUpdateMainFilename
422 )
423 );
424 return true;
425 });
426 compilation.hooks.runtimeRequirementInTree
427 .for(RuntimeGlobals.ensureChunk)
428 .tap(PLUGIN_NAME, (chunk, set) => {
429 const hasAsyncChunks = chunk.hasAsyncChunks();
430 if (hasAsyncChunks) {
431 set.add(RuntimeGlobals.ensureChunkHandlers);
432 }
433 compilation.addRuntimeModule(
434 chunk,
435 new EnsureChunkRuntimeModule(set)
436 );
437 return true;
438 });
439 compilation.hooks.runtimeRequirementInTree
440 .for(RuntimeGlobals.ensureChunkIncludeEntries)
441 .tap(PLUGIN_NAME, (chunk, set) => {
442 set.add(RuntimeGlobals.ensureChunkHandlers);
443 });
444 compilation.hooks.runtimeRequirementInTree
445 .for(RuntimeGlobals.shareScopeMap)
446 .tap(PLUGIN_NAME, (chunk, set) => {
447 compilation.addRuntimeModule(chunk, new ShareRuntimeModule());
448 return true;
449 });
450 compilation.hooks.runtimeRequirementInTree
451 .for(RuntimeGlobals.loadScript)
452 .tap(PLUGIN_NAME, (chunk, set) => {
453 const withCreateScriptUrl = Boolean(
454 compilation.outputOptions.trustedTypes
455 );
456 if (withCreateScriptUrl) {
457 set.add(RuntimeGlobals.createScriptUrl);
458 }
459 const withFetchPriority = set.has(RuntimeGlobals.hasFetchPriority);
460 compilation.addRuntimeModule(
461 chunk,
462 new LoadScriptRuntimeModule(withCreateScriptUrl, withFetchPriority)
463 );
464 return true;
465 });
466 compilation.hooks.runtimeRequirementInTree
467 .for(RuntimeGlobals.createScript)
468 .tap(PLUGIN_NAME, (chunk, set) => {
469 if (compilation.outputOptions.trustedTypes) {
470 set.add(RuntimeGlobals.getTrustedTypesPolicy);
471 }
472 compilation.addRuntimeModule(chunk, new CreateScriptRuntimeModule());
473 return true;
474 });
475 compilation.hooks.runtimeRequirementInTree
476 .for(RuntimeGlobals.createScriptUrl)
477 .tap(PLUGIN_NAME, (chunk, set) => {
478 if (compilation.outputOptions.trustedTypes) {
479 set.add(RuntimeGlobals.getTrustedTypesPolicy);
480 }
481 compilation.addRuntimeModule(
482 chunk,
483 new CreateScriptUrlRuntimeModule()
484 );
485 return true;
486 });
487 compilation.hooks.runtimeRequirementInTree
488 .for(RuntimeGlobals.getTrustedTypesPolicy)
489 .tap(PLUGIN_NAME, (chunk, set) => {
490 compilation.addRuntimeModule(
491 chunk,
492 new GetTrustedTypesPolicyRuntimeModule(set)
493 );
494 return true;
495 });
496 compilation.hooks.runtimeRequirementInTree
497 .for(RuntimeGlobals.relativeUrl)
498 .tap(PLUGIN_NAME, (chunk, _set) => {
499 compilation.addRuntimeModule(chunk, new RelativeUrlRuntimeModule());
500 return true;
501 });
502 compilation.hooks.runtimeRequirementInTree
503 .for(RuntimeGlobals.onChunksLoaded)
504 .tap(PLUGIN_NAME, (chunk, _set) => {
505 compilation.addRuntimeModule(
506 chunk,
507 new OnChunksLoadedRuntimeModule()
508 );
509 return true;
510 });
511 compilation.hooks.runtimeRequirementInTree
512 .for(RuntimeGlobals.baseURI)
513 .tap(PLUGIN_NAME, (chunk) => {
514 if (isChunkLoadingDisabledForChunk(chunk)) {
515 compilation.addRuntimeModule(chunk, new BaseUriRuntimeModule());
516 return true;
517 }
518 });
519 compilation.hooks.runtimeRequirementInTree
520 .for(RuntimeGlobals.scriptNonce)
521 .tap(PLUGIN_NAME, (chunk) => {
522 compilation.addRuntimeModule(chunk, new NonceRuntimeModule());
523 return true;
524 });
525 compilation.hooks.runtimeRequirementInTree
526 .for(RuntimeGlobals.toBinary)
527 .tap(PLUGIN_NAME, (chunk) => {
528 compilation.addRuntimeModule(chunk, new ToBinaryRuntimeModule());
529 return true;
530 });
531 // TODO webpack 6: remove CompatRuntimeModule
532 compilation.hooks.additionalTreeRuntimeRequirements.tap(
533 PLUGIN_NAME,
534 (chunk, _set) => {
535 const { mainTemplate } = compilation;
536 if (
537 mainTemplate.hooks.bootstrap.isUsed() ||
538 mainTemplate.hooks.localVars.isUsed() ||
539 mainTemplate.hooks.requireEnsure.isUsed() ||
540 mainTemplate.hooks.requireExtensions.isUsed()
541 ) {
542 compilation.addRuntimeModule(chunk, new CompatRuntimeModule());
543 }
544 }
545 );
546 JavascriptModulesPlugin.getCompilationHooks(compilation).chunkHash.tap(
547 PLUGIN_NAME,
548 (chunk, hash, { chunkGraph }) => {
549 const xor = new StringXor();
550 for (const m of chunkGraph.getChunkRuntimeModulesIterable(chunk)) {
551 xor.add(chunkGraph.getModuleHash(m, chunk.runtime));
552 }
553 xor.updateHash(hash);
554 }
555 );
556 });
557 }
558}
559
560module.exports = RuntimePlugin;
Note: See TracBrowser for help on using the repository browser.