| 
									
										
										
										
											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"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-06-25 22:36:59 +08:00
										 |  |  | /** @typedef {import("./Compiler").EntryOptionValuesFunction} EntryOptionValuesFunction */ | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | class DynamicEntryPlugin { | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} context the context path | 
					
						
							|  |  |  | 	 * @param {EntryOptionValuesFunction} entry the entry value | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.make.tapAsync( | 
					
						
							|  |  |  | 			"DynamicEntryPlugin", | 
					
						
							|  |  |  | 			(compilation, callback) => { | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | 				/** | 
					
						
							|  |  |  | 				 * @param {string|string[]} entry entry value or array of entry values | 
					
						
							|  |  |  | 				 * @param {string} name name of entry | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 				 * @returns {Promise<void>} returns the promise resolving the Compilation#addEntry function | 
					
						
							| 
									
										
										
										
											2018-05-04 00:57:02 +08:00
										 |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				const addEntry = (entry, name) => { | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 					const deps = DynamicEntryPlugin.createDependencies(entry, name); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					return new Promise((resolve, reject) => { | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 						for (const dep of deps) { | 
					
						
							|  |  |  | 							compilation.addEntry(this.context, dep, name, err => { | 
					
						
							|  |  |  | 								if (err) return reject(err); | 
					
						
							|  |  |  | 								resolve(); | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Promise.resolve(this.entry()).then(entry => { | 
					
						
							|  |  |  | 					if (typeof entry === "string" || Array.isArray(entry)) { | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 						addEntry(entry, "main").then(() => { | 
					
						
							|  |  |  | 							callback(); | 
					
						
							|  |  |  | 						}, callback); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} else if (typeof entry === "object") { | 
					
						
							|  |  |  | 						Promise.all( | 
					
						
							|  |  |  | 							Object.keys(entry).map(name => { | 
					
						
							|  |  |  | 								return addEntry(entry[name], name); | 
					
						
							|  |  |  | 							}) | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 						).then(() => { | 
					
						
							|  |  |  | 							callback(); | 
					
						
							|  |  |  | 						}, callback); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 				}, callback); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-31 03:36:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string|string[]} entry entry value or array of entry paths | 
					
						
							|  |  |  | 	 * @param {string} name name of entry | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 	 * @returns {EntryDependency[]} dependencies | 
					
						
							| 
									
										
										
										
											2018-07-31 03:36:46 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 	static createDependencies(entry, name) { | 
					
						
							|  |  |  | 		const entryArray = Array.isArray(entry) ? entry : [entry]; | 
					
						
							|  |  |  | 		return entryArray.map(entry => EntryPlugin.createDependency(entry, name)); | 
					
						
							| 
									
										
										
										
											2018-07-31 03:36:46 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = DynamicEntryPlugin; |