| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 17:36:17 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	DEFAULT_EXPORT, | 
					
						
							|  |  |  | 	NAMESPACE_OBJECT_EXPORT | 
					
						
							|  |  |  | } = require("./util/concatenate"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | /** @typedef {import("./Module")} Module */ | 
					
						
							| 
									
										
										
										
											2024-10-25 02:13:59 +08:00
										 |  |  | /** @typedef {import("./optimize/ConcatenatedModule").ConcatenatedModuleInfo} ConcatenatedModuleInfo */ | 
					
						
							|  |  |  | /** @typedef {import("./optimize/ConcatenatedModule").ModuleInfo} ModuleInfo */ | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | const MODULE_REFERENCE_REGEXP = | 
					
						
							| 
									
										
										
										
											2025-07-02 20:02:03 +08:00
										 |  |  | 	/^__WEBPACK_MODULE_REFERENCE__(\d+)_([\da-f]+|ns)(_call)?(_directImport)?(_deferredImport)?(?:_asiSafe(\d))?__$/; | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2024-06-11 21:09:50 +08:00
										 |  |  |  * @typedef {object} ModuleReferenceOptions | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  |  * @property {string[]} ids the properties/exports of the module | 
					
						
							|  |  |  |  * @property {boolean} call true, when this referenced export is called | 
					
						
							|  |  |  |  * @property {boolean} directImport true, when this referenced export is directly imported (not via property access) | 
					
						
							| 
									
										
										
										
											2025-07-02 20:02:03 +08:00
										 |  |  |  * @property {boolean} deferredImport true, when this referenced export is deferred | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  |  * @property {boolean | undefined} asiSafe if the position is ASI safe or unknown | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ConcatenationScope { | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-10-30 00:53:56 +08:00
										 |  |  | 	 * @param {ModuleInfo[] | Map<Module, ModuleInfo>} modulesMap all module info by module | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 	 * @param {ConcatenatedModuleInfo} currentModule the current module info | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | 	 * @param {Set<string>} usedNames all used names | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | 	constructor(modulesMap, currentModule, usedNames) { | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 		this._currentModule = currentModule; | 
					
						
							| 
									
										
										
										
											2020-10-30 00:53:56 +08:00
										 |  |  | 		if (Array.isArray(modulesMap)) { | 
					
						
							|  |  |  | 			const map = new Map(); | 
					
						
							|  |  |  | 			for (const info of modulesMap) { | 
					
						
							|  |  |  | 				map.set(info.module, info); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			modulesMap = map; | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | 		this.usedNames = usedNames; | 
					
						
							| 
									
										
										
										
											2020-10-30 00:53:56 +08:00
										 |  |  | 		this._modulesMap = modulesMap; | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Module} module the referenced module | 
					
						
							|  |  |  | 	 * @returns {boolean} true, when it's in the scope | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 	isModuleInScope(module) { | 
					
						
							|  |  |  | 		return this._modulesMap.has(module); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} exportName name of the export | 
					
						
							|  |  |  | 	 * @param {string} symbol identifier of the export in source code | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-09-11 16:02:55 +08:00
										 |  |  | 	registerExport(exportName, symbol) { | 
					
						
							| 
									
										
										
										
											2020-09-15 18:21:37 +08:00
										 |  |  | 		if (!this._currentModule.exportMap) { | 
					
						
							|  |  |  | 			this._currentModule.exportMap = new Map(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-09-11 16:02:55 +08:00
										 |  |  | 		if (!this._currentModule.exportMap.has(exportName)) { | 
					
						
							|  |  |  | 			this._currentModule.exportMap.set(exportName, symbol); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 18:21:37 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} exportName name of the export | 
					
						
							|  |  |  | 	 * @param {string} expression expression to be used | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	registerRawExport(exportName, expression) { | 
					
						
							|  |  |  | 		if (!this._currentModule.rawExportMap) { | 
					
						
							|  |  |  | 			this._currentModule.rawExportMap = new Map(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (!this._currentModule.rawExportMap.has(exportName)) { | 
					
						
							|  |  |  | 			this._currentModule.rawExportMap.set(exportName, expression); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} exportName name of the export | 
					
						
							|  |  |  | 	 * @returns {string | undefined} the expression of the export | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getRawExport(exportName) { | 
					
						
							|  |  |  | 		if (!this._currentModule.rawExportMap) { | 
					
						
							|  |  |  | 			return undefined; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return this._currentModule.rawExportMap.get(exportName); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} exportName name of the export | 
					
						
							|  |  |  | 	 * @param {string} expression expression to be used | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	setRawExportMap(exportName, expression) { | 
					
						
							|  |  |  | 		if (!this._currentModule.rawExportMap) { | 
					
						
							|  |  |  | 			this._currentModule.rawExportMap = new Map(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (this._currentModule.rawExportMap.has(exportName)) { | 
					
						
							|  |  |  | 			this._currentModule.rawExportMap.set(exportName, expression); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 17:43:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} symbol identifier of the export in source code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	registerNamespaceExport(symbol) { | 
					
						
							|  |  |  | 		this._currentModule.namespaceExportSymbol = symbol; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-25 23:22:02 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} symbol identifier of the export in source code | 
					
						
							|  |  |  | 	 * @returns {boolean} registered success | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	registerUsedName(symbol) { | 
					
						
							|  |  |  | 		if (this.usedNames.has(symbol)) { | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		this.usedNames.add(symbol); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Module} module the referenced module | 
					
						
							|  |  |  | 	 * @param {Partial<ModuleReferenceOptions>} options options | 
					
						
							|  |  |  | 	 * @returns {string} the reference as identifier | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 	createModuleReference( | 
					
						
							|  |  |  | 		module, | 
					
						
							| 
									
										
										
										
											2025-07-02 20:02:03 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			ids = undefined, | 
					
						
							|  |  |  | 			call = false, | 
					
						
							|  |  |  | 			directImport = false, | 
					
						
							|  |  |  | 			deferredImport = false, | 
					
						
							|  |  |  | 			asiSafe = false | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 	) { | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 		const info = /** @type {ModuleInfo} */ (this._modulesMap.get(module)); | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 		const callFlag = call ? "_call" : ""; | 
					
						
							|  |  |  | 		const directImportFlag = directImport ? "_directImport" : ""; | 
					
						
							| 
									
										
										
										
											2025-07-02 20:02:03 +08:00
										 |  |  | 		const deferredImportFlag = deferredImport ? "_deferredImport" : ""; | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 		const asiSafeFlag = asiSafe | 
					
						
							|  |  |  | 			? "_asiSafe1" | 
					
						
							|  |  |  | 			: asiSafe === false | 
					
						
							| 
									
										
										
										
											2024-06-11 21:26:12 +08:00
										 |  |  | 				? "_asiSafe0" | 
					
						
							|  |  |  | 				: ""; | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 		const exportData = ids | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 			? Buffer.from(JSON.stringify(ids), "utf8").toString("hex") | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 			: "ns"; | 
					
						
							| 
									
										
										
										
											2020-09-30 04:12:42 +08:00
										 |  |  | 		// a "._" is appended to allow "delete ...", which would cause a SyntaxError in strict mode
 | 
					
						
							| 
									
										
										
										
											2025-07-02 20:02:03 +08:00
										 |  |  | 		return `__WEBPACK_MODULE_REFERENCE__${info.index}_${exportData}${callFlag}${directImportFlag}${deferredImportFlag}${asiSafeFlag}__._`; | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} name the identifier | 
					
						
							|  |  |  | 	 * @returns {boolean} true, when it's an module reference | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 	static isModuleReference(name) { | 
					
						
							|  |  |  | 		return MODULE_REFERENCE_REGEXP.test(name); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} name the identifier | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 	 * @returns {ModuleReferenceOptions & { index: number } | null} parsed options and index | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	static matchModuleReference(name) { | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 		const match = MODULE_REFERENCE_REGEXP.exec(name); | 
					
						
							|  |  |  | 		if (!match) return null; | 
					
						
							| 
									
										
										
										
											2024-07-31 11:11:11 +08:00
										 |  |  | 		const index = Number(match[1]); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:02:03 +08:00
										 |  |  | 		const asiSafe = match[6]; | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 		return { | 
					
						
							|  |  |  | 			index, | 
					
						
							|  |  |  | 			ids: | 
					
						
							|  |  |  | 				match[2] === "ns" | 
					
						
							|  |  |  | 					? [] | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 					: JSON.parse(Buffer.from(match[2], "hex").toString("utf8")), | 
					
						
							| 
									
										
										
										
											2024-07-31 11:11:11 +08:00
										 |  |  | 			call: Boolean(match[3]), | 
					
						
							|  |  |  | 			directImport: Boolean(match[4]), | 
					
						
							| 
									
										
										
										
											2025-07-02 20:02:03 +08:00
										 |  |  | 			deferredImport: Boolean(match[5]), | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 			asiSafe: asiSafe ? asiSafe === "1" : undefined | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | ConcatenationScope.DEFAULT_EXPORT = DEFAULT_EXPORT; | 
					
						
							|  |  |  | ConcatenationScope.NAMESPACE_OBJECT_EXPORT = NAMESPACE_OBJECT_EXPORT; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | module.exports = ConcatenationScope; |