| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Naoyuki Kanezawa @nkzawa | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 04:21:42 +08:00
										 |  |  | const EntryOptionPlugin = require("./EntryOptionPlugin"); | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | const EntryPlugin = require("./EntryPlugin"); | 
					
						
							|  |  |  | const EntryDependency = require("./dependencies/EntryDependency"); | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-02 05:18:10 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescriptionNormalized */ | 
					
						
							| 
									
										
										
										
											2020-02-26 20:08:05 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").EntryDynamicNormalized} EntryDynamic */ | 
					
						
							| 
									
										
										
										
											2020-02-03 22:46:14 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */ | 
					
						
							| 
									
										
										
										
											2020-02-26 20:08:05 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").EntryStaticNormalized} EntryStatic */ | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | class DynamicEntryPlugin { | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} context the context path | 
					
						
							| 
									
										
										
										
											2018-09-19 19:05:22 +08:00
										 |  |  | 	 * @param {EntryDynamic} entry the entry value | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | 	constructor(context, entry) { | 
					
						
							|  |  |  | 		this.context = context; | 
					
						
							|  |  |  | 		this.entry = entry; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"DynamicEntryPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 					EntryDependency, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-02 05:18:10 +08:00
										 |  |  | 		compiler.hooks.make.tapPromise("DynamicEntryPlugin", compilation => | 
					
						
							|  |  |  | 			Promise.resolve(this.entry()) | 
					
						
							|  |  |  | 				.then(entry => { | 
					
						
							|  |  |  | 					const promises = []; | 
					
						
							|  |  |  | 					for (const name of Object.keys(entry)) { | 
					
						
							|  |  |  | 						const desc = entry[name]; | 
					
						
							|  |  |  | 						const options = EntryOptionPlugin.entryDescriptionToOptions( | 
					
						
							|  |  |  | 							compiler, | 
					
						
							|  |  |  | 							name, | 
					
						
							|  |  |  | 							desc | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						for (const entry of /** @type {NonNullable<EntryDescriptionNormalized["import"]>} */ ( | 
					
						
							|  |  |  | 							desc.import | 
					
						
							|  |  |  | 						)) { | 
					
						
							|  |  |  | 							promises.push( | 
					
						
							|  |  |  | 								new Promise( | 
					
						
							|  |  |  | 									/** | 
					
						
							| 
									
										
										
										
											2025-03-07 21:12:22 +08:00
										 |  |  | 									 * @param {(value?: undefined) => void} resolve resolve | 
					
						
							| 
									
										
										
										
											2024-10-02 05:18:10 +08:00
										 |  |  | 									 * @param {(reason?: Error) => void} reject reject | 
					
						
							|  |  |  | 									 */ | 
					
						
							|  |  |  | 									(resolve, reject) => { | 
					
						
							| 
									
										
										
										
											2020-02-26 20:08:05 +08:00
										 |  |  | 										compilation.addEntry( | 
					
						
							|  |  |  | 											this.context, | 
					
						
							|  |  |  | 											EntryPlugin.createDependency(entry, options), | 
					
						
							|  |  |  | 											options, | 
					
						
							|  |  |  | 											err => { | 
					
						
							|  |  |  | 												if (err) return reject(err); | 
					
						
							|  |  |  | 												resolve(); | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										); | 
					
						
							| 
									
										
										
										
											2024-10-02 05:18:10 +08:00
										 |  |  | 									} | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2020-02-26 20:08:05 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2024-10-02 05:18:10 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					return Promise.all(promises); | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 				.then(x => {}) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = DynamicEntryPlugin; |