| 
									
										
										
										
											2020-02-27 06:28:36 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const isValidExternalsType = require("../../schemas/plugins/container/ExternalsType.check.js"); | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | const SharePlugin = require("../sharing/SharePlugin"); | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const createSchemaValidation = require("../util/create-schema-validation"); | 
					
						
							| 
									
										
										
										
											2020-02-27 06:28:36 +08:00
										 |  |  | const ContainerPlugin = require("./ContainerPlugin"); | 
					
						
							|  |  |  | const ContainerReferencePlugin = require("./ContainerReferencePlugin"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 03:45:56 +08:00
										 |  |  | /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ExternalsType} ExternalsType */ | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ModuleFederationPluginOptions} ModuleFederationPluginOptions */ | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").Shared} Shared */ | 
					
						
							| 
									
										
										
										
											2020-02-27 06:28:36 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const validate = createSchemaValidation( | 
					
						
							|  |  |  | 	require("../../schemas/plugins/container/ModuleFederationPlugin.check.js"), | 
					
						
							|  |  |  | 	() => require("../../schemas/plugins/container/ModuleFederationPlugin.json"), | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Module Federation Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "options" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2020-02-27 06:28:36 +08:00
										 |  |  | class ModuleFederationPlugin { | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ModuleFederationPluginOptions} options options | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-02-27 06:28:36 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 		validate(options); | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		this._options = options; | 
					
						
							| 
									
										
										
										
											2020-02-27 06:28:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2020-02-27 06:28:36 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 		const { _options: options } = this; | 
					
						
							| 
									
										
										
										
											2020-06-04 08:06:57 +08:00
										 |  |  | 		const library = options.library || { type: "var", name: options.name }; | 
					
						
							|  |  |  | 		const remoteType = | 
					
						
							| 
									
										
										
										
											2020-08-26 03:45:56 +08:00
										 |  |  | 			options.remoteType || | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 			(options.library && isValidExternalsType(options.library.type) | 
					
						
							| 
									
										
										
										
											2020-08-26 03:45:56 +08:00
										 |  |  | 				? /** @type {ExternalsType} */ (options.library.type) | 
					
						
							|  |  |  | 				: "script"); | 
					
						
							| 
									
										
										
										
											2020-04-14 21:13:15 +08:00
										 |  |  | 		if ( | 
					
						
							| 
									
										
										
										
											2020-06-04 08:06:57 +08:00
										 |  |  | 			library && | 
					
						
							|  |  |  | 			!compiler.options.output.enabledLibraryTypes.includes(library.type) | 
					
						
							| 
									
										
										
										
											2020-04-14 21:13:15 +08:00
										 |  |  | 		) { | 
					
						
							| 
									
										
										
										
											2020-06-04 08:06:57 +08:00
										 |  |  | 			compiler.options.output.enabledLibraryTypes.push(library.type); | 
					
						
							| 
									
										
										
										
											2020-04-14 21:13:15 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		compiler.hooks.afterPlugins.tap("ModuleFederationPlugin", () => { | 
					
						
							| 
									
										
										
										
											2020-04-17 07:45:21 +08:00
										 |  |  | 			if ( | 
					
						
							|  |  |  | 				options.exposes && | 
					
						
							|  |  |  | 				(Array.isArray(options.exposes) | 
					
						
							|  |  |  | 					? options.exposes.length > 0 | 
					
						
							|  |  |  | 					: Object.keys(options.exposes).length > 0) | 
					
						
							|  |  |  | 			) { | 
					
						
							|  |  |  | 				new ContainerPlugin({ | 
					
						
							|  |  |  | 					name: options.name, | 
					
						
							| 
									
										
										
										
											2020-06-04 08:06:57 +08:00
										 |  |  | 					library, | 
					
						
							| 
									
										
										
										
											2020-04-17 07:45:21 +08:00
										 |  |  | 					filename: options.filename, | 
					
						
							| 
									
										
										
										
											2021-07-05 16:53:18 +08:00
										 |  |  | 					runtime: options.runtime, | 
					
						
							| 
									
										
										
										
											2022-07-09 13:51:16 +08:00
										 |  |  | 					shareScope: options.shareScope, | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 					exposes: options.exposes | 
					
						
							| 
									
										
										
										
											2020-04-17 07:45:21 +08:00
										 |  |  | 				}).apply(compiler); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if ( | 
					
						
							|  |  |  | 				options.remotes && | 
					
						
							|  |  |  | 				(Array.isArray(options.remotes) | 
					
						
							|  |  |  | 					? options.remotes.length > 0 | 
					
						
							|  |  |  | 					: Object.keys(options.remotes).length > 0) | 
					
						
							|  |  |  | 			) { | 
					
						
							|  |  |  | 				new ContainerReferencePlugin({ | 
					
						
							| 
									
										
										
										
											2020-06-04 08:06:57 +08:00
										 |  |  | 					remoteType, | 
					
						
							| 
									
										
										
										
											2022-07-09 13:51:16 +08:00
										 |  |  | 					shareScope: options.shareScope, | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 					remotes: options.remotes | 
					
						
							|  |  |  | 				}).apply(compiler); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (options.shared) { | 
					
						
							|  |  |  | 				new SharePlugin({ | 
					
						
							|  |  |  | 					shared: options.shared, | 
					
						
							|  |  |  | 					shareScope: options.shareScope | 
					
						
							| 
									
										
										
										
											2020-04-17 07:45:21 +08:00
										 |  |  | 				}).apply(compiler); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-04-14 21:13:15 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-02-27 06:28:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ModuleFederationPlugin; |