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