| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ExternalsPlugin = require("../ExternalsPlugin"); | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const createSchemaValidation = require("../util/create-schema-validation"); | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | const FallbackDependency = require("./FallbackDependency"); | 
					
						
							|  |  |  | const FallbackItemDependency = require("./FallbackItemDependency"); | 
					
						
							|  |  |  | const FallbackModuleFactory = require("./FallbackModuleFactory"); | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | const RemoteModule = require("./RemoteModule"); | 
					
						
							|  |  |  | const RemoteRuntimeModule = require("./RemoteRuntimeModule"); | 
					
						
							|  |  |  | const RemoteToExternalDependency = require("./RemoteToExternalDependency"); | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | const { parseOptions } = require("./options"); | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | /** @typedef {import("../../declarations/plugins/container/ContainerReferencePlugin").ContainerReferencePluginOptions} ContainerReferencePluginOptions */ | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | /** @typedef {import("../../declarations/plugins/container/ContainerReferencePlugin").RemotesConfig} RemotesConfig */ | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const validate = createSchemaValidation( | 
					
						
							|  |  |  | 	require("../../schemas/plugins/container/ContainerReferencePlugin.check.js"), | 
					
						
							|  |  |  | 	() => | 
					
						
							|  |  |  | 		require("../../schemas/plugins/container/ContainerReferencePlugin.json"), | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Container Reference Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "options" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 23:49:20 +08:00
										 |  |  | const slashCode = "/".charCodeAt(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | class ContainerReferencePlugin { | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ContainerReferencePluginOptions} options options | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 		validate(options); | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 		this._remoteType = options.remoteType; | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 		this._remotes = parseOptions( | 
					
						
							|  |  |  | 			options.remotes, | 
					
						
							|  |  |  | 			item => ({ | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 				external: Array.isArray(item) ? item : [item], | 
					
						
							|  |  |  | 				shareScope: options.shareScope || "default" | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 			}), | 
					
						
							|  |  |  | 			item => ({ | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 				external: Array.isArray(item.external) | 
					
						
							|  |  |  | 					? item.external | 
					
						
							|  |  |  | 					: [item.external], | 
					
						
							|  |  |  | 				shareScope: item.shareScope || options.shareScope || "default" | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 		const { _remotes: remotes, _remoteType: remoteType } = this; | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 		/** @type {Record<string, string>} */ | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 		const remoteExternals = {}; | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 		for (const [key, config] of remotes) { | 
					
						
							|  |  |  | 			let i = 0; | 
					
						
							|  |  |  | 			for (const external of config.external) { | 
					
						
							| 
									
										
										
										
											2020-05-19 23:49:20 +08:00
										 |  |  | 				if (external.startsWith("internal ")) continue; | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 				remoteExternals[ | 
					
						
							|  |  |  | 					`webpack/container/reference/${key}${i ? `/fallback-${i}` : ""}` | 
					
						
							|  |  |  | 				] = external; | 
					
						
							|  |  |  | 				i++; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 19:39:58 +08:00
										 |  |  | 		new ExternalsPlugin(remoteType, remoteExternals).apply(compiler); | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"ContainerReferencePlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					RemoteToExternalDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 06:27:56 +08:00
										 |  |  | 				compilation.dependencyFactories.set( | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 					FallbackItemDependency, | 
					
						
							| 
									
										
										
										
											2020-02-27 06:27:56 +08:00
										 |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 00:16:01 +08:00
										 |  |  | 				compilation.dependencyFactories.set( | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 					FallbackDependency, | 
					
						
							|  |  |  | 					new FallbackModuleFactory() | 
					
						
							| 
									
										
										
										
											2020-02-27 00:16:01 +08:00
										 |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 				normalModuleFactory.hooks.factorize.tap( | 
					
						
							|  |  |  | 					"ContainerReferencePlugin", | 
					
						
							|  |  |  | 					data => { | 
					
						
							|  |  |  | 						if (!data.request.includes("!")) { | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 							for (const [key, config] of remotes) { | 
					
						
							| 
									
										
										
										
											2020-05-19 23:49:20 +08:00
										 |  |  | 								if ( | 
					
						
							|  |  |  | 									data.request.startsWith(`${key}`) && | 
					
						
							|  |  |  | 									(data.request.length === key.length || | 
					
						
							|  |  |  | 										data.request.charCodeAt(key.length) === slashCode) | 
					
						
							|  |  |  | 								) { | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 									return new RemoteModule( | 
					
						
							|  |  |  | 										data.request, | 
					
						
							| 
									
										
										
										
											2020-05-19 23:49:20 +08:00
										 |  |  | 										config.external.map((external, i) => | 
					
						
							|  |  |  | 											external.startsWith("internal ") | 
					
						
							|  |  |  | 												? external.slice(9) | 
					
						
							|  |  |  | 												: `webpack/container/reference/${key}${ | 
					
						
							|  |  |  | 														i ? `/fallback-${i}` : "" | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 													}`
 | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 										), | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 										`.${data.request.slice(key.length)}`, | 
					
						
							|  |  |  | 										config.shareScope | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 									); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 05:32:48 +08:00
										 |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.ensureChunkHandlers) | 
					
						
							| 
									
										
										
										
											2020-05-19 23:49:20 +08:00
										 |  |  | 					.tap("ContainerReferencePlugin", (chunk, set) => { | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 						set.add(RuntimeGlobals.module); | 
					
						
							| 
									
										
										
										
											2020-05-27 18:02:43 +08:00
										 |  |  | 						set.add(RuntimeGlobals.moduleFactoriesAddOnly); | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 						set.add(RuntimeGlobals.hasOwnProperty); | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 						set.add(RuntimeGlobals.initializeSharing); | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.shareScopeMap); | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 						compilation.addRuntimeModule(chunk, new RemoteRuntimeModule()); | 
					
						
							| 
									
										
										
										
											2020-02-27 05:32:48 +08:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ContainerReferencePlugin; |