[79a0317] | 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 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 19 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 20 | /** @typedef {import("../Compilation")} Compilation */
|
---|
| 21 | /** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
|
---|
| 22 |
|
---|
| 23 | class RequireChunkLoadingRuntimeModule extends RuntimeModule {
|
---|
| 24 | /**
|
---|
| 25 | * @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements
|
---|
| 26 | */
|
---|
| 27 | constructor(runtimeRequirements) {
|
---|
| 28 | super("require chunk loading", RuntimeModule.STAGE_ATTACH);
|
---|
| 29 | this.runtimeRequirements = runtimeRequirements;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * @private
|
---|
| 34 | * @param {Chunk} chunk chunk
|
---|
| 35 | * @param {string} rootOutputDir root output directory
|
---|
| 36 | * @returns {string} generated code
|
---|
| 37 | */
|
---|
| 38 | _generateBaseUri(chunk, rootOutputDir) {
|
---|
| 39 | const options = chunk.getEntryOptions();
|
---|
| 40 | if (options && options.baseUri) {
|
---|
| 41 | return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | return `${RuntimeGlobals.baseURI} = require("url").pathToFileURL(${
|
---|
| 45 | rootOutputDir !== "./"
|
---|
| 46 | ? `__dirname + ${JSON.stringify(`/${rootOutputDir}`)}`
|
---|
| 47 | : "__filename"
|
---|
| 48 | });`;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * @returns {string | null} runtime code
|
---|
| 53 | */
|
---|
| 54 | generate() {
|
---|
| 55 | const compilation = /** @type {Compilation} */ (this.compilation);
|
---|
| 56 | const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
|
---|
| 57 | const chunk = /** @type {Chunk} */ (this.chunk);
|
---|
| 58 | const { runtimeTemplate } = compilation;
|
---|
| 59 | const fn = RuntimeGlobals.ensureChunkHandlers;
|
---|
| 60 | const withBaseURI = this.runtimeRequirements.has(RuntimeGlobals.baseURI);
|
---|
| 61 | const withExternalInstallChunk = this.runtimeRequirements.has(
|
---|
| 62 | RuntimeGlobals.externalInstallChunk
|
---|
| 63 | );
|
---|
| 64 | const withOnChunkLoad = this.runtimeRequirements.has(
|
---|
| 65 | RuntimeGlobals.onChunksLoaded
|
---|
| 66 | );
|
---|
| 67 | const withLoading = this.runtimeRequirements.has(
|
---|
| 68 | RuntimeGlobals.ensureChunkHandlers
|
---|
| 69 | );
|
---|
| 70 | const withHmr = this.runtimeRequirements.has(
|
---|
| 71 | RuntimeGlobals.hmrDownloadUpdateHandlers
|
---|
| 72 | );
|
---|
| 73 | const withHmrManifest = this.runtimeRequirements.has(
|
---|
| 74 | RuntimeGlobals.hmrDownloadManifest
|
---|
| 75 | );
|
---|
| 76 | const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
|
---|
| 77 | const hasJsMatcher = compileBooleanMatcher(conditionMap);
|
---|
| 78 | const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
|
---|
| 79 |
|
---|
| 80 | const outputName = compilation.getPath(
|
---|
| 81 | getChunkFilenameTemplate(chunk, compilation.outputOptions),
|
---|
| 82 | {
|
---|
| 83 | chunk,
|
---|
| 84 | contentHashType: "javascript"
|
---|
| 85 | }
|
---|
| 86 | );
|
---|
| 87 | const rootOutputDir = getUndoPath(
|
---|
| 88 | outputName,
|
---|
| 89 | /** @type {string} */ (compilation.outputOptions.path),
|
---|
| 90 | true
|
---|
| 91 | );
|
---|
| 92 |
|
---|
| 93 | const stateExpression = withHmr
|
---|
| 94 | ? `${RuntimeGlobals.hmrRuntimeStatePrefix}_require`
|
---|
| 95 | : undefined;
|
---|
| 96 |
|
---|
| 97 | return Template.asString([
|
---|
| 98 | withBaseURI
|
---|
| 99 | ? this._generateBaseUri(chunk, rootOutputDir)
|
---|
| 100 | : "// no baseURI",
|
---|
| 101 | "",
|
---|
| 102 | "// object to store loaded chunks",
|
---|
| 103 | '// "1" means "loaded", otherwise not loaded yet',
|
---|
| 104 | `var installedChunks = ${
|
---|
| 105 | stateExpression ? `${stateExpression} = ${stateExpression} || ` : ""
|
---|
| 106 | }{`,
|
---|
| 107 | Template.indent(
|
---|
| 108 | Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 1`).join(
|
---|
| 109 | ",\n"
|
---|
| 110 | )
|
---|
| 111 | ),
|
---|
| 112 | "};",
|
---|
| 113 | "",
|
---|
| 114 | withOnChunkLoad
|
---|
| 115 | ? `${
|
---|
| 116 | RuntimeGlobals.onChunksLoaded
|
---|
| 117 | }.require = ${runtimeTemplate.returningFunction(
|
---|
| 118 | "installedChunks[chunkId]",
|
---|
| 119 | "chunkId"
|
---|
| 120 | )};`
|
---|
| 121 | : "// no on chunks loaded",
|
---|
| 122 | "",
|
---|
| 123 | withLoading || withExternalInstallChunk
|
---|
| 124 | ? `var installChunk = ${runtimeTemplate.basicFunction("chunk", [
|
---|
| 125 | "var moreModules = chunk.modules, chunkIds = chunk.ids, runtime = chunk.runtime;",
|
---|
| 126 | "for(var moduleId in moreModules) {",
|
---|
| 127 | Template.indent([
|
---|
| 128 | `if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
|
---|
| 129 | Template.indent([
|
---|
| 130 | `${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];`
|
---|
| 131 | ]),
|
---|
| 132 | "}"
|
---|
| 133 | ]),
|
---|
| 134 | "}",
|
---|
| 135 | `if(runtime) runtime(${RuntimeGlobals.require});`,
|
---|
| 136 | "for(var i = 0; i < chunkIds.length; i++)",
|
---|
| 137 | Template.indent("installedChunks[chunkIds[i]] = 1;"),
|
---|
| 138 | withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : ""
|
---|
| 139 | ])};`
|
---|
| 140 | : "// no chunk install function needed",
|
---|
| 141 | "",
|
---|
| 142 | withLoading
|
---|
| 143 | ? Template.asString([
|
---|
| 144 | "// require() chunk loading for javascript",
|
---|
| 145 | `${fn}.require = ${runtimeTemplate.basicFunction(
|
---|
| 146 | "chunkId, promises",
|
---|
| 147 | hasJsMatcher !== false
|
---|
| 148 | ? [
|
---|
| 149 | '// "1" is the signal for "already loaded"',
|
---|
| 150 | "if(!installedChunks[chunkId]) {",
|
---|
| 151 | Template.indent([
|
---|
| 152 | hasJsMatcher === true
|
---|
| 153 | ? "if(true) { // all chunks have JS"
|
---|
| 154 | : `if(${hasJsMatcher("chunkId")}) {`,
|
---|
| 155 | Template.indent([
|
---|
| 156 | `installChunk(require(${JSON.stringify(
|
---|
| 157 | rootOutputDir
|
---|
| 158 | )} + ${
|
---|
| 159 | RuntimeGlobals.getChunkScriptFilename
|
---|
| 160 | }(chunkId)));`
|
---|
| 161 | ]),
|
---|
| 162 | "} else installedChunks[chunkId] = 1;",
|
---|
| 163 | ""
|
---|
| 164 | ]),
|
---|
| 165 | "}"
|
---|
| 166 | ]
|
---|
| 167 | : "installedChunks[chunkId] = 1;"
|
---|
| 168 | )};`
|
---|
| 169 | ])
|
---|
| 170 | : "// no chunk loading",
|
---|
| 171 | "",
|
---|
| 172 | withExternalInstallChunk
|
---|
| 173 | ? Template.asString([
|
---|
| 174 | `module.exports = ${RuntimeGlobals.require};`,
|
---|
| 175 | `${RuntimeGlobals.externalInstallChunk} = installChunk;`
|
---|
| 176 | ])
|
---|
| 177 | : "// no external install chunk",
|
---|
| 178 | "",
|
---|
| 179 | withHmr
|
---|
| 180 | ? Template.asString([
|
---|
| 181 | "function loadUpdateChunk(chunkId, updatedModulesList) {",
|
---|
| 182 | Template.indent([
|
---|
| 183 | `var update = require(${JSON.stringify(rootOutputDir)} + ${
|
---|
| 184 | RuntimeGlobals.getChunkUpdateScriptFilename
|
---|
| 185 | }(chunkId));`,
|
---|
| 186 | "var updatedModules = update.modules;",
|
---|
| 187 | "var runtime = update.runtime;",
|
---|
| 188 | "for(var moduleId in updatedModules) {",
|
---|
| 189 | Template.indent([
|
---|
| 190 | `if(${RuntimeGlobals.hasOwnProperty}(updatedModules, moduleId)) {`,
|
---|
| 191 | Template.indent([
|
---|
| 192 | "currentUpdate[moduleId] = updatedModules[moduleId];",
|
---|
| 193 | "if(updatedModulesList) updatedModulesList.push(moduleId);"
|
---|
| 194 | ]),
|
---|
| 195 | "}"
|
---|
| 196 | ]),
|
---|
| 197 | "}",
|
---|
| 198 | "if(runtime) currentUpdateRuntime.push(runtime);"
|
---|
| 199 | ]),
|
---|
| 200 | "}",
|
---|
| 201 | "",
|
---|
| 202 | Template.getFunctionContent(
|
---|
| 203 | require("../hmr/JavascriptHotModuleReplacement.runtime.js")
|
---|
| 204 | )
|
---|
| 205 | .replace(/\$key\$/g, "require")
|
---|
| 206 | .replace(/\$installedChunks\$/g, "installedChunks")
|
---|
| 207 | .replace(/\$loadUpdateChunk\$/g, "loadUpdateChunk")
|
---|
| 208 | .replace(/\$moduleCache\$/g, RuntimeGlobals.moduleCache)
|
---|
| 209 | .replace(/\$moduleFactories\$/g, RuntimeGlobals.moduleFactories)
|
---|
| 210 | .replace(
|
---|
| 211 | /\$ensureChunkHandlers\$/g,
|
---|
| 212 | RuntimeGlobals.ensureChunkHandlers
|
---|
| 213 | )
|
---|
| 214 | .replace(/\$hasOwnProperty\$/g, RuntimeGlobals.hasOwnProperty)
|
---|
| 215 | .replace(/\$hmrModuleData\$/g, RuntimeGlobals.hmrModuleData)
|
---|
| 216 | .replace(
|
---|
| 217 | /\$hmrDownloadUpdateHandlers\$/g,
|
---|
| 218 | RuntimeGlobals.hmrDownloadUpdateHandlers
|
---|
| 219 | )
|
---|
| 220 | .replace(
|
---|
| 221 | /\$hmrInvalidateModuleHandlers\$/g,
|
---|
| 222 | RuntimeGlobals.hmrInvalidateModuleHandlers
|
---|
| 223 | )
|
---|
| 224 | ])
|
---|
| 225 | : "// no HMR",
|
---|
| 226 | "",
|
---|
| 227 | withHmrManifest
|
---|
| 228 | ? Template.asString([
|
---|
| 229 | `${RuntimeGlobals.hmrDownloadManifest} = function() {`,
|
---|
| 230 | Template.indent([
|
---|
| 231 | "return Promise.resolve().then(function() {",
|
---|
| 232 | Template.indent([
|
---|
| 233 | `return require(${JSON.stringify(rootOutputDir)} + ${
|
---|
| 234 | RuntimeGlobals.getUpdateManifestFilename
|
---|
| 235 | }());`
|
---|
| 236 | ]),
|
---|
| 237 | "})['catch'](function(err) { if(err.code !== 'MODULE_NOT_FOUND') throw err; });"
|
---|
| 238 | ]),
|
---|
| 239 | "}"
|
---|
| 240 | ])
|
---|
| 241 | : "// no HMR manifest"
|
---|
| 242 | ]);
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | module.exports = RequireChunkLoadingRuntimeModule;
|
---|