| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 15:08:46 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | const { ModuleExternalInitFragment } = require("./ExternalModule"); | 
					
						
							| 
									
										
										
										
											2017-11-15 16:28:45 +08:00
										 |  |  | const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin"); | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | const ConcatenatedModule = require("./optimize/ConcatenatedModule"); | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 22:24:11 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */ | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2025-08-21 21:14:20 +08:00
										 |  |  | /** @typedef {import("./ExternalModule").Imported} Imported */ | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | const PLUGIN_NAME = "ExternalsPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 15:08:46 +08:00
										 |  |  | class ExternalsPlugin { | 
					
						
							| 
									
										
										
										
											2020-05-15 22:24:11 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2025-08-20 18:50:12 +08:00
										 |  |  | 	 * @param {string} type default external type | 
					
						
							| 
									
										
										
										
											2020-05-15 22:24:11 +08:00
										 |  |  | 	 * @param {Externals} externals externals config | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-12-29 15:08:46 +08:00
										 |  |  | 	constructor(type, externals) { | 
					
						
							|  |  |  | 		this.type = type; | 
					
						
							|  |  |  | 		this.externals = externals; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-12-29 15:08:46 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 		compiler.hooks.compile.tap(PLUGIN_NAME, ({ normalModuleFactory }) => { | 
					
						
							| 
									
										
										
										
											2024-08-19 19:50:35 +08:00
										 |  |  | 			new ExternalModuleFactoryPlugin(this.type, this.externals).apply( | 
					
						
							|  |  |  | 				normalModuleFactory | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2016-12-29 15:08:46 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | 
					
						
							|  |  |  | 			const { concatenatedModuleInfo } = | 
					
						
							|  |  |  | 				ConcatenatedModule.getCompilationHooks(compilation); | 
					
						
							|  |  |  | 			concatenatedModuleInfo.tap(PLUGIN_NAME, (updatedInfo, moduleInfo) => { | 
					
						
							| 
									
										
										
										
											2025-08-20 18:50:12 +08:00
										 |  |  | 				const rawExportMap = updatedInfo.rawExportMap; | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if (!rawExportMap) { | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-20 18:50:12 +08:00
										 |  |  | 				const chunkInitFragments = moduleInfo.chunkInitFragments; | 
					
						
							| 
									
										
										
										
											2025-08-21 21:14:20 +08:00
										 |  |  | 				const moduleExternalInitFragments = | 
					
						
							|  |  |  | 					/** @type {ModuleExternalInitFragment[]} */ | 
					
						
							|  |  |  | 					( | 
					
						
							|  |  |  | 						chunkInitFragments | 
					
						
							|  |  |  | 							? /** @type {unknown[]} */ | 
					
						
							|  |  |  | 								(chunkInitFragments).filter( | 
					
						
							|  |  |  | 									(fragment) => fragment instanceof ModuleExternalInitFragment | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 							: [] | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				let initFragmentChanged = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				for (const fragment of moduleExternalInitFragments) { | 
					
						
							|  |  |  | 					const imported = fragment.getImported(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (Array.isArray(imported)) { | 
					
						
							| 
									
										
										
										
											2025-08-21 21:14:20 +08:00
										 |  |  | 						const newImported = | 
					
						
							|  |  |  | 							/** @type {Imported} */ | 
					
						
							|  |  |  | 							( | 
					
						
							|  |  |  | 								imported.map(([specifier, finalName]) => [ | 
					
						
							|  |  |  | 									specifier, | 
					
						
							|  |  |  | 									rawExportMap.has(specifier) | 
					
						
							|  |  |  | 										? rawExportMap.get(specifier) | 
					
						
							|  |  |  | 										: finalName | 
					
						
							|  |  |  | 								]) | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | 						fragment.setImported(newImported); | 
					
						
							|  |  |  | 						initFragmentChanged = true; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (initFragmentChanged) { | 
					
						
							|  |  |  | 					return true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-29 15:08:46 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-29 15:08:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | module.exports = ExternalsPlugin; |