source: imaps-frontend/node_modules/webpack/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 8.4 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3*/
4
5"use strict";
6
7const RuntimeGlobals = require("../RuntimeGlobals");
8const RuntimeModule = require("../RuntimeModule");
9const Template = require("../Template");
10const {
11 getChunkFilenameTemplate,
12 chunkHasJs
13} = require("../javascript/JavascriptModulesPlugin");
14const { getInitialChunkIds } = require("../javascript/StartupHelpers");
15const compileBooleanMatcher = require("../util/compileBooleanMatcher");
16const { getUndoPath } = require("../util/identifier");
17
18/** @typedef {import("../Chunk")} Chunk */
19/** @typedef {import("../ChunkGraph")} ChunkGraph */
20/** @typedef {import("../Compilation")} Compilation */
21/** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
22
23class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
24 /**
25 * @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements
26 * @param {boolean} withCreateScriptUrl with createScriptUrl support
27 */
28 constructor(runtimeRequirements, withCreateScriptUrl) {
29 super("importScripts chunk loading", RuntimeModule.STAGE_ATTACH);
30 this.runtimeRequirements = runtimeRequirements;
31 this._withCreateScriptUrl = withCreateScriptUrl;
32 }
33
34 /**
35 * @private
36 * @param {Chunk} chunk chunk
37 * @returns {string} generated code
38 */
39 _generateBaseUri(chunk) {
40 const options = chunk.getEntryOptions();
41 if (options && options.baseUri) {
42 return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`;
43 }
44 const compilation = /** @type {Compilation} */ (this.compilation);
45 const outputName = compilation.getPath(
46 getChunkFilenameTemplate(chunk, compilation.outputOptions),
47 {
48 chunk,
49 contentHashType: "javascript"
50 }
51 );
52 const rootOutputDir = getUndoPath(
53 outputName,
54 /** @type {string} */ (compilation.outputOptions.path),
55 false
56 );
57 return `${RuntimeGlobals.baseURI} = self.location + ${JSON.stringify(
58 rootOutputDir ? `/../${rootOutputDir}` : ""
59 )};`;
60 }
61
62 /**
63 * @returns {string | null} runtime code
64 */
65 generate() {
66 const compilation = /** @type {Compilation} */ (this.compilation);
67 const fn = RuntimeGlobals.ensureChunkHandlers;
68 const withBaseURI = this.runtimeRequirements.has(RuntimeGlobals.baseURI);
69 const withLoading = this.runtimeRequirements.has(
70 RuntimeGlobals.ensureChunkHandlers
71 );
72 const withHmr = this.runtimeRequirements.has(
73 RuntimeGlobals.hmrDownloadUpdateHandlers
74 );
75 const withHmrManifest = this.runtimeRequirements.has(
76 RuntimeGlobals.hmrDownloadManifest
77 );
78 const globalObject = compilation.runtimeTemplate.globalObject;
79 const chunkLoadingGlobalExpr = `${globalObject}[${JSON.stringify(
80 compilation.outputOptions.chunkLoadingGlobal
81 )}]`;
82 const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
83 const chunk = /** @type {Chunk} */ (this.chunk);
84 const hasJsMatcher = compileBooleanMatcher(
85 chunkGraph.getChunkConditionMap(chunk, chunkHasJs)
86 );
87 const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
88
89 const stateExpression = withHmr
90 ? `${RuntimeGlobals.hmrRuntimeStatePrefix}_importScripts`
91 : undefined;
92 const runtimeTemplate = compilation.runtimeTemplate;
93 const { _withCreateScriptUrl: withCreateScriptUrl } = this;
94
95 return Template.asString([
96 withBaseURI ? this._generateBaseUri(chunk) : "// no baseURI",
97 "",
98 "// object to store loaded chunks",
99 '// "1" means "already loaded"',
100 `var installedChunks = ${
101 stateExpression ? `${stateExpression} = ${stateExpression} || ` : ""
102 }{`,
103 Template.indent(
104 Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 1`).join(
105 ",\n"
106 )
107 ),
108 "};",
109 "",
110 withLoading
111 ? Template.asString([
112 "// importScripts chunk loading",
113 `var installChunk = ${runtimeTemplate.basicFunction("data", [
114 runtimeTemplate.destructureArray(
115 ["chunkIds", "moreModules", "runtime"],
116 "data"
117 ),
118 "for(var moduleId in moreModules) {",
119 Template.indent([
120 `if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
121 Template.indent(
122 `${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];`
123 ),
124 "}"
125 ]),
126 "}",
127 `if(runtime) runtime(${RuntimeGlobals.require});`,
128 "while(chunkIds.length)",
129 Template.indent("installedChunks[chunkIds.pop()] = 1;"),
130 "parentChunkLoadingFunction(data);"
131 ])};`
132 ])
133 : "// no chunk install function needed",
134 withLoading
135 ? Template.asString([
136 `${fn}.i = ${runtimeTemplate.basicFunction(
137 "chunkId, promises",
138 hasJsMatcher !== false
139 ? [
140 '// "1" is the signal for "already loaded"',
141 "if(!installedChunks[chunkId]) {",
142 Template.indent([
143 hasJsMatcher === true
144 ? "if(true) { // all chunks have JS"
145 : `if(${hasJsMatcher("chunkId")}) {`,
146 Template.indent(
147 `importScripts(${
148 withCreateScriptUrl
149 ? `${RuntimeGlobals.createScriptUrl}(${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId))`
150 : `${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId)`
151 });`
152 ),
153 "}"
154 ]),
155 "}"
156 ]
157 : "installedChunks[chunkId] = 1;"
158 )};`,
159 "",
160 `var chunkLoadingGlobal = ${chunkLoadingGlobalExpr} = ${chunkLoadingGlobalExpr} || [];`,
161 "var parentChunkLoadingFunction = chunkLoadingGlobal.push.bind(chunkLoadingGlobal);",
162 "chunkLoadingGlobal.push = installChunk;"
163 ])
164 : "// no chunk loading",
165 "",
166 withHmr
167 ? Template.asString([
168 "function loadUpdateChunk(chunkId, updatedModulesList) {",
169 Template.indent([
170 "var success = false;",
171 `${globalObject}[${JSON.stringify(
172 compilation.outputOptions.hotUpdateGlobal
173 )}] = ${runtimeTemplate.basicFunction("_, moreModules, runtime", [
174 "for(var moduleId in moreModules) {",
175 Template.indent([
176 `if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
177 Template.indent([
178 "currentUpdate[moduleId] = moreModules[moduleId];",
179 "if(updatedModulesList) updatedModulesList.push(moduleId);"
180 ]),
181 "}"
182 ]),
183 "}",
184 "if(runtime) currentUpdateRuntime.push(runtime);",
185 "success = true;"
186 ])};`,
187 "// start update chunk loading",
188 `importScripts(${
189 withCreateScriptUrl
190 ? `${RuntimeGlobals.createScriptUrl}(${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkUpdateScriptFilename}(chunkId))`
191 : `${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkUpdateScriptFilename}(chunkId)`
192 });`,
193 'if(!success) throw new Error("Loading update chunk failed for unknown reason");'
194 ]),
195 "}",
196 "",
197 Template.getFunctionContent(
198 require("../hmr/JavascriptHotModuleReplacement.runtime.js")
199 )
200 .replace(/\$key\$/g, "importScripts")
201 .replace(/\$installedChunks\$/g, "installedChunks")
202 .replace(/\$loadUpdateChunk\$/g, "loadUpdateChunk")
203 .replace(/\$moduleCache\$/g, RuntimeGlobals.moduleCache)
204 .replace(/\$moduleFactories\$/g, RuntimeGlobals.moduleFactories)
205 .replace(
206 /\$ensureChunkHandlers\$/g,
207 RuntimeGlobals.ensureChunkHandlers
208 )
209 .replace(/\$hasOwnProperty\$/g, RuntimeGlobals.hasOwnProperty)
210 .replace(/\$hmrModuleData\$/g, RuntimeGlobals.hmrModuleData)
211 .replace(
212 /\$hmrDownloadUpdateHandlers\$/g,
213 RuntimeGlobals.hmrDownloadUpdateHandlers
214 )
215 .replace(
216 /\$hmrInvalidateModuleHandlers\$/g,
217 RuntimeGlobals.hmrInvalidateModuleHandlers
218 )
219 ])
220 : "// no HMR",
221 "",
222 withHmrManifest
223 ? Template.asString([
224 `${
225 RuntimeGlobals.hmrDownloadManifest
226 } = ${runtimeTemplate.basicFunction("", [
227 'if (typeof fetch === "undefined") throw new Error("No browser support: need fetch API");',
228 `return fetch(${RuntimeGlobals.publicPath} + ${
229 RuntimeGlobals.getUpdateManifestFilename
230 }()).then(${runtimeTemplate.basicFunction("response", [
231 "if(response.status === 404) return; // no update available",
232 'if(!response.ok) throw new Error("Failed to fetch update manifest " + response.statusText);',
233 "return response.json();"
234 ])});`
235 ])};`
236 ])
237 : "// no HMR manifest"
238 ]);
239 }
240}
241
242module.exports = ImportScriptsChunkLoadingRuntimeModule;
Note: See TracBrowser for help on using the repository browser.