| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 01:29:24 +08:00
										 |  |  | const validateOptions = require("schema-utils"); | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | const schema = require("../../schemas/plugins/container/ContainerPlugin.json"); | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | const ContainerEntryDependency = require("./ContainerEntryDependency"); | 
					
						
							|  |  |  | const ContainerEntryModuleFactory = require("./ContainerEntryModuleFactory"); | 
					
						
							|  |  |  | const ContainerExposedDependency = require("./ContainerExposedDependency"); | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | const { parseOptions } = require("./options"); | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | /** @typedef {import("../../declarations/plugins/container/ContainerPlugin").ContainerPluginOptions} ContainerPluginOptions */ | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const PLUGIN_NAME = "ContainerPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | class ContainerPlugin { | 
					
						
							| 
									
										
										
										
											2020-02-27 04:52:58 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ContainerPluginOptions} options options | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 		validateOptions(schema, options, { name: "Container Plugin" }); | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 		this._options = { | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 			name: options.name, | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 			shareScope: options.shareScope || "default", | 
					
						
							| 
									
										
										
										
											2020-02-27 04:52:58 +08:00
										 |  |  | 			library: options.library || { | 
					
						
							|  |  |  | 				type: "var", | 
					
						
							|  |  |  | 				name: options.name | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 			filename: options.filename || undefined, | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 			exposes: parseOptions( | 
					
						
							|  |  |  | 				options.exposes, | 
					
						
							|  |  |  | 				item => ({ | 
					
						
							|  |  |  | 					import: Array.isArray(item) ? item : [item] | 
					
						
							|  |  |  | 				}), | 
					
						
							|  |  |  | 				item => ({ | 
					
						
							|  |  |  | 					import: Array.isArray(item.import) ? item.import : [item.import] | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			) | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 		const { name, exposes, shareScope, filename, library } = this._options; | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 04:52:58 +08:00
										 |  |  | 		compiler.options.output.enabledLibraryTypes.push(library.type); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => { | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 			const dep = new ContainerEntryDependency(name, exposes, shareScope); | 
					
						
							| 
									
										
										
										
											2020-02-27 01:29:24 +08:00
										 |  |  | 			dep.loc = { name }; | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 			compilation.addEntry( | 
					
						
							|  |  |  | 				compilation.options.context, | 
					
						
							| 
									
										
										
										
											2020-02-27 01:29:24 +08:00
										 |  |  | 				dep, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					name, | 
					
						
							| 
									
										
										
										
											2020-02-27 04:52:58 +08:00
										 |  |  | 					filename, | 
					
						
							|  |  |  | 					library | 
					
						
							| 
									
										
										
										
											2020-02-27 01:29:24 +08:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2020-02-26 07:30:34 +08:00
										 |  |  | 				error => { | 
					
						
							|  |  |  | 					if (error) return callback(error); | 
					
						
							|  |  |  | 					callback(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							|  |  |  | 			PLUGIN_NAME, | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					ContainerEntryDependency, | 
					
						
							|  |  |  | 					new ContainerEntryModuleFactory() | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					ContainerExposedDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ContainerPlugin; |