| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Naoyuki Kanezawa @nkzawa | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const MultiEntryDependency = require("./dependencies/MultiEntryDependency"); | 
					
						
							|  |  |  | const SingleEntryDependency = require("./dependencies/SingleEntryDependency"); | 
					
						
							|  |  |  | const MultiModuleFactory = require("./MultiModuleFactory"); | 
					
						
							|  |  |  | const MultiEntryPlugin = require("./MultiEntryPlugin"); | 
					
						
							|  |  |  | const SingleEntryPlugin = require("./SingleEntryPlugin"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DynamicEntryPlugin { | 
					
						
							|  |  |  | 	constructor(context, entry) { | 
					
						
							|  |  |  | 		this.context = context; | 
					
						
							|  |  |  | 		this.entry = entry; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"DynamicEntryPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				const multiModuleFactory = new MultiModuleFactory(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					MultiEntryDependency, | 
					
						
							|  |  |  | 					multiModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					SingleEntryDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.make.tapAsync( | 
					
						
							|  |  |  | 			"DynamicEntryPlugin", | 
					
						
							|  |  |  | 			(compilation, callback) => { | 
					
						
							|  |  |  | 				const addEntry = (entry, name) => { | 
					
						
							|  |  |  | 					const dep = DynamicEntryPlugin.createDependency(entry, name); | 
					
						
							|  |  |  | 					return new Promise((resolve, reject) => { | 
					
						
							|  |  |  | 						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)) { | 
					
						
							|  |  |  | 						addEntry(entry, "main").then(() => callback(), callback); | 
					
						
							|  |  |  | 					} else if (typeof entry === "object") { | 
					
						
							|  |  |  | 						Promise.all( | 
					
						
							|  |  |  | 							Object.keys(entry).map(name => { | 
					
						
							|  |  |  | 								return addEntry(entry[name], name); | 
					
						
							|  |  |  | 							}) | 
					
						
							|  |  |  | 						).then(() => callback(), callback); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = DynamicEntryPlugin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | DynamicEntryPlugin.createDependency = (entry, name) => { | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 	if (Array.isArray(entry)) { | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | 		return MultiEntryPlugin.createDependency(entry, name); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		return SingleEntryPlugin.createDependency(entry, name); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-09 15:31:49 +08:00
										 |  |  | }; |