| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | const { UsageState } = require("../ExportsInfo"); | 
					
						
							| 
									
										
										
										
											2019-12-06 14:09:43 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	numberToIdentifier, | 
					
						
							|  |  |  | 	NUMBER_OF_IDENTIFIER_START_CHARS, | 
					
						
							|  |  |  | 	NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS | 
					
						
							|  |  |  | } = require("../Template"); | 
					
						
							| 
									
										
										
										
											2019-02-20 18:32:12 +08:00
										 |  |  | const { assignDeterministicIds } = require("../ids/IdHelpers"); | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | const { compareSelect, compareStringsNumeric } = require("../util/comparators"); | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | /** @typedef {import("../ExportsInfo")} ExportsInfo */ | 
					
						
							| 
									
										
										
										
											2020-07-31 18:13:30 +08:00
										 |  |  | /** @typedef {import("../ExportsInfo").ExportInfo} ExportInfo */ | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {ExportsInfo} exportsInfo exports info | 
					
						
							|  |  |  |  * @returns {boolean} mangle is possible | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-12-22 03:26:41 +08:00
										 |  |  | const canMangle = exportsInfo => { | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 	if (exportsInfo.otherExportsInfo.getUsed(undefined) !== UsageState.Unused) | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 	let hasSomethingToMangle = false; | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 	for (const exportInfo of exportsInfo.exports) { | 
					
						
							|  |  |  | 		if (exportInfo.canMangle === true) { | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 			hasSomethingToMangle = true; | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 	return hasSomethingToMangle; | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | // Sort by name
 | 
					
						
							|  |  |  | const comparator = compareSelect(e => e.name, compareStringsNumeric); | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-07-31 18:13:30 +08:00
										 |  |  |  * @param {boolean} deterministic use deterministic names | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  |  * @param {ExportsInfo} exportsInfo exports info | 
					
						
							| 
									
										
										
										
											2021-09-03 18:04:51 +08:00
										 |  |  |  * @param {boolean} isNamespace is namespace object | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  |  * @returns {void} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-09-03 18:04:51 +08:00
										 |  |  | const mangleExportsInfo = (deterministic, exportsInfo, isNamespace) => { | 
					
						
							| 
									
										
										
										
											2019-12-22 03:26:41 +08:00
										 |  |  | 	if (!canMangle(exportsInfo)) return; | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 	const usedNames = new Set(); | 
					
						
							| 
									
										
										
										
											2020-07-31 18:13:30 +08:00
										 |  |  | 	/** @type {ExportInfo[]} */ | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 	const mangleableExports = []; | 
					
						
							| 
									
										
										
										
											2021-09-03 18:04:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Avoid to renamed exports that are not provided when
 | 
					
						
							|  |  |  | 	// 1. it's not a namespace export: non-provided exports can be found in prototype chain
 | 
					
						
							|  |  |  | 	// 2. there are other provided exports and deterministic mode is chosen:
 | 
					
						
							|  |  |  | 	//    non-provided exports would break the determinism
 | 
					
						
							|  |  |  | 	let avoidMangleNonProvided = !isNamespace; | 
					
						
							|  |  |  | 	if (!avoidMangleNonProvided && deterministic) { | 
					
						
							|  |  |  | 		for (const exportInfo of exportsInfo.ownedExports) { | 
					
						
							|  |  |  | 			if (exportInfo.provided !== false) { | 
					
						
							|  |  |  | 				avoidMangleNonProvided = true; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-05 04:05:17 +08:00
										 |  |  | 	for (const exportInfo of exportsInfo.ownedExports) { | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		const name = exportInfo.name; | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 		if (!exportInfo.hasUsedName()) { | 
					
						
							| 
									
										
										
										
											2019-11-05 04:52:54 +08:00
										 |  |  | 			if ( | 
					
						
							| 
									
										
										
										
											2020-08-18 03:32:47 +08:00
										 |  |  | 				// Can the export be mangled?
 | 
					
						
							| 
									
										
										
										
											2019-11-05 04:52:54 +08:00
										 |  |  | 				exportInfo.canMangle !== true || | 
					
						
							| 
									
										
										
										
											2020-08-18 03:32:47 +08:00
										 |  |  | 				// Never rename 1 char exports
 | 
					
						
							| 
									
										
										
										
											2019-11-05 04:52:54 +08:00
										 |  |  | 				(name.length === 1 && /^[a-zA-Z0-9_$]/.test(name)) || | 
					
						
							| 
									
										
										
										
											2020-08-18 03:32:47 +08:00
										 |  |  | 				// Don't rename 2 char exports in deterministic mode
 | 
					
						
							| 
									
										
										
										
											2020-07-31 18:13:30 +08:00
										 |  |  | 				(deterministic && | 
					
						
							|  |  |  | 					name.length === 2 && | 
					
						
							| 
									
										
										
										
											2019-12-22 03:26:41 +08:00
										 |  |  | 					/^[a-zA-Z_$][a-zA-Z0-9_$]|^[1-9][0-9]/.test(name)) || | 
					
						
							| 
									
										
										
										
											2020-10-06 18:04:43 +08:00
										 |  |  | 				// Don't rename exports that are not provided
 | 
					
						
							| 
									
										
										
										
											2021-09-03 18:04:51 +08:00
										 |  |  | 				(avoidMangleNonProvided && exportInfo.provided !== true) | 
					
						
							| 
									
										
										
										
											2019-11-05 04:52:54 +08:00
										 |  |  | 			) { | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 				exportInfo.setUsedName(name); | 
					
						
							| 
									
										
										
										
											2019-11-05 04:52:54 +08:00
										 |  |  | 				usedNames.add(name); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				mangleableExports.push(exportInfo); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 		if (exportInfo.exportsInfoOwned) { | 
					
						
							|  |  |  | 			const used = exportInfo.getUsed(undefined); | 
					
						
							|  |  |  | 			if ( | 
					
						
							|  |  |  | 				used === UsageState.OnlyPropertiesUsed || | 
					
						
							|  |  |  | 				used === UsageState.Unused | 
					
						
							|  |  |  | 			) { | 
					
						
							| 
									
										
										
										
											2021-09-03 18:04:51 +08:00
										 |  |  | 				mangleExportsInfo(deterministic, exportInfo.exportsInfo, false); | 
					
						
							| 
									
										
										
										
											2020-07-31 18:13:30 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (deterministic) { | 
					
						
							|  |  |  | 		assignDeterministicIds( | 
					
						
							|  |  |  | 			mangleableExports, | 
					
						
							|  |  |  | 			e => e.name, | 
					
						
							|  |  |  | 			comparator, | 
					
						
							|  |  |  | 			(e, id) => { | 
					
						
							|  |  |  | 				const name = numberToIdentifier(id); | 
					
						
							|  |  |  | 				const size = usedNames.size; | 
					
						
							|  |  |  | 				usedNames.add(name); | 
					
						
							|  |  |  | 				if (size === usedNames.size) return false; | 
					
						
							|  |  |  | 				e.setUsedName(name); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			[ | 
					
						
							|  |  |  | 				NUMBER_OF_IDENTIFIER_START_CHARS, | 
					
						
							|  |  |  | 				NUMBER_OF_IDENTIFIER_START_CHARS * | 
					
						
							|  |  |  | 					NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS | 
					
						
							|  |  |  | 			], | 
					
						
							|  |  |  | 			NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS, | 
					
						
							|  |  |  | 			usedNames.size | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		const usedExports = []; | 
					
						
							|  |  |  | 		const unusedExports = []; | 
					
						
							|  |  |  | 		for (const exportInfo of mangleableExports) { | 
					
						
							|  |  |  | 			if (exportInfo.getUsed(undefined) === UsageState.Unused) { | 
					
						
							|  |  |  | 				unusedExports.push(exportInfo); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				usedExports.push(exportInfo); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		usedExports.sort(comparator); | 
					
						
							|  |  |  | 		unusedExports.sort(comparator); | 
					
						
							|  |  |  | 		let i = 0; | 
					
						
							|  |  |  | 		for (const list of [usedExports, unusedExports]) { | 
					
						
							|  |  |  | 			for (const exportInfo of list) { | 
					
						
							|  |  |  | 				let name; | 
					
						
							|  |  |  | 				do { | 
					
						
							|  |  |  | 					name = numberToIdentifier(i++); | 
					
						
							|  |  |  | 				} while (usedNames.has(name)); | 
					
						
							|  |  |  | 				exportInfo.setUsedName(name); | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | class MangleExportsPlugin { | 
					
						
							| 
									
										
										
										
											2020-07-31 18:13:30 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {boolean} deterministic use deterministic names | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(deterministic) { | 
					
						
							|  |  |  | 		this._deterministic = deterministic; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2020-07-31 18:13:30 +08:00
										 |  |  | 		const { _deterministic: deterministic } = this; | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("MangleExportsPlugin", compilation => { | 
					
						
							|  |  |  | 			const moduleGraph = compilation.moduleGraph; | 
					
						
							|  |  |  | 			compilation.hooks.optimizeCodeGeneration.tap( | 
					
						
							|  |  |  | 				"MangleExportsPlugin", | 
					
						
							|  |  |  | 				modules => { | 
					
						
							| 
									
										
										
										
											2021-10-05 19:08:21 +08:00
										 |  |  | 					if (compilation.moduleMemCaches) { | 
					
						
							|  |  |  | 						throw new Error( | 
					
						
							|  |  |  | 							"optimization.mangleExports can't be used with cacheUnaffected as export mangling is a global effect" | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 					for (const module of modules) { | 
					
						
							| 
									
										
										
										
											2021-09-03 18:04:51 +08:00
										 |  |  | 						const isNamespace = | 
					
						
							|  |  |  | 							module.buildMeta && module.buildMeta.exportsType === "namespace"; | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 						const exportsInfo = moduleGraph.getExportsInfo(module); | 
					
						
							| 
									
										
										
										
											2021-09-03 18:04:51 +08:00
										 |  |  | 						mangleExportsInfo(deterministic, exportsInfo, isNamespace); | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = MangleExportsPlugin; |