2 MIT License http://www.opensource.org/licenses/mit-license.php
7 const RuntimeGlobals = require("../RuntimeGlobals");
8 const RuntimeModule = require("../RuntimeModule");
9 const Template = require("../Template");
12 getChunkFilenameTemplate
13 } = require("../javascript/JavascriptModulesPlugin");
14 const { getInitialChunkIds } = require("../javascript/StartupHelpers");
15 const compileBooleanMatcher = require("../util/compileBooleanMatcher");
16 const { getUndoPath } = require("../util/identifier");
18 /** @typedef {import("../Chunk")} Chunk */
20 class RequireChunkLoadingRuntimeModule extends RuntimeModule {
21 constructor(runtimeRequirements) {
22 super("require chunk loading", RuntimeModule.STAGE_ATTACH);
23 this.runtimeRequirements = runtimeRequirements;
28 * @param {Chunk} chunk chunk
29 * @param {string} rootOutputDir root output directory
30 * @returns {string} generated code
32 _generateBaseUri(chunk, rootOutputDir) {
33 const options = chunk.getEntryOptions();
34 if (options && options.baseUri) {
35 return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`;
38 return `${RuntimeGlobals.baseURI} = require("url").pathToFileURL(${
39 rootOutputDir !== "./"
40 ? `__dirname + ${JSON.stringify("/" + rootOutputDir)}`
46 * @returns {string} runtime code
49 const { chunkGraph, chunk } = this;
50 const { runtimeTemplate } = this.compilation;
51 const fn = RuntimeGlobals.ensureChunkHandlers;
52 const withBaseURI = this.runtimeRequirements.has(RuntimeGlobals.baseURI);
53 const withExternalInstallChunk = this.runtimeRequirements.has(
54 RuntimeGlobals.externalInstallChunk
56 const withOnChunkLoad = this.runtimeRequirements.has(
57 RuntimeGlobals.onChunksLoaded
59 const withLoading = this.runtimeRequirements.has(
60 RuntimeGlobals.ensureChunkHandlers
62 const withHmr = this.runtimeRequirements.has(
63 RuntimeGlobals.hmrDownloadUpdateHandlers
65 const withHmrManifest = this.runtimeRequirements.has(
66 RuntimeGlobals.hmrDownloadManifest
68 const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
69 const hasJsMatcher = compileBooleanMatcher(conditionMap);
70 const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
72 const outputName = this.compilation.getPath(
73 getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
76 contentHashType: "javascript"
79 const rootOutputDir = getUndoPath(
81 this.compilation.outputOptions.path,
85 const stateExpression = withHmr
86 ? `${RuntimeGlobals.hmrRuntimeStatePrefix}_require`
89 return Template.asString([
91 ? this._generateBaseUri(chunk, rootOutputDir)
94 "// object to store loaded chunks",
95 '// "1" means "loaded", otherwise not loaded yet',
96 `var installedChunks = ${
97 stateExpression ? `${stateExpression} = ${stateExpression} || ` : ""
100 Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 1`).join(
108 RuntimeGlobals.onChunksLoaded
109 }.require = ${runtimeTemplate.returningFunction(
110 "installedChunks[chunkId]",
113 : "// no on chunks loaded",
115 withLoading || withExternalInstallChunk
116 ? `var installChunk = ${runtimeTemplate.basicFunction("chunk", [
117 "var moreModules = chunk.modules, chunkIds = chunk.ids, runtime = chunk.runtime;",
118 "for(var moduleId in moreModules) {",
120 `if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
122 `${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];`
127 `if(runtime) runtime(__webpack_require__);`,
128 "for(var i = 0; i < chunkIds.length; i++)",
129 Template.indent("installedChunks[chunkIds[i]] = 1;"),
130 withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : ""
132 : "// no chunk install function needed",
135 ? Template.asString([
136 "// require() chunk loading for javascript",
137 `${fn}.require = ${runtimeTemplate.basicFunction(
139 hasJsMatcher !== false
141 '// "1" is the signal for "already loaded"',
142 "if(!installedChunks[chunkId]) {",
144 hasJsMatcher === true
145 ? "if(true) { // all chunks have JS"
146 : `if(${hasJsMatcher("chunkId")}) {`,
148 `installChunk(require(${JSON.stringify(
151 RuntimeGlobals.getChunkScriptFilename
154 "} else installedChunks[chunkId] = 1;",
159 : "installedChunks[chunkId] = 1;"
162 : "// no chunk loading",
164 withExternalInstallChunk
165 ? Template.asString([
166 "module.exports = __webpack_require__;",
167 `${RuntimeGlobals.externalInstallChunk} = installChunk;`
169 : "// no external install chunk",
172 ? Template.asString([
173 "function loadUpdateChunk(chunkId, updatedModulesList) {",
175 `var update = require(${JSON.stringify(rootOutputDir)} + ${
176 RuntimeGlobals.getChunkUpdateScriptFilename
178 "var updatedModules = update.modules;",
179 "var runtime = update.runtime;",
180 "for(var moduleId in updatedModules) {",
182 `if(${RuntimeGlobals.hasOwnProperty}(updatedModules, moduleId)) {`,
184 `currentUpdate[moduleId] = updatedModules[moduleId];`,
185 "if(updatedModulesList) updatedModulesList.push(moduleId);"
190 "if(runtime) currentUpdateRuntime.push(runtime);"
194 Template.getFunctionContent(
195 require("../hmr/JavascriptHotModuleReplacement.runtime.js")
197 .replace(/\$key\$/g, "require")
198 .replace(/\$installedChunks\$/g, "installedChunks")
199 .replace(/\$loadUpdateChunk\$/g, "loadUpdateChunk")
200 .replace(/\$moduleCache\$/g, RuntimeGlobals.moduleCache)
201 .replace(/\$moduleFactories\$/g, RuntimeGlobals.moduleFactories)
203 /\$ensureChunkHandlers\$/g,
204 RuntimeGlobals.ensureChunkHandlers
206 .replace(/\$hasOwnProperty\$/g, RuntimeGlobals.hasOwnProperty)
207 .replace(/\$hmrModuleData\$/g, RuntimeGlobals.hmrModuleData)
209 /\$hmrDownloadUpdateHandlers\$/g,
210 RuntimeGlobals.hmrDownloadUpdateHandlers
213 /\$hmrInvalidateModuleHandlers\$/g,
214 RuntimeGlobals.hmrInvalidateModuleHandlers
220 ? Template.asString([
221 `${RuntimeGlobals.hmrDownloadManifest} = function() {`,
223 "return Promise.resolve().then(function() {",
225 `return require(${JSON.stringify(rootOutputDir)} + ${
226 RuntimeGlobals.getUpdateManifestFilename
229 "})['catch'](function(err) { if(err.code !== 'MODULE_NOT_FOUND') throw err; });"
233 : "// no HMR manifest"
238 module.exports = RequireChunkLoadingRuntimeModule;