| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const RuntimeModule = require("../RuntimeModule"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							| 
									
										
										
										
											2020-01-16 22:18:03 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	chunkHasJs, | 
					
						
							|  |  |  | 	getChunkFilenameTemplate | 
					
						
							|  |  |  | } = require("../javascript/JavascriptModulesPlugin"); | 
					
						
							| 
									
										
										
										
											2021-03-15 02:54:34 +08:00
										 |  |  | const { getInitialChunkIds } = require("../javascript/StartupHelpers"); | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | const compileBooleanMatcher = require("../util/compileBooleanMatcher"); | 
					
						
							| 
									
										
										
										
											2020-08-04 04:55:51 +08:00
										 |  |  | const { getUndoPath } = require("../util/identifier"); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 21:23:47 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | class RequireChunkLoadingRuntimeModule extends RuntimeModule { | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 	constructor(runtimeRequirements) { | 
					
						
							| 
									
										
										
										
											2020-12-11 21:32:42 +08:00
										 |  |  | 		super("require chunk loading", RuntimeModule.STAGE_ATTACH); | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 		this.runtimeRequirements = runtimeRequirements; | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 21:23:47 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @private | 
					
						
							|  |  |  | 	 * @param {Chunk} chunk chunk | 
					
						
							|  |  |  | 	 * @param {string} rootOutputDir root output directory | 
					
						
							|  |  |  | 	 * @returns {string} generated code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	_generateBaseUri(chunk, rootOutputDir) { | 
					
						
							|  |  |  | 		const options = chunk.getEntryOptions(); | 
					
						
							|  |  |  | 		if (options && options.baseUri) { | 
					
						
							| 
									
										
										
										
											2022-02-22 16:54:35 +08:00
										 |  |  | 			return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`; | 
					
						
							| 
									
										
										
										
											2022-02-21 21:23:47 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return `${RuntimeGlobals.baseURI} = require("url").pathToFileURL(${ | 
					
						
							|  |  |  | 			rootOutputDir !== "./" | 
					
						
							|  |  |  | 				? `__dirname + ${JSON.stringify("/" + rootOutputDir)}` | 
					
						
							|  |  |  | 				: "__filename" | 
					
						
							|  |  |  | 		});`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string} runtime code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	generate() { | 
					
						
							| 
									
										
										
										
											2021-04-13 02:53:25 +08:00
										 |  |  | 		const { chunkGraph, chunk } = this; | 
					
						
							|  |  |  | 		const { runtimeTemplate } = this.compilation; | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		const fn = RuntimeGlobals.ensureChunkHandlers; | 
					
						
							| 
									
										
										
										
											2020-08-29 04:39:07 +08:00
										 |  |  | 		const withBaseURI = this.runtimeRequirements.has(RuntimeGlobals.baseURI); | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 		const withExternalInstallChunk = this.runtimeRequirements.has( | 
					
						
							|  |  |  | 			RuntimeGlobals.externalInstallChunk | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 		const withOnChunkLoad = this.runtimeRequirements.has( | 
					
						
							|  |  |  | 			RuntimeGlobals.onChunksLoaded | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 		const withLoading = this.runtimeRequirements.has( | 
					
						
							|  |  |  | 			RuntimeGlobals.ensureChunkHandlers | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		const withHmr = this.runtimeRequirements.has( | 
					
						
							|  |  |  | 			RuntimeGlobals.hmrDownloadUpdateHandlers | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		const withHmrManifest = this.runtimeRequirements.has( | 
					
						
							|  |  |  | 			RuntimeGlobals.hmrDownloadManifest | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 		const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs); | 
					
						
							|  |  |  | 		const hasJsMatcher = compileBooleanMatcher(conditionMap); | 
					
						
							| 
									
										
										
										
											2021-12-03 23:23:09 +08:00
										 |  |  | 		const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs); | 
					
						
							| 
									
										
										
										
											2019-12-17 20:58:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 04:55:51 +08:00
										 |  |  | 		const outputName = this.compilation.getPath( | 
					
						
							|  |  |  | 			getChunkFilenameTemplate(chunk, this.compilation.outputOptions), | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				chunk, | 
					
						
							|  |  |  | 				contentHashType: "javascript" | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-03-16 20:07:45 +08:00
										 |  |  | 		const rootOutputDir = getUndoPath( | 
					
						
							|  |  |  | 			outputName, | 
					
						
							|  |  |  | 			this.compilation.outputOptions.path, | 
					
						
							|  |  |  | 			true | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2019-12-17 20:58:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 		const stateExpression = withHmr | 
					
						
							|  |  |  | 			? `${RuntimeGlobals.hmrRuntimeStatePrefix}_require` | 
					
						
							|  |  |  | 			: undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		return Template.asString([ | 
					
						
							| 
									
										
										
										
											2020-08-29 04:39:07 +08:00
										 |  |  | 			withBaseURI | 
					
						
							| 
									
										
										
										
											2022-02-21 21:23:47 +08:00
										 |  |  | 				? this._generateBaseUri(chunk, rootOutputDir) | 
					
						
							| 
									
										
										
										
											2020-08-29 04:39:07 +08:00
										 |  |  | 				: "// no baseURI", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			"// object to store loaded chunks", | 
					
						
							|  |  |  | 			'// "1" means "loaded", otherwise not loaded yet', | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 			`var installedChunks = ${ | 
					
						
							|  |  |  | 				stateExpression ? `${stateExpression} = ${stateExpression} || ` : "" | 
					
						
							|  |  |  | 			}{`,
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			Template.indent( | 
					
						
							| 
									
										
										
										
											2021-03-15 02:54:34 +08:00
										 |  |  | 				Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 1`).join( | 
					
						
							|  |  |  | 					",\n" | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			), | 
					
						
							|  |  |  | 			"};", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 			withOnChunkLoad | 
					
						
							|  |  |  | 				? `${ | 
					
						
							|  |  |  | 						RuntimeGlobals.onChunksLoaded | 
					
						
							| 
									
										
										
										
											2021-03-09 23:16:15 +08:00
										 |  |  | 				  }.require = ${runtimeTemplate.returningFunction( | 
					
						
							|  |  |  | 						"installedChunks[chunkId]", | 
					
						
							|  |  |  | 						"chunkId" | 
					
						
							|  |  |  | 				  )};`
 | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 				: "// no on chunks loaded", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 			withLoading || withExternalInstallChunk | 
					
						
							|  |  |  | 				? `var installChunk = ${runtimeTemplate.basicFunction("chunk", [ | 
					
						
							|  |  |  | 						"var moreModules = chunk.modules, chunkIds = chunk.ids, runtime = chunk.runtime;", | 
					
						
							|  |  |  | 						"for(var moduleId in moreModules) {", | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							|  |  |  | 							`if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`, | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							|  |  |  | 								`${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];` | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"}" | 
					
						
							|  |  |  | 						]), | 
					
						
							|  |  |  | 						"}", | 
					
						
							|  |  |  | 						`if(runtime) runtime(__webpack_require__);`, | 
					
						
							|  |  |  | 						"for(var i = 0; i < chunkIds.length; i++)", | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 						Template.indent("installedChunks[chunkIds[i]] = 1;"), | 
					
						
							|  |  |  | 						withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : "" | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 				  ])};`
 | 
					
						
							|  |  |  | 				: "// no chunk install function needed", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 			withLoading | 
					
						
							|  |  |  | 				? Template.asString([ | 
					
						
							|  |  |  | 						"// require() chunk loading for javascript", | 
					
						
							| 
									
										
										
										
											2021-03-12 01:32:32 +08:00
										 |  |  | 						`${fn}.require = ${runtimeTemplate.basicFunction( | 
					
						
							|  |  |  | 							"chunkId, promises", | 
					
						
							|  |  |  | 							hasJsMatcher !== false | 
					
						
							|  |  |  | 								? [ | 
					
						
							|  |  |  | 										'// "1" is the signal for "already loaded"', | 
					
						
							|  |  |  | 										"if(!installedChunks[chunkId]) {", | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 										Template.indent([ | 
					
						
							| 
									
										
										
										
											2021-03-12 01:32:32 +08:00
										 |  |  | 											hasJsMatcher === true | 
					
						
							|  |  |  | 												? "if(true) { // all chunks have JS" | 
					
						
							|  |  |  | 												: `if(${hasJsMatcher("chunkId")}) {`, | 
					
						
							|  |  |  | 											Template.indent([ | 
					
						
							|  |  |  | 												`installChunk(require(${JSON.stringify( | 
					
						
							|  |  |  | 													rootOutputDir | 
					
						
							|  |  |  | 												)} + ${ | 
					
						
							|  |  |  | 													RuntimeGlobals.getChunkScriptFilename | 
					
						
							|  |  |  | 												}(chunkId)));`
 | 
					
						
							|  |  |  | 											]), | 
					
						
							|  |  |  | 											"} else installedChunks[chunkId] = 1;", | 
					
						
							|  |  |  | 											"" | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 										]), | 
					
						
							| 
									
										
										
										
											2021-03-12 01:32:32 +08:00
										 |  |  | 										"}" | 
					
						
							|  |  |  | 								  ] | 
					
						
							|  |  |  | 								: "installedChunks[chunkId] = 1;" | 
					
						
							|  |  |  | 						)};`
 | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 				  ]) | 
					
						
							|  |  |  | 				: "// no chunk loading", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 			withExternalInstallChunk | 
					
						
							|  |  |  | 				? Template.asString([ | 
					
						
							|  |  |  | 						"module.exports = __webpack_require__;", | 
					
						
							|  |  |  | 						`${RuntimeGlobals.externalInstallChunk} = installChunk;` | 
					
						
							|  |  |  | 				  ]) | 
					
						
							|  |  |  | 				: "// no external install chunk", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 			withHmr | 
					
						
							|  |  |  | 				? Template.asString([ | 
					
						
							|  |  |  | 						"function loadUpdateChunk(chunkId, updatedModulesList) {", | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							| 
									
										
										
										
											2020-08-04 04:55:51 +08:00
										 |  |  | 							`var update = require(${JSON.stringify(rootOutputDir)} + ${ | 
					
						
							|  |  |  | 								RuntimeGlobals.getChunkUpdateScriptFilename | 
					
						
							|  |  |  | 							}(chunkId));`,
 | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 							"var updatedModules = update.modules;", | 
					
						
							|  |  |  | 							"var runtime = update.runtime;", | 
					
						
							|  |  |  | 							"for(var moduleId in updatedModules) {", | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							| 
									
										
										
										
											2019-12-02 22:59:37 +08:00
										 |  |  | 								`if(${RuntimeGlobals.hasOwnProperty}(updatedModules, moduleId)) {`, | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 								Template.indent([ | 
					
						
							|  |  |  | 									`currentUpdate[moduleId] = updatedModules[moduleId];`, | 
					
						
							|  |  |  | 									"if(updatedModulesList) updatedModulesList.push(moduleId);" | 
					
						
							|  |  |  | 								]), | 
					
						
							|  |  |  | 								"}" | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"}", | 
					
						
							| 
									
										
										
										
											2018-11-29 21:13:02 +08:00
										 |  |  | 							"if(runtime) currentUpdateRuntime.push(runtime);" | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 						]), | 
					
						
							|  |  |  | 						"}", | 
					
						
							|  |  |  | 						"", | 
					
						
							| 
									
										
										
										
											2020-04-15 16:51:03 +08:00
										 |  |  | 						Template.getFunctionContent( | 
					
						
							|  |  |  | 							require("../hmr/JavascriptHotModuleReplacement.runtime.js") | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 							.replace(/\$key\$/g, "require") | 
					
						
							|  |  |  | 							.replace(/\$installedChunks\$/g, "installedChunks") | 
					
						
							|  |  |  | 							.replace(/\$loadUpdateChunk\$/g, "loadUpdateChunk") | 
					
						
							|  |  |  | 							.replace(/\$moduleCache\$/g, RuntimeGlobals.moduleCache) | 
					
						
							|  |  |  | 							.replace(/\$moduleFactories\$/g, RuntimeGlobals.moduleFactories) | 
					
						
							|  |  |  | 							.replace( | 
					
						
							|  |  |  | 								/\$ensureChunkHandlers\$/g, | 
					
						
							|  |  |  | 								RuntimeGlobals.ensureChunkHandlers | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 							.replace(/\$hasOwnProperty\$/g, RuntimeGlobals.hasOwnProperty) | 
					
						
							|  |  |  | 							.replace(/\$hmrModuleData\$/g, RuntimeGlobals.hmrModuleData) | 
					
						
							|  |  |  | 							.replace( | 
					
						
							|  |  |  | 								/\$hmrDownloadUpdateHandlers\$/g, | 
					
						
							|  |  |  | 								RuntimeGlobals.hmrDownloadUpdateHandlers | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 							.replace( | 
					
						
							|  |  |  | 								/\$hmrInvalidateModuleHandlers\$/g, | 
					
						
							|  |  |  | 								RuntimeGlobals.hmrInvalidateModuleHandlers | 
					
						
							|  |  |  | 							) | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 				  ]) | 
					
						
							|  |  |  | 				: "// no HMR", | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			withHmrManifest | 
					
						
							|  |  |  | 				? Template.asString([ | 
					
						
							|  |  |  | 						`${RuntimeGlobals.hmrDownloadManifest} = function() {`, | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							|  |  |  | 							"return Promise.resolve().then(function() {", | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							| 
									
										
										
										
											2020-08-04 04:55:51 +08:00
										 |  |  | 								`return require(${JSON.stringify(rootOutputDir)} + ${ | 
					
						
							|  |  |  | 									RuntimeGlobals.getUpdateManifestFilename | 
					
						
							|  |  |  | 								}());`
 | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 							]), | 
					
						
							| 
									
										
										
										
											2021-10-15 13:40:47 +08:00
										 |  |  | 							"})['catch'](function(err) { if(err.code !== 'MODULE_NOT_FOUND') throw err; });" | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 						]), | 
					
						
							|  |  |  | 						"}" | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 				  ]) | 
					
						
							|  |  |  | 				: "// no HMR manifest" | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | module.exports = RequireChunkLoadingRuntimeModule; |