source: trip-planner-front/node_modules/webpack/lib/node/ReadFileChunkLoadingRuntimeModule.js

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

initial commit

  • Property mode set to 100644
File size: 9.0 KB
RevLine 
[6a3a178]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 chunkHasJs,
12 getChunkFilenameTemplate
13} = require("../javascript/JavascriptModulesPlugin");
14const { getInitialChunkIds } = require("../javascript/StartupHelpers");
15const compileBooleanMatcher = require("../util/compileBooleanMatcher");
16const { getUndoPath } = require("../util/identifier");
17
18class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
19 constructor(runtimeRequirements) {
20 super("readFile chunk loading", RuntimeModule.STAGE_ATTACH);
21 this.runtimeRequirements = runtimeRequirements;
22 }
23
24 /**
25 * @returns {string} runtime code
26 */
27 generate() {
28 const { chunkGraph, chunk } = this;
29 const { runtimeTemplate } = this.compilation;
30 const fn = RuntimeGlobals.ensureChunkHandlers;
31 const withBaseURI = this.runtimeRequirements.has(RuntimeGlobals.baseURI);
32 const withExternalInstallChunk = this.runtimeRequirements.has(
33 RuntimeGlobals.externalInstallChunk
34 );
35 const withOnChunkLoad = this.runtimeRequirements.has(
36 RuntimeGlobals.onChunksLoaded
37 );
38 const withLoading = this.runtimeRequirements.has(
39 RuntimeGlobals.ensureChunkHandlers
40 );
41 const withHmr = this.runtimeRequirements.has(
42 RuntimeGlobals.hmrDownloadUpdateHandlers
43 );
44 const withHmrManifest = this.runtimeRequirements.has(
45 RuntimeGlobals.hmrDownloadManifest
46 );
47 const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
48 const hasJsMatcher = compileBooleanMatcher(conditionMap);
49 const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);
50
51 const outputName = this.compilation.getPath(
52 getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
53 {
54 chunk,
55 contentHashType: "javascript"
56 }
57 );
58 const rootOutputDir = getUndoPath(
59 outputName,
60 this.compilation.outputOptions.path,
61 false
62 );
63
64 return Template.asString([
65 withBaseURI
66 ? Template.asString([
67 `${RuntimeGlobals.baseURI} = require("url").pathToFileURL(${
68 rootOutputDir
69 ? `__dirname + ${JSON.stringify("/" + rootOutputDir)}`
70 : "__filename"
71 });`
72 ])
73 : "// no baseURI",
74 "",
75 "// object to store loaded chunks",
76 '// "0" means "already loaded", Promise means loading',
77 "var installedChunks = {",
78 Template.indent(
79 Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 0`).join(
80 ",\n"
81 )
82 ),
83 "};",
84 "",
85 withOnChunkLoad
86 ? `${
87 RuntimeGlobals.onChunksLoaded
88 }.readFileVm = ${runtimeTemplate.returningFunction(
89 "installedChunks[chunkId] === 0",
90 "chunkId"
91 )};`
92 : "// no on chunks loaded",
93 "",
94 withLoading || withExternalInstallChunk
95 ? `var installChunk = ${runtimeTemplate.basicFunction("chunk", [
96 "var moreModules = chunk.modules, chunkIds = chunk.ids, runtime = chunk.runtime;",
97 "for(var moduleId in moreModules) {",
98 Template.indent([
99 `if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
100 Template.indent([
101 `${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];`
102 ]),
103 "}"
104 ]),
105 "}",
106 `if(runtime) runtime(__webpack_require__);`,
107 "for(var i = 0; i < chunkIds.length; i++) {",
108 Template.indent([
109 "if(installedChunks[chunkIds[i]]) {",
110 Template.indent(["installedChunks[chunkIds[i]][0]();"]),
111 "}",
112 "installedChunks[chunkIds[i]] = 0;"
113 ]),
114 "}",
115 withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : ""
116 ])};`
117 : "// no chunk install function needed",
118 "",
119 withLoading
120 ? Template.asString([
121 "// ReadFile + VM.run chunk loading for javascript",
122 `${fn}.readFileVm = function(chunkId, promises) {`,
123 hasJsMatcher !== false
124 ? Template.indent([
125 "",
126 "var installedChunkData = installedChunks[chunkId];",
127 'if(installedChunkData !== 0) { // 0 means "already installed".',
128 Template.indent([
129 '// array of [resolve, reject, promise] means "currently loading"',
130 "if(installedChunkData) {",
131 Template.indent(["promises.push(installedChunkData[2]);"]),
132 "} else {",
133 Template.indent([
134 hasJsMatcher === true
135 ? "if(true) { // all chunks have JS"
136 : `if(${hasJsMatcher("chunkId")}) {`,
137 Template.indent([
138 "// load the chunk and return promise to it",
139 "var promise = new Promise(function(resolve, reject) {",
140 Template.indent([
141 "installedChunkData = installedChunks[chunkId] = [resolve, reject];",
142 `var filename = require('path').join(__dirname, ${JSON.stringify(
143 rootOutputDir
144 )} + ${
145 RuntimeGlobals.getChunkScriptFilename
146 }(chunkId));`,
147 "require('fs').readFile(filename, 'utf-8', function(err, content) {",
148 Template.indent([
149 "if(err) return reject(err);",
150 "var chunk = {};",
151 "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
152 "(chunk, require, require('path').dirname(filename), filename);",
153 "installChunk(chunk);"
154 ]),
155 "});"
156 ]),
157 "});",
158 "promises.push(installedChunkData[2] = promise);"
159 ]),
160 "} else installedChunks[chunkId] = 0;"
161 ]),
162 "}"
163 ]),
164 "}"
165 ])
166 : Template.indent(["installedChunks[chunkId] = 0;"]),
167 "};"
168 ])
169 : "// no chunk loading",
170 "",
171 withExternalInstallChunk
172 ? Template.asString([
173 "module.exports = __webpack_require__;",
174 `${RuntimeGlobals.externalInstallChunk} = installChunk;`
175 ])
176 : "// no external install chunk",
177 "",
178 withHmr
179 ? Template.asString([
180 "function loadUpdateChunk(chunkId, updatedModulesList) {",
181 Template.indent([
182 "return new Promise(function(resolve, reject) {",
183 Template.indent([
184 `var filename = require('path').join(__dirname, ${JSON.stringify(
185 rootOutputDir
186 )} + ${RuntimeGlobals.getChunkUpdateScriptFilename}(chunkId));`,
187 "require('fs').readFile(filename, 'utf-8', function(err, content) {",
188 Template.indent([
189 "if(err) return reject(err);",
190 "var update = {};",
191 "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
192 "(update, require, require('path').dirname(filename), filename);",
193 "var updatedModules = update.modules;",
194 "var runtime = update.runtime;",
195 "for(var moduleId in updatedModules) {",
196 Template.indent([
197 `if(${RuntimeGlobals.hasOwnProperty}(updatedModules, moduleId)) {`,
198 Template.indent([
199 `currentUpdate[moduleId] = updatedModules[moduleId];`,
200 "if(updatedModulesList) updatedModulesList.push(moduleId);"
201 ]),
202 "}"
203 ]),
204 "}",
205 "if(runtime) currentUpdateRuntime.push(runtime);",
206 "resolve();"
207 ]),
208 "});"
209 ]),
210 "});"
211 ]),
212 "}",
213 "",
214 Template.getFunctionContent(
215 require("../hmr/JavascriptHotModuleReplacement.runtime.js")
216 )
217 .replace(/\$key\$/g, "readFileVm")
218 .replace(/\$installedChunks\$/g, "installedChunks")
219 .replace(/\$loadUpdateChunk\$/g, "loadUpdateChunk")
220 .replace(/\$moduleCache\$/g, RuntimeGlobals.moduleCache)
221 .replace(/\$moduleFactories\$/g, RuntimeGlobals.moduleFactories)
222 .replace(
223 /\$ensureChunkHandlers\$/g,
224 RuntimeGlobals.ensureChunkHandlers
225 )
226 .replace(/\$hasOwnProperty\$/g, RuntimeGlobals.hasOwnProperty)
227 .replace(/\$hmrModuleData\$/g, RuntimeGlobals.hmrModuleData)
228 .replace(
229 /\$hmrDownloadUpdateHandlers\$/g,
230 RuntimeGlobals.hmrDownloadUpdateHandlers
231 )
232 .replace(
233 /\$hmrInvalidateModuleHandlers\$/g,
234 RuntimeGlobals.hmrInvalidateModuleHandlers
235 )
236 ])
237 : "// no HMR",
238 "",
239 withHmrManifest
240 ? Template.asString([
241 `${RuntimeGlobals.hmrDownloadManifest} = function() {`,
242 Template.indent([
243 "return new Promise(function(resolve, reject) {",
244 Template.indent([
245 `var filename = require('path').join(__dirname, ${JSON.stringify(
246 rootOutputDir
247 )} + ${RuntimeGlobals.getUpdateManifestFilename}());`,
248 "require('fs').readFile(filename, 'utf-8', function(err, content) {",
249 Template.indent([
250 "if(err) {",
251 Template.indent([
252 'if(err.code === "ENOENT") return resolve();',
253 "return reject(err);"
254 ]),
255 "}",
256 "try { resolve(JSON.parse(content)); }",
257 "catch(e) { reject(e); }"
258 ]),
259 "});"
260 ]),
261 "});"
262 ]),
263 "}"
264 ])
265 : "// no HMR manifest"
266 ]);
267 }
268}
269
270module.exports = ReadFileChunkLoadingRuntimeModule;
Note: See TracBrowser for help on using the repository browser.