| 
									
										
										
										
											2018-12-29 19:48:59 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | const StartupChunkDependenciesRuntimeModule = require("./StartupChunkDependenciesRuntimeModule"); | 
					
						
							|  |  |  | const StartupEntrypointRuntimeModule = require("./StartupEntrypointRuntimeModule"); | 
					
						
							| 
									
										
										
										
											2018-12-29 19:48:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class StartupChunkDependenciesPlugin { | 
					
						
							| 
									
										
										
										
											2019-05-21 04:53:58 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 		this.chunkLoading = options.chunkLoading; | 
					
						
							| 
									
										
										
										
											2019-05-21 04:53:58 +08:00
										 |  |  | 		this.asyncChunkLoading = | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 			typeof options.asyncChunkLoading === "boolean" | 
					
						
							| 
									
										
										
										
											2019-05-21 04:53:58 +08:00
										 |  |  | 				? options.asyncChunkLoading | 
					
						
							|  |  |  | 				: true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-29 19:48:59 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-12-29 19:48:59 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							|  |  |  | 			"StartupChunkDependenciesPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 				const globalChunkLoading = compilation.outputOptions.chunkLoading; | 
					
						
							|  |  |  | 				const isEnabledForChunk = chunk => { | 
					
						
							|  |  |  | 					const options = chunk.getEntryOptions(); | 
					
						
							|  |  |  | 					const chunkLoading = | 
					
						
							| 
									
										
										
										
											2021-04-15 02:21:17 +08:00
										 |  |  | 						options && options.chunkLoading !== undefined | 
					
						
							|  |  |  | 							? options.chunkLoading | 
					
						
							|  |  |  | 							: globalChunkLoading; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 					return chunkLoading === this.chunkLoading; | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2018-12-29 19:48:59 +08:00
										 |  |  | 				compilation.hooks.additionalTreeRuntimeRequirements.tap( | 
					
						
							|  |  |  | 					"StartupChunkDependenciesPlugin", | 
					
						
							| 
									
										
										
										
											2021-04-09 21:50:25 +08:00
										 |  |  | 					(chunk, set, { chunkGraph }) => { | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 						if (!isEnabledForChunk(chunk)) return; | 
					
						
							| 
									
										
										
										
											2021-04-09 21:50:25 +08:00
										 |  |  | 						if (chunkGraph.hasChunkEntryDependentChunks(chunk)) { | 
					
						
							| 
									
										
										
										
											2018-12-29 19:48:59 +08:00
										 |  |  | 							set.add(RuntimeGlobals.startup); | 
					
						
							|  |  |  | 							set.add(RuntimeGlobals.ensureChunk); | 
					
						
							|  |  |  | 							set.add(RuntimeGlobals.ensureChunkIncludeEntries); | 
					
						
							|  |  |  | 							compilation.addRuntimeModule( | 
					
						
							|  |  |  | 								chunk, | 
					
						
							|  |  |  | 								new StartupChunkDependenciesRuntimeModule( | 
					
						
							| 
									
										
										
										
											2019-05-21 04:53:58 +08:00
										 |  |  | 									this.asyncChunkLoading | 
					
						
							| 
									
										
										
										
											2018-12-29 19:48:59 +08:00
										 |  |  | 								) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.startupEntrypoint) | 
					
						
							|  |  |  | 					.tap("StartupChunkDependenciesPlugin", (chunk, set) => { | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 						if (!isEnabledForChunk(chunk)) return; | 
					
						
							| 
									
										
										
										
											2021-02-18 22:49:43 +08:00
										 |  |  | 						set.add(RuntimeGlobals.require); | 
					
						
							| 
									
										
										
										
											2020-06-29 17:46:16 +08:00
										 |  |  | 						set.add(RuntimeGlobals.ensureChunk); | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.ensureChunkIncludeEntries); | 
					
						
							|  |  |  | 						compilation.addRuntimeModule( | 
					
						
							|  |  |  | 							chunk, | 
					
						
							|  |  |  | 							new StartupEntrypointRuntimeModule(this.asyncChunkLoading) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2018-12-29 19:48:59 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = StartupChunkDependenciesPlugin; |