| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2025-06-27 23:44:59 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	generateJavascriptHMR | 
					
						
							|  |  |  | } = require("../hmr/JavascriptHotModuleReplacementHelper"); | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +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 */ | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | /** @typedef {import("../ChunkGraph")} ChunkGraph */ | 
					
						
							|  |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */ | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | /** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */ | 
					
						
							| 
									
										
										
										
											2022-02-21 21:23:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | class ReadFileChunkLoadingRuntimeModule extends RuntimeModule { | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 	 * @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 	constructor(runtimeRequirements) { | 
					
						
							| 
									
										
										
										
											2020-12-11 21:32:42 +08:00
										 |  |  | 		super("readFile 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 | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 	 * @param {RuntimeTemplate} runtimeTemplate the runtime template | 
					
						
							| 
									
										
										
										
											2022-02-21 21:23:47 +08:00
										 |  |  | 	 * @returns {string} generated code | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 	_generateBaseUri(chunk, rootOutputDir, runtimeTemplate) { | 
					
						
							| 
									
										
										
										
											2022-02-21 21:23:47 +08:00
										 |  |  | 		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
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 		return `${RuntimeGlobals.baseURI} = require(${runtimeTemplate.renderNodePrefixForCoreModule("url")}).pathToFileURL(${ | 
					
						
							| 
									
										
										
										
											2022-02-21 21:23:47 +08:00
										 |  |  | 			rootOutputDir | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 				? `__dirname + ${JSON.stringify(`/${rootOutputDir}`)}` | 
					
						
							| 
									
										
										
										
											2022-02-21 21:23:47 +08:00
										 |  |  | 				: "__filename" | 
					
						
							|  |  |  | 		});`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 	 * @returns {string | null} runtime code | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	generate() { | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		const compilation = /** @type {Compilation} */ (this.compilation); | 
					
						
							|  |  |  | 		const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph); | 
					
						
							|  |  |  | 		const chunk = /** @type {Chunk} */ (this.chunk); | 
					
						
							|  |  |  | 		const { runtimeTemplate } = 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); | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		const outputName = compilation.getPath( | 
					
						
							|  |  |  | 			getChunkFilenameTemplate(chunk, compilation.outputOptions), | 
					
						
							| 
									
										
										
										
											2020-08-04 04:55:51 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				chunk, | 
					
						
							|  |  |  | 				contentHashType: "javascript" | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-03-16 20:07:45 +08:00
										 |  |  | 		const rootOutputDir = getUndoPath( | 
					
						
							|  |  |  | 			outputName, | 
					
						
							| 
									
										
										
										
											2025-08-20 18:50:12 +08:00
										 |  |  | 			compilation.outputOptions.path, | 
					
						
							| 
									
										
										
										
											2021-03-16 20:07:45 +08:00
										 |  |  | 			false | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 		const stateExpression = withHmr | 
					
						
							|  |  |  | 			? `${RuntimeGlobals.hmrRuntimeStatePrefix}_readFileVm` | 
					
						
							|  |  |  | 			: undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		return Template.asString([ | 
					
						
							| 
									
										
										
										
											2020-08-29 04:39:07 +08:00
										 |  |  | 			withBaseURI | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 				? this._generateBaseUri(chunk, rootOutputDir, runtimeTemplate) | 
					
						
							| 
									
										
										
										
											2020-08-29 04:39:07 +08:00
										 |  |  | 				: "// no baseURI", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			"// object to store loaded chunks", | 
					
						
							|  |  |  | 			'// "0" means "already loaded", Promise means loading', | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 			`var installedChunks = ${ | 
					
						
							|  |  |  | 				stateExpression ? `${stateExpression} = ${stateExpression} || ` : "" | 
					
						
							|  |  |  | 			}{`,
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			Template.indent( | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 				Array.from(initialChunkIds, (id) => `${JSON.stringify(id)}: 0`).join( | 
					
						
							| 
									
										
										
										
											2021-03-15 02:54:34 +08:00
										 |  |  | 					",\n" | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			), | 
					
						
							|  |  |  | 			"};", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 			withOnChunkLoad | 
					
						
							|  |  |  | 				? `${ | 
					
						
							|  |  |  | 						RuntimeGlobals.onChunksLoaded | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 					}.readFileVm = ${runtimeTemplate.returningFunction( | 
					
						
							| 
									
										
										
										
											2021-03-09 23:16:15 +08:00
										 |  |  | 						"installedChunks[chunkId] === 0", | 
					
						
							|  |  |  | 						"chunkId" | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 					)};`
 | 
					
						
							| 
									
										
										
										
											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];` | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"}" | 
					
						
							|  |  |  | 						]), | 
					
						
							|  |  |  | 						"}", | 
					
						
							| 
									
										
										
										
											2023-05-19 23:55:47 +08:00
										 |  |  | 						`if(runtime) runtime(${RuntimeGlobals.require});`, | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 						"for(var i = 0; i < chunkIds.length; i++) {", | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 							"if(installedChunks[chunkIds[i]]) {", | 
					
						
							|  |  |  | 							Template.indent(["installedChunks[chunkIds[i]][0]();"]), | 
					
						
							|  |  |  | 							"}", | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 							"installedChunks[chunkIds[i]] = 0;" | 
					
						
							|  |  |  | 						]), | 
					
						
							|  |  |  | 						"}", | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 						withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : "" | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 					])};`
 | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 				: "// no chunk install function needed", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 			withLoading | 
					
						
							|  |  |  | 				? Template.asString([ | 
					
						
							|  |  |  | 						"// ReadFile + VM.run chunk loading for javascript", | 
					
						
							| 
									
										
										
										
											2018-11-29 21:42:16 +08:00
										 |  |  | 						`${fn}.readFileVm = function(chunkId, promises) {`, | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 						hasJsMatcher !== false | 
					
						
							|  |  |  | 							? Template.indent([ | 
					
						
							|  |  |  | 									"", | 
					
						
							|  |  |  | 									"var installedChunkData = installedChunks[chunkId];", | 
					
						
							|  |  |  | 									'if(installedChunkData !== 0) { // 0 means "already installed".', | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 									Template.indent([ | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 										'// array of [resolve, reject, promise] means "currently loading"', | 
					
						
							|  |  |  | 										"if(installedChunkData) {", | 
					
						
							|  |  |  | 										Template.indent(["promises.push(installedChunkData[2]);"]), | 
					
						
							|  |  |  | 										"} else {", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 										Template.indent([ | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 											hasJsMatcher === true | 
					
						
							|  |  |  | 												? "if(true) { // all chunks have JS" | 
					
						
							|  |  |  | 												: `if(${hasJsMatcher("chunkId")}) {`, | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 											Template.indent([ | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 												"// load the chunk and return promise to it", | 
					
						
							|  |  |  | 												"var promise = new Promise(function(resolve, reject) {", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 												Template.indent([ | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 													"installedChunkData = installedChunks[chunkId] = [resolve, reject];", | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 													`var filename = require(${runtimeTemplate.renderNodePrefixForCoreModule("path")}).join(__dirname, ${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2020-08-04 04:55:51 +08:00
										 |  |  | 														rootOutputDir | 
					
						
							|  |  |  | 													)} + ${ | 
					
						
							|  |  |  | 														RuntimeGlobals.getChunkScriptFilename | 
					
						
							|  |  |  | 													}(chunkId));`,
 | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 													`require(${runtimeTemplate.renderNodePrefixForCoreModule("fs")}).readFile(filename, 'utf-8', function(err, content) {`, | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 													Template.indent([ | 
					
						
							|  |  |  | 														"if(err) return reject(err);", | 
					
						
							|  |  |  | 														"var chunk = {};", | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 														`require(${runtimeTemplate.renderNodePrefixForCoreModule("vm")}).runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)` + | 
					
						
							|  |  |  | 															`(chunk, require, require(${runtimeTemplate.renderNodePrefixForCoreModule("path")}).dirname(filename), filename);`, | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 														"installChunk(chunk);" | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 													]), | 
					
						
							|  |  |  | 													"});" | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 												]), | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 												"});", | 
					
						
							|  |  |  | 												"promises.push(installedChunkData[2] = promise);" | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 											]), | 
					
						
							| 
									
										
										
										
											2023-05-09 03:07:45 +08:00
										 |  |  | 											hasJsMatcher === true | 
					
						
							|  |  |  | 												? "}" | 
					
						
							|  |  |  | 												: "} else installedChunks[chunkId] = 0;" | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 										]), | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 										"}" | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 									]), | 
					
						
							| 
									
										
										
										
											2019-05-22 19:07:10 +08:00
										 |  |  | 									"}" | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 								]) | 
					
						
							| 
									
										
										
										
											2020-04-15 16:51:03 +08:00
										 |  |  | 							: Template.indent(["installedChunks[chunkId] = 0;"]), | 
					
						
							| 
									
										
										
										
											2018-11-29 21:42:16 +08:00
										 |  |  | 						"};" | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 					]) | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 				: "// no chunk loading", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 			withExternalInstallChunk | 
					
						
							|  |  |  | 				? Template.asString([ | 
					
						
							| 
									
										
										
										
											2023-05-19 23:55:47 +08:00
										 |  |  | 						`module.exports = ${RuntimeGlobals.require};`, | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 						`${RuntimeGlobals.externalInstallChunk} = installChunk;` | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 					]) | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 				: "// no external install chunk", | 
					
						
							|  |  |  | 			"", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 			withHmr | 
					
						
							|  |  |  | 				? Template.asString([ | 
					
						
							|  |  |  | 						"function loadUpdateChunk(chunkId, updatedModulesList) {", | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							|  |  |  | 							"return new Promise(function(resolve, reject) {", | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 								`var filename = require(${runtimeTemplate.renderNodePrefixForCoreModule("path")}).join(__dirname, ${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2020-08-04 04:55:51 +08:00
										 |  |  | 									rootOutputDir | 
					
						
							|  |  |  | 								)} + ${RuntimeGlobals.getChunkUpdateScriptFilename}(chunkId));`,
 | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 								`require(${runtimeTemplate.renderNodePrefixForCoreModule("fs")}).readFile(filename, 'utf-8', function(err, content) {`, | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 								Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 									"if(err) return reject(err);", | 
					
						
							|  |  |  | 									"var update = {};", | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 									`require(${runtimeTemplate.renderNodePrefixForCoreModule("vm")}).runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)` + | 
					
						
							|  |  |  | 										`(update, require, require(${runtimeTemplate.renderNodePrefixForCoreModule("path")}).dirname(filename), filename);`, | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 									"var updatedModules = update.modules;", | 
					
						
							|  |  |  | 									"var runtime = update.runtime;", | 
					
						
							|  |  |  | 									"for(var moduleId in updatedModules) {", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 									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([ | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 											"currentUpdate[moduleId] = updatedModules[moduleId];", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 											"if(updatedModulesList) updatedModulesList.push(moduleId);" | 
					
						
							|  |  |  | 										]), | 
					
						
							|  |  |  | 										"}" | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 									]), | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 									"}", | 
					
						
							| 
									
										
										
										
											2018-11-29 21:13:02 +08:00
										 |  |  | 									"if(runtime) currentUpdateRuntime.push(runtime);", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 									"resolve();" | 
					
						
							|  |  |  | 								]), | 
					
						
							|  |  |  | 								"});" | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"});" | 
					
						
							|  |  |  | 						]), | 
					
						
							|  |  |  | 						"}", | 
					
						
							|  |  |  | 						"", | 
					
						
							| 
									
										
										
										
											2025-06-27 23:44:59 +08:00
										 |  |  | 						generateJavascriptHMR("readFileVm") | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 					]) | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 				: "// no HMR", | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			withHmrManifest | 
					
						
							|  |  |  | 				? Template.asString([ | 
					
						
							|  |  |  | 						`${RuntimeGlobals.hmrDownloadManifest} = function() {`, | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							|  |  |  | 							"return new Promise(function(resolve, reject) {", | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 								`var filename = require(${runtimeTemplate.renderNodePrefixForCoreModule("path")}).join(__dirname, ${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2020-08-04 04:55:51 +08:00
										 |  |  | 									rootOutputDir | 
					
						
							|  |  |  | 								)} + ${RuntimeGlobals.getUpdateManifestFilename}());`,
 | 
					
						
							| 
									
										
										
										
											2025-07-24 02:45:57 +08:00
										 |  |  | 								`require(${runtimeTemplate.renderNodePrefixForCoreModule("fs")}).readFile(filename, 'utf-8', function(err, content) {`, | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 								Template.indent([ | 
					
						
							|  |  |  | 									"if(err) {", | 
					
						
							|  |  |  | 									Template.indent([ | 
					
						
							| 
									
										
										
										
											2025-08-25 22:03:25 +08:00
										 |  |  | 										'if(["MODULE_NOT_FOUND", "ENOENT"].includes(err.code)) return resolve();', | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 										"return reject(err);" | 
					
						
							|  |  |  | 									]), | 
					
						
							|  |  |  | 									"}", | 
					
						
							|  |  |  | 									"try { resolve(JSON.parse(content)); }", | 
					
						
							|  |  |  | 									"catch(e) { reject(e); }" | 
					
						
							|  |  |  | 								]), | 
					
						
							|  |  |  | 								"});" | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 							]), | 
					
						
							|  |  |  | 							"});" | 
					
						
							|  |  |  | 						]), | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 						"}" | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 					]) | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 				: "// no HMR manifest" | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ReadFileChunkLoadingRuntimeModule; |