| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							|  |  |  | const { isSubset } = require("../util/SetHelpers"); | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | const { getAllChunks } = require("./ChunkHelpers"); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  | /** @typedef {import("../util/Hash")} Hash */ | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | /** @typedef {import("../Chunk").ChunkId} ChunkId */ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | /** @typedef {import("../ChunkGraph")} ChunkGraph */ | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | /** @typedef {import("../Entrypoint")} Entrypoint */ | 
					
						
							| 
									
										
										
										
											2022-02-08 20:29:56 +08:00
										 |  |  | /** @typedef {import("../ChunkGraph").EntryModuleWithChunkGroup} EntryModuleWithChunkGroup */ | 
					
						
							| 
									
										
										
										
											2020-02-07 17:05:51 +08:00
										 |  |  | /** @typedef {import("../ChunkGroup")} ChunkGroup */ | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */ | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | /** @typedef {(string|number)[]} EntryItem */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 23:34:26 +08:00
										 |  |  | const EXPORT_PREFIX = `var ${RuntimeGlobals.exports} = `; | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  |  * @param {ChunkGraph} chunkGraph chunkGraph | 
					
						
							|  |  |  |  * @param {RuntimeTemplate} runtimeTemplate runtimeTemplate | 
					
						
							| 
									
										
										
										
											2022-02-08 20:29:56 +08:00
										 |  |  |  * @param {EntryModuleWithChunkGroup[]} entries entries | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  |  * @param {Chunk} chunk chunk | 
					
						
							|  |  |  |  * @param {boolean} passive true: passive startup with on chunks loaded | 
					
						
							|  |  |  |  * @returns {string} runtime code | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | exports.generateEntryStartup = ( | 
					
						
							|  |  |  | 	chunkGraph, | 
					
						
							|  |  |  | 	runtimeTemplate, | 
					
						
							|  |  |  | 	entries, | 
					
						
							|  |  |  | 	chunk, | 
					
						
							|  |  |  | 	passive | 
					
						
							|  |  |  | ) => { | 
					
						
							|  |  |  | 	/** @type {string[]} */ | 
					
						
							|  |  |  | 	const runtime = [ | 
					
						
							|  |  |  | 		`var __webpack_exec__ = ${runtimeTemplate.returningFunction( | 
					
						
							| 
									
										
										
										
											2023-05-19 23:55:47 +08:00
										 |  |  | 			`${RuntimeGlobals.require}(${RuntimeGlobals.entryModuleId} = moduleId)`, | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 			"moduleId" | 
					
						
							|  |  |  | 		)}`
 | 
					
						
							|  |  |  | 	]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const runModule = id => { | 
					
						
							|  |  |  | 		return `__webpack_exec__(${JSON.stringify(id)})`; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	const outputCombination = (chunks, moduleIds, final) => { | 
					
						
							|  |  |  | 		if (chunks.size === 0) { | 
					
						
							| 
									
										
										
										
											2021-03-19 22:58:42 +08:00
										 |  |  | 			runtime.push( | 
					
						
							|  |  |  | 				`${final ? EXPORT_PREFIX : ""}(${moduleIds.map(runModule).join(", ")});` | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			const fn = runtimeTemplate.returningFunction( | 
					
						
							|  |  |  | 				moduleIds.map(runModule).join(", ") | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			runtime.push( | 
					
						
							| 
									
										
										
										
											2021-03-19 22:58:42 +08:00
										 |  |  | 				`${final && !passive ? EXPORT_PREFIX : ""}${ | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 					passive | 
					
						
							|  |  |  | 						? RuntimeGlobals.onChunksLoaded | 
					
						
							|  |  |  | 						: RuntimeGlobals.startupEntrypoint | 
					
						
							| 
									
										
										
										
											2021-03-19 22:58:42 +08:00
										 |  |  | 				}(0, ${JSON.stringify(Array.from(chunks, c => c.id))}, ${fn});`
 | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2021-03-19 22:58:42 +08:00
										 |  |  | 			if (final && passive) { | 
					
						
							|  |  |  | 				runtime.push(`${EXPORT_PREFIX}${RuntimeGlobals.onChunksLoaded}();`); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-06-25 01:44:02 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2020-02-07 17:05:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 	let currentChunks = undefined; | 
					
						
							|  |  |  | 	let currentModuleIds = undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (const [module, entrypoint] of entries) { | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 		const runtimeChunk = | 
					
						
							|  |  |  | 			/** @type {Entrypoint} */ | 
					
						
							|  |  |  | 			(entrypoint).getRuntimeChunk(); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 		const moduleId = chunkGraph.getModuleId(module); | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 		const chunks = getAllChunks( | 
					
						
							|  |  |  | 			/** @type {Entrypoint} */ (entrypoint), | 
					
						
							|  |  |  | 			chunk, | 
					
						
							|  |  |  | 			runtimeChunk | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 		if ( | 
					
						
							|  |  |  | 			currentChunks && | 
					
						
							|  |  |  | 			currentChunks.size === chunks.size && | 
					
						
							|  |  |  | 			isSubset(currentChunks, chunks) | 
					
						
							|  |  |  | 		) { | 
					
						
							|  |  |  | 			currentModuleIds.push(moduleId); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			if (currentChunks) { | 
					
						
							|  |  |  | 				outputCombination(currentChunks, currentModuleIds); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			currentChunks = chunks; | 
					
						
							|  |  |  | 			currentModuleIds = [moduleId]; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// output current modules with export prefix
 | 
					
						
							|  |  |  | 	if (currentChunks) { | 
					
						
							|  |  |  | 		outputCombination(currentChunks, currentModuleIds, true); | 
					
						
							| 
									
										
										
										
											2020-02-07 17:05:51 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 	runtime.push(""); | 
					
						
							|  |  |  | 	return Template.asString(runtime); | 
					
						
							| 
									
										
										
										
											2020-02-07 17:05:51 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-03-15 02:54:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {Hash} hash the hash to update | 
					
						
							|  |  |  |  * @param {ChunkGraph} chunkGraph chunkGraph | 
					
						
							| 
									
										
										
										
											2022-02-08 20:29:56 +08:00
										 |  |  |  * @param {EntryModuleWithChunkGroup[]} entries entries | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  |  * @param {Chunk} chunk chunk | 
					
						
							|  |  |  |  * @returns {void} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | exports.updateHashForEntryStartup = (hash, chunkGraph, entries, chunk) => { | 
					
						
							|  |  |  | 	for (const [module, entrypoint] of entries) { | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 		const runtimeChunk = | 
					
						
							|  |  |  | 			/** @type {Entrypoint} */ | 
					
						
							|  |  |  | 			(entrypoint).getRuntimeChunk(); | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  | 		const moduleId = chunkGraph.getModuleId(module); | 
					
						
							|  |  |  | 		hash.update(`${moduleId}`); | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 		for (const c of getAllChunks( | 
					
						
							|  |  |  | 			/** @type {Entrypoint} */ (entrypoint), | 
					
						
							|  |  |  | 			chunk, | 
					
						
							|  |  |  | 			/** @type {Chunk} */ (runtimeChunk) | 
					
						
							|  |  |  | 		)) { | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  | 			hash.update(`${c.id}`); | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 02:54:34 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {Chunk} chunk the chunk | 
					
						
							|  |  |  |  * @param {ChunkGraph} chunkGraph the chunk graph | 
					
						
							| 
									
										
										
										
											2021-12-03 23:23:09 +08:00
										 |  |  |  * @param {function(Chunk, ChunkGraph): boolean} filterFn filter function | 
					
						
							| 
									
										
										
										
											2021-03-15 02:54:34 +08:00
										 |  |  |  * @returns {Set<number | string>} initially fulfilled chunk ids | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-12-03 23:23:09 +08:00
										 |  |  | exports.getInitialChunkIds = (chunk, chunkGraph, filterFn) => { | 
					
						
							| 
									
										
										
										
											2021-03-15 02:54:34 +08:00
										 |  |  | 	const initialChunkIds = new Set(chunk.ids); | 
					
						
							|  |  |  | 	for (const c of chunk.getAllInitialChunks()) { | 
					
						
							| 
									
										
										
										
											2021-12-03 23:23:09 +08:00
										 |  |  | 		if (c === chunk || filterFn(c, chunkGraph)) continue; | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 		for (const id of /** @type {ChunkId[]} */ (c.ids)) { | 
					
						
							|  |  |  | 			initialChunkIds.add(id); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-03-15 02:54:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return initialChunkIds; | 
					
						
							|  |  |  | }; |