1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | */
|
---|
4 |
|
---|
5 | "use strict";
|
---|
6 |
|
---|
7 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
8 | const RuntimeModule = require("../RuntimeModule");
|
---|
9 | const Template = require("../Template");
|
---|
10 | const {
|
---|
11 | chunkHasJs,
|
---|
12 | getChunkFilenameTemplate
|
---|
13 | } = require("../javascript/JavascriptModulesPlugin");
|
---|
14 | const { getInitialChunkIds } = require("../javascript/StartupHelpers");
|
---|
15 | const compileBooleanMatcher = require("../util/compileBooleanMatcher");
|
---|
16 | const { getUndoPath } = require("../util/identifier");
|
---|
17 |
|
---|
18 | class RequireChunkLoadingRuntimeModule extends RuntimeModule {
|
---|
19 | constructor(runtimeRequirements) {
|
---|
20 | super("require 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 | true
|
---|
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 | '// "1" means "loaded", otherwise not loaded yet',
|
---|
77 | "var installedChunks = {",
|
---|
78 | Template.indent(
|
---|
79 | Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 1`).join(
|
---|
80 | ",\n"
|
---|
81 | )
|
---|
82 | ),
|
---|
83 | "};",
|
---|
84 | "",
|
---|
85 | withOnChunkLoad
|
---|
86 | ? `${
|
---|
87 | RuntimeGlobals.onChunksLoaded
|
---|
88 | }.require = ${runtimeTemplate.returningFunction(
|
---|
89 | "installedChunks[chunkId]",
|
---|
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("installedChunks[chunkIds[i]] = 1;"),
|
---|
109 | withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : ""
|
---|
110 | ])};`
|
---|
111 | : "// no chunk install function needed",
|
---|
112 | "",
|
---|
113 | withLoading
|
---|
114 | ? Template.asString([
|
---|
115 | "// require() chunk loading for javascript",
|
---|
116 | `${fn}.require = ${runtimeTemplate.basicFunction(
|
---|
117 | "chunkId, promises",
|
---|
118 | hasJsMatcher !== false
|
---|
119 | ? [
|
---|
120 | '// "1" is the signal for "already loaded"',
|
---|
121 | "if(!installedChunks[chunkId]) {",
|
---|
122 | Template.indent([
|
---|
123 | hasJsMatcher === true
|
---|
124 | ? "if(true) { // all chunks have JS"
|
---|
125 | : `if(${hasJsMatcher("chunkId")}) {`,
|
---|
126 | Template.indent([
|
---|
127 | `installChunk(require(${JSON.stringify(
|
---|
128 | rootOutputDir
|
---|
129 | )} + ${
|
---|
130 | RuntimeGlobals.getChunkScriptFilename
|
---|
131 | }(chunkId)));`
|
---|
132 | ]),
|
---|
133 | "} else installedChunks[chunkId] = 1;",
|
---|
134 | ""
|
---|
135 | ]),
|
---|
136 | "}"
|
---|
137 | ]
|
---|
138 | : "installedChunks[chunkId] = 1;"
|
---|
139 | )};`
|
---|
140 | ])
|
---|
141 | : "// no chunk loading",
|
---|
142 | "",
|
---|
143 | withExternalInstallChunk
|
---|
144 | ? Template.asString([
|
---|
145 | "module.exports = __webpack_require__;",
|
---|
146 | `${RuntimeGlobals.externalInstallChunk} = installChunk;`
|
---|
147 | ])
|
---|
148 | : "// no external install chunk",
|
---|
149 | "",
|
---|
150 | withHmr
|
---|
151 | ? Template.asString([
|
---|
152 | "function loadUpdateChunk(chunkId, updatedModulesList) {",
|
---|
153 | Template.indent([
|
---|
154 | `var update = require(${JSON.stringify(rootOutputDir)} + ${
|
---|
155 | RuntimeGlobals.getChunkUpdateScriptFilename
|
---|
156 | }(chunkId));`,
|
---|
157 | "var updatedModules = update.modules;",
|
---|
158 | "var runtime = update.runtime;",
|
---|
159 | "for(var moduleId in updatedModules) {",
|
---|
160 | Template.indent([
|
---|
161 | `if(${RuntimeGlobals.hasOwnProperty}(updatedModules, moduleId)) {`,
|
---|
162 | Template.indent([
|
---|
163 | `currentUpdate[moduleId] = updatedModules[moduleId];`,
|
---|
164 | "if(updatedModulesList) updatedModulesList.push(moduleId);"
|
---|
165 | ]),
|
---|
166 | "}"
|
---|
167 | ]),
|
---|
168 | "}",
|
---|
169 | "if(runtime) currentUpdateRuntime.push(runtime);"
|
---|
170 | ]),
|
---|
171 | "}",
|
---|
172 | "",
|
---|
173 | Template.getFunctionContent(
|
---|
174 | require("../hmr/JavascriptHotModuleReplacement.runtime.js")
|
---|
175 | )
|
---|
176 | .replace(/\$key\$/g, "require")
|
---|
177 | .replace(/\$installedChunks\$/g, "installedChunks")
|
---|
178 | .replace(/\$loadUpdateChunk\$/g, "loadUpdateChunk")
|
---|
179 | .replace(/\$moduleCache\$/g, RuntimeGlobals.moduleCache)
|
---|
180 | .replace(/\$moduleFactories\$/g, RuntimeGlobals.moduleFactories)
|
---|
181 | .replace(
|
---|
182 | /\$ensureChunkHandlers\$/g,
|
---|
183 | RuntimeGlobals.ensureChunkHandlers
|
---|
184 | )
|
---|
185 | .replace(/\$hasOwnProperty\$/g, RuntimeGlobals.hasOwnProperty)
|
---|
186 | .replace(/\$hmrModuleData\$/g, RuntimeGlobals.hmrModuleData)
|
---|
187 | .replace(
|
---|
188 | /\$hmrDownloadUpdateHandlers\$/g,
|
---|
189 | RuntimeGlobals.hmrDownloadUpdateHandlers
|
---|
190 | )
|
---|
191 | .replace(
|
---|
192 | /\$hmrInvalidateModuleHandlers\$/g,
|
---|
193 | RuntimeGlobals.hmrInvalidateModuleHandlers
|
---|
194 | )
|
---|
195 | ])
|
---|
196 | : "// no HMR",
|
---|
197 | "",
|
---|
198 | withHmrManifest
|
---|
199 | ? Template.asString([
|
---|
200 | `${RuntimeGlobals.hmrDownloadManifest} = function() {`,
|
---|
201 | Template.indent([
|
---|
202 | "return Promise.resolve().then(function() {",
|
---|
203 | Template.indent([
|
---|
204 | `return require(${JSON.stringify(rootOutputDir)} + ${
|
---|
205 | RuntimeGlobals.getUpdateManifestFilename
|
---|
206 | }());`
|
---|
207 | ]),
|
---|
208 | '}).catch(function(err) { if(err.code !== "MODULE_NOT_FOUND") throw err; });'
|
---|
209 | ]),
|
---|
210 | "}"
|
---|
211 | ])
|
---|
212 | : "// no HMR manifest"
|
---|
213 | ]);
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | module.exports = RequireChunkLoadingRuntimeModule;
|
---|