mirror of https://github.com/webpack/webpack.git
				
				
				
			
		
			
				
	
	
		
			96 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			96 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
| /*
 | |
| 	MIT License http://www.opensource.org/licenses/mit-license.php
 | |
| 	Author Naoyuki Kanezawa @nkzawa
 | |
| */
 | |
| 
 | |
| "use strict";
 | |
| 
 | |
| const MultiEntryPlugin = require("./MultiEntryPlugin");
 | |
| const MultiModuleFactory = require("./MultiModuleFactory");
 | |
| const SingleEntryPlugin = require("./SingleEntryPlugin");
 | |
| const MultiEntryDependency = require("./dependencies/MultiEntryDependency");
 | |
| const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
 | |
| 
 | |
| /** @typedef {import("./Compiler")} Compiler */
 | |
| /** @typedef {import("./Compiler").EntryOptionValuesFunction} EntryOptionValuesFunction */
 | |
| 
 | |
| class DynamicEntryPlugin {
 | |
| 	/**
 | |
| 	 * @param {string} context the context path
 | |
| 	 * @param {EntryOptionValuesFunction} entry the entry value
 | |
| 	 */
 | |
| 	constructor(context, entry) {
 | |
| 		this.context = context;
 | |
| 		this.entry = entry;
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * @param {Compiler} compiler the compiler instance
 | |
| 	 * @returns {void}
 | |
| 	 */
 | |
| 	apply(compiler) {
 | |
| 		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) => {
 | |
| 				/**
 | |
| 				 * @param {string|string[]} entry entry value or array of entry values
 | |
| 				 * @param {string} name name of entry
 | |
| 				 * @returns {Promise<any>} returns the promise resolving the Compilation#addEntry function
 | |
| 				 */
 | |
| 				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();
 | |
| 						});
 | |
| 					});
 | |
| 				};
 | |
| 
 | |
| 				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);
 | |
| 					}
 | |
| 				});
 | |
| 			}
 | |
| 		);
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * @param {string|string[]} entry entry value or array of entry paths
 | |
| 	 * @param {string} name name of entry
 | |
| 	 * @returns {SingleEntryDependency|MultiEntryDependency} returns dep
 | |
| 	 */
 | |
| 	static createDependency(entry, name) {
 | |
| 		if (Array.isArray(entry)) {
 | |
| 			return MultiEntryPlugin.createDependency(entry, name);
 | |
| 		} else {
 | |
| 			return SingleEntryPlugin.createDependency(entry, name);
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| module.exports = DynamicEntryPlugin;
 |