| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const RuntimeModule = require("../RuntimeModule"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							|  |  |  | /** @typedef {import("../ChunkGraph")} ChunkGraph */ | 
					
						
							|  |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | class StartupChunkDependenciesRuntimeModule extends RuntimeModule { | 
					
						
							| 
									
										
										
										
											2023-05-25 03:37:58 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {boolean} asyncChunkLoading use async chunk loading | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 	constructor(asyncChunkLoading) { | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 		super("startup chunk dependencies", RuntimeModule.STAGE_TRIGGER); | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 		this.asyncChunkLoading = asyncChunkLoading; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 	 * @returns {string | null} runtime code | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	generate() { | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 		const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph); | 
					
						
							|  |  |  | 		const chunk = /** @type {Chunk} */ (this.chunk); | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 		const chunkIds = Array.from( | 
					
						
							|  |  |  | 			chunkGraph.getChunkEntryDependentChunksIterable(chunk) | 
					
						
							|  |  |  | 		).map(chunk => { | 
					
						
							|  |  |  | 			return chunk.id; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 		const compilation = /** @type {Compilation} */ (this.compilation); | 
					
						
							|  |  |  | 		const { runtimeTemplate } = compilation; | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			`var next = ${RuntimeGlobals.startup};`, | 
					
						
							|  |  |  | 			`${RuntimeGlobals.startup} = ${runtimeTemplate.basicFunction( | 
					
						
							|  |  |  | 				"", | 
					
						
							|  |  |  | 				!this.asyncChunkLoading | 
					
						
							|  |  |  | 					? chunkIds | 
					
						
							|  |  |  | 							.map( | 
					
						
							|  |  |  | 								id => `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)});` | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 							.concat("return next();") | 
					
						
							|  |  |  | 					: chunkIds.length === 1 | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 						? `return ${RuntimeGlobals.ensureChunk}(${JSON.stringify( | 
					
						
							|  |  |  | 								chunkIds[0] | 
					
						
							|  |  |  | 							)}).then(next);`
 | 
					
						
							|  |  |  | 						: chunkIds.length > 2 | 
					
						
							|  |  |  | 							? [ | 
					
						
							|  |  |  | 									// using map is shorter for 3 or more chunks
 | 
					
						
							|  |  |  | 									`return Promise.all(${JSON.stringify(chunkIds)}.map(${ | 
					
						
							|  |  |  | 										RuntimeGlobals.ensureChunk | 
					
						
							|  |  |  | 									}, ${RuntimeGlobals.require})).then(next);`
 | 
					
						
							|  |  |  | 								] | 
					
						
							|  |  |  | 							: [ | 
					
						
							|  |  |  | 									// calling ensureChunk directly is shorter for 0 - 2 chunks
 | 
					
						
							|  |  |  | 									"return Promise.all([", | 
					
						
							|  |  |  | 									Template.indent( | 
					
						
							|  |  |  | 										chunkIds | 
					
						
							|  |  |  | 											.map( | 
					
						
							|  |  |  | 												id => | 
					
						
							|  |  |  | 													`${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)})` | 
					
						
							|  |  |  | 											) | 
					
						
							|  |  |  | 											.join(",\n") | 
					
						
							|  |  |  | 									), | 
					
						
							|  |  |  | 									"]).then(next);" | 
					
						
							|  |  |  | 								] | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 			)};`
 | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = StartupChunkDependenciesRuntimeModule; |