| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | /** @typedef {import("./Module")} Module */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | const MODULE_REFERENCE_REGEXP = | 
					
						
							|  |  |  | 	/^__WEBPACK_MODULE_REFERENCE__(\d+)_([\da-f]+|ns)(_call)?(_directImport)?(?:_asiSafe(\d))?__$/; | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const DEFAULT_EXPORT = "__WEBPACK_DEFAULT_EXPORT__"; | 
					
						
							|  |  |  | const NAMESPACE_OBJECT_EXPORT = "__WEBPACK_NAMESPACE_OBJECT__"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} ExternalModuleInfo | 
					
						
							|  |  |  |  * @property {number} index | 
					
						
							|  |  |  |  * @property {Module} module | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} ConcatenatedModuleInfo | 
					
						
							|  |  |  |  * @property {number} index | 
					
						
							|  |  |  |  * @property {Module} module | 
					
						
							|  |  |  |  * @property {Map<string, string>} exportMap mapping from export name to symbol | 
					
						
							| 
									
										
										
										
											2020-09-15 18:21:37 +08:00
										 |  |  |  * @property {Map<string, string>} rawExportMap mapping from export name to symbol | 
					
						
							| 
									
										
										
										
											2020-09-15 17:43:06 +08:00
										 |  |  |  * @property {string=} namespaceExportSymbol | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo} ModuleInfo */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} ModuleReferenceOptions | 
					
						
							|  |  |  |  * @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) | 
					
						
							|  |  |  |  * @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 | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-10-30 00:53:56 +08:00
										 |  |  | 	constructor(modulesMap, currentModule) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 17:43:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} symbol identifier of the export in source code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	registerNamespaceExport(symbol) { | 
					
						
							|  |  |  | 		this._currentModule.namespaceExportSymbol = symbol; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 		{ ids = undefined, call = false, directImport = 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" : ""; | 
					
						
							|  |  |  | 		const asiSafeFlag = asiSafe | 
					
						
							|  |  |  | 			? "_asiSafe1" | 
					
						
							|  |  |  | 			: asiSafe === false | 
					
						
							|  |  |  | 			? "_asiSafe0" | 
					
						
							|  |  |  | 			: ""; | 
					
						
							|  |  |  | 		const exportData = ids | 
					
						
							|  |  |  | 			? Buffer.from(JSON.stringify(ids), "utf-8").toString("hex") | 
					
						
							|  |  |  | 			: "ns"; | 
					
						
							| 
									
										
										
										
											2020-09-30 04:12:42 +08:00
										 |  |  | 		// a "._" is appended to allow "delete ...", which would cause a SyntaxError in strict mode
 | 
					
						
							|  |  |  | 		return `__WEBPACK_MODULE_REFERENCE__${info.index}_${exportData}${callFlag}${directImportFlag}${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; | 
					
						
							|  |  |  | 		const index = +match[1]; | 
					
						
							| 
									
										
										
										
											2020-09-15 16:56:06 +08:00
										 |  |  | 		const asiSafe = match[5]; | 
					
						
							| 
									
										
										
										
											2020-09-11 15:06:24 +08:00
										 |  |  | 		return { | 
					
						
							|  |  |  | 			index, | 
					
						
							|  |  |  | 			ids: | 
					
						
							|  |  |  | 				match[2] === "ns" | 
					
						
							|  |  |  | 					? [] | 
					
						
							|  |  |  | 					: JSON.parse(Buffer.from(match[2], "hex").toString("utf-8")), | 
					
						
							|  |  |  | 			call: !!match[3], | 
					
						
							|  |  |  | 			directImport: !!match[4], | 
					
						
							|  |  |  | 			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; |