source: trip-planner-front/node_modules/webpack/lib/esm/ModuleChunkLoadingRuntimeModule.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3*/
4
5"use strict";
6
7const { SyncWaterfallHook } = require("tapable");
8const Compilation = require("../Compilation");
9const RuntimeGlobals = require("../RuntimeGlobals");
10const RuntimeModule = require("../RuntimeModule");
11const Template = require("../Template");
12const {
13 getChunkFilenameTemplate,
14 chunkHasJs
15} = require("../javascript/JavascriptModulesPlugin");
16const { getInitialChunkIds } = require("../javascript/StartupHelpers");
17const compileBooleanMatcher = require("../util/compileBooleanMatcher");
18const { getUndoPath } = require("../util/identifier");
19
20/** @typedef {import("../Chunk")} Chunk */
21
22/**
23 * @typedef {Object} JsonpCompilationPluginHooks
24 * @property {SyncWaterfallHook<[string, Chunk]>} linkPreload
25 * @property {SyncWaterfallHook<[string, Chunk]>} linkPrefetch
26 */
27
28/** @type {WeakMap<Compilation, JsonpCompilationPluginHooks>} */
29const compilationHooksMap = new WeakMap();
30
31class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
32 /**
33 * @param {Compilation} compilation the compilation
34 * @returns {JsonpCompilationPluginHooks} hooks
35 */
36 static getCompilationHooks(compilation) {
37 if (!(compilation instanceof Compilation)) {
38 throw new TypeError(
39 "The 'compilation' argument must be an instance of Compilation"
40 );
41 }
42 let hooks = compilationHooksMap.get(compilation);
43 if (hooks === undefined) {
44 hooks = {
45 linkPreload: new SyncWaterfallHook(["source", "chunk"]),
46 linkPrefetch: new SyncWaterfallHook(["source", "chunk"])
47 };
48 compilationHooksMap.set(compilation, hooks);
49 }
50 return hooks;
51 }
52
53 constructor(runtimeRequirements) {
54 super("import chunk loading", RuntimeModule.STAGE_ATTACH);
55 this._runtimeRequirements = runtimeRequirements;
56 }
57
58 /**
59 * @returns {string} runtime code
60 */
61 generate() {
62 const { compilation, chunk } = this;
63 const {
64 runtimeTemplate,
65 chunkGraph,
66 outputOptions: { importFunctionName, importMetaName }
67 } = compilation;
68 const fn = RuntimeGlobals.ensureChunkHandlers;
69 const withBaseURI = this._runtimeRequirements.has(RuntimeGlobals.baseURI);
70 const withExternalInstallChunk = this._runtimeRequirements.has(
71 RuntimeGlobals.externalInstallChunk
72 );
73 const withLoading = this._runtimeRequirements.has(
74 RuntimeGlobals.ensureChunkHandlers
75 );
76 const withOnChunkLoad = this._runtimeRequirements.has(
77 RuntimeGlobals.onChunksLoaded
78 );
79 const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
80 const hasJsMatcher = compileBooleanMatcher(conditionMap);
81 const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);
82
83 const outputName = this.compilation.getPath(
84 getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
85 {
86 chunk,
87 contentHashType: "javascript"
88 }
89 );
90 const rootOutputDir = getUndoPath(
91 outputName,
92 this.compilation.outputOptions.path,
93 true
94 );
95
96 return Template.asString([
97 withBaseURI
98 ? Template.asString([
99 `${RuntimeGlobals.baseURI} = new URL(${JSON.stringify(
100 rootOutputDir
101 )}, ${importMetaName}.url);`
102 ])
103 : "// no baseURI",
104 "",
105 "// object to store loaded and loading chunks",
106 "// undefined = chunk not loaded, null = chunk preloaded/prefetched",
107 "// [resolve, reject, Promise] = chunk loading, 0 = chunk loaded",
108 "var installedChunks = {",
109 Template.indent(
110 Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 0`).join(
111 ",\n"
112 )
113 ),
114 "};",
115 "",
116 withLoading || withExternalInstallChunk
117 ? `var installChunk = ${runtimeTemplate.basicFunction("data", [
118 runtimeTemplate.destructureObject(
119 ["ids", "modules", "runtime"],
120 "data"
121 ),
122 '// add "modules" to the modules object,',
123 '// then flag all "ids" as loaded and fire callback',
124 "var moduleId, chunkId, i = 0;",
125 "for(moduleId in modules) {",
126 Template.indent([
127 `if(${RuntimeGlobals.hasOwnProperty}(modules, moduleId)) {`,
128 Template.indent(
129 `${RuntimeGlobals.moduleFactories}[moduleId] = modules[moduleId];`
130 ),
131 "}"
132 ]),
133 "}",
134 "if(runtime) runtime(__webpack_require__);",
135 "for(;i < ids.length; i++) {",
136 Template.indent([
137 "chunkId = ids[i];",
138 `if(${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) && installedChunks[chunkId]) {`,
139 Template.indent("installedChunks[chunkId][0]();"),
140 "}",
141 "installedChunks[ids[i]] = 0;"
142 ]),
143 "}",
144 withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : ""
145 ])}`
146 : "// no install chunk",
147 "",
148 withLoading
149 ? Template.asString([
150 `${fn}.j = ${runtimeTemplate.basicFunction(
151 "chunkId, promises",
152 hasJsMatcher !== false
153 ? Template.indent([
154 "// import() chunk loading for javascript",
155 `var installedChunkData = ${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) ? installedChunks[chunkId] : undefined;`,
156 'if(installedChunkData !== 0) { // 0 means "already installed".',
157 Template.indent([
158 "",
159 '// a Promise means "currently loading".',
160 "if(installedChunkData) {",
161 Template.indent([
162 "promises.push(installedChunkData[1]);"
163 ]),
164 "} else {",
165 Template.indent([
166 hasJsMatcher === true
167 ? "if(true) { // all chunks have JS"
168 : `if(${hasJsMatcher("chunkId")}) {`,
169 Template.indent([
170 "// setup Promise in chunk cache",
171 `var promise = ${importFunctionName}(${JSON.stringify(
172 rootOutputDir
173 )} + ${
174 RuntimeGlobals.getChunkScriptFilename
175 }(chunkId)).then(installChunk, ${runtimeTemplate.basicFunction(
176 "e",
177 [
178 "if(installedChunks[chunkId] !== 0) installedChunks[chunkId] = undefined;",
179 "throw e;"
180 ]
181 )});`,
182 `var promise = Promise.race([promise, new Promise(${runtimeTemplate.expressionFunction(
183 `installedChunkData = installedChunks[chunkId] = [resolve]`,
184 "resolve"
185 )})])`,
186 `promises.push(installedChunkData[1] = promise);`
187 ]),
188 "} else installedChunks[chunkId] = 0;"
189 ]),
190 "}"
191 ]),
192 "}"
193 ])
194 : Template.indent(["installedChunks[chunkId] = 0;"])
195 )};`
196 ])
197 : "// no chunk on demand loading",
198 "",
199 withExternalInstallChunk
200 ? Template.asString([
201 `${RuntimeGlobals.externalInstallChunk} = installChunk;`
202 ])
203 : "// no external install chunk",
204 "",
205 withOnChunkLoad
206 ? `${
207 RuntimeGlobals.onChunksLoaded
208 }.j = ${runtimeTemplate.returningFunction(
209 "installedChunks[chunkId] === 0",
210 "chunkId"
211 )};`
212 : "// no on chunks loaded"
213 ]);
214 }
215}
216
217module.exports = ModuleChunkLoadingRuntimeModule;
Note: See TracBrowser for help on using the repository browser.