| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @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 | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 04:21:42 +08:00
										 |  |  | 		compiler.hooks.make.tapPromise( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			"DynamicEntryPlugin", | 
					
						
							| 
									
										
										
										
											2020-02-05 04:21:42 +08:00
										 |  |  | 			(compilation, callback) => | 
					
						
							|  |  |  | 				Promise.resolve(this.entry()) | 
					
						
							|  |  |  | 					.then(entry => { | 
					
						
							|  |  |  | 						const promises = []; | 
					
						
							| 
									
										
										
										
											2020-02-26 20:08:05 +08:00
										 |  |  | 						for (const name of Object.keys(entry)) { | 
					
						
							|  |  |  | 							const desc = entry[name]; | 
					
						
							|  |  |  | 							const options = EntryOptionPlugin.entryDescriptionToOptions( | 
					
						
							| 
									
										
										
										
											2020-02-26 19:34:57 +08:00
										 |  |  | 								compiler, | 
					
						
							| 
									
										
										
										
											2020-02-26 20:08:05 +08:00
										 |  |  | 								name, | 
					
						
							|  |  |  | 								desc | 
					
						
							| 
									
										
										
										
											2020-02-05 04:21:42 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2020-02-26 20:08:05 +08:00
										 |  |  | 							for (const entry of desc.import) { | 
					
						
							|  |  |  | 								promises.push( | 
					
						
							|  |  |  | 									new Promise((resolve, reject) => { | 
					
						
							|  |  |  | 										compilation.addEntry( | 
					
						
							|  |  |  | 											this.context, | 
					
						
							|  |  |  | 											EntryPlugin.createDependency(entry, options), | 
					
						
							|  |  |  | 											options, | 
					
						
							|  |  |  | 											err => { | 
					
						
							|  |  |  | 												if (err) return reject(err); | 
					
						
							|  |  |  | 												resolve(); | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										); | 
					
						
							|  |  |  | 									}) | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2020-02-05 04:21:42 +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; |