source: imaps-frontend/node_modules/webpack/lib/sharing/ShareRuntimeModule.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.9 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 RuntimeModule = require("../RuntimeModule");
10const Template = require("../Template");
11const {
12 compareModulesByIdentifier,
13 compareStrings
14} = require("../util/comparators");
15
16/** @typedef {import("../Chunk")} Chunk */
17/** @typedef {import("../ChunkGraph")} ChunkGraph */
18/** @typedef {import("../Compilation")} Compilation */
19
20class ShareRuntimeModule extends RuntimeModule {
21 constructor() {
22 super("sharing");
23 }
24
25 /**
26 * @returns {string | null} runtime code
27 */
28 generate() {
29 const compilation = /** @type {Compilation} */ (this.compilation);
30 const {
31 runtimeTemplate,
32 codeGenerationResults,
33 outputOptions: { uniqueName, ignoreBrowserWarnings }
34 } = compilation;
35 const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
36 /** @type {Map<string, Map<number, Set<string>>>} */
37 const initCodePerScope = new Map();
38 for (const chunk of /** @type {Chunk} */ (
39 this.chunk
40 ).getAllReferencedChunks()) {
41 const modules = chunkGraph.getOrderedChunkModulesIterableBySourceType(
42 chunk,
43 "share-init",
44 compareModulesByIdentifier
45 );
46 if (!modules) continue;
47 for (const m of modules) {
48 const data = codeGenerationResults.getData(
49 m,
50 chunk.runtime,
51 "share-init"
52 );
53 if (!data) continue;
54 for (const item of data) {
55 const { shareScope, initStage, init } = item;
56 let stages = initCodePerScope.get(shareScope);
57 if (stages === undefined) {
58 initCodePerScope.set(shareScope, (stages = new Map()));
59 }
60 let list = stages.get(initStage || 0);
61 if (list === undefined) {
62 stages.set(initStage || 0, (list = new Set()));
63 }
64 list.add(init);
65 }
66 }
67 }
68 return Template.asString([
69 `${RuntimeGlobals.shareScopeMap} = {};`,
70 "var initPromises = {};",
71 "var initTokens = {};",
72 `${RuntimeGlobals.initializeSharing} = ${runtimeTemplate.basicFunction(
73 "name, initScope",
74 [
75 "if(!initScope) initScope = [];",
76 "// handling circular init calls",
77 "var initToken = initTokens[name];",
78 "if(!initToken) initToken = initTokens[name] = {};",
79 "if(initScope.indexOf(initToken) >= 0) return;",
80 "initScope.push(initToken);",
81 "// only runs once",
82 "if(initPromises[name]) return initPromises[name];",
83 "// creates a new share scope if needed",
84 `if(!${RuntimeGlobals.hasOwnProperty}(${RuntimeGlobals.shareScopeMap}, name)) ${RuntimeGlobals.shareScopeMap}[name] = {};`,
85 "// runs all init snippets from all modules reachable",
86 `var scope = ${RuntimeGlobals.shareScopeMap}[name];`,
87 `var warn = ${
88 ignoreBrowserWarnings
89 ? runtimeTemplate.basicFunction("", "")
90 : runtimeTemplate.basicFunction("msg", [
91 'if (typeof console !== "undefined" && console.warn) console.warn(msg);'
92 ])
93 };`,
94 `var uniqueName = ${JSON.stringify(uniqueName || undefined)};`,
95 `var register = ${runtimeTemplate.basicFunction(
96 "name, version, factory, eager",
97 [
98 "var versions = scope[name] = scope[name] || {};",
99 "var activeVersion = versions[version];",
100 "if(!activeVersion || (!activeVersion.loaded && (!eager != !activeVersion.eager ? eager : uniqueName > activeVersion.from))) versions[version] = { get: factory, from: uniqueName, eager: !!eager };"
101 ]
102 )};`,
103 `var initExternal = ${runtimeTemplate.basicFunction("id", [
104 `var handleError = ${runtimeTemplate.expressionFunction(
105 'warn("Initialization of sharing external failed: " + err)',
106 "err"
107 )};`,
108 "try {",
109 Template.indent([
110 `var module = ${RuntimeGlobals.require}(id);`,
111 "if(!module) return;",
112 `var initFn = ${runtimeTemplate.returningFunction(
113 `module && module.init && module.init(${RuntimeGlobals.shareScopeMap}[name], initScope)`,
114 "module"
115 )}`,
116 "if(module.then) return promises.push(module.then(initFn, handleError));",
117 "var initResult = initFn(module);",
118 "if(initResult && initResult.then) return promises.push(initResult['catch'](handleError));"
119 ]),
120 "} catch(err) { handleError(err); }"
121 ])}`,
122 "var promises = [];",
123 "switch(name) {",
124 ...Array.from(initCodePerScope)
125 .sort(([a], [b]) => compareStrings(a, b))
126 .map(([name, stages]) =>
127 Template.indent([
128 `case ${JSON.stringify(name)}: {`,
129 Template.indent(
130 Array.from(stages)
131 .sort(([a], [b]) => a - b)
132 .map(([, initCode]) =>
133 Template.asString(Array.from(initCode))
134 )
135 ),
136 "}",
137 "break;"
138 ])
139 ),
140 "}",
141 "if(!promises.length) return initPromises[name] = 1;",
142 `return initPromises[name] = Promise.all(promises).then(${runtimeTemplate.returningFunction(
143 "initPromises[name] = 1"
144 )});`
145 ]
146 )};`
147 ]);
148 }
149}
150
151module.exports = ShareRuntimeModule;
Note: See TracBrowser for help on using the repository browser.