| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | const { UsageState } = require("../ModuleGraph"); | 
					
						
							| 
									
										
										
										
											2019-04-13 00:29:41 +08:00
										 |  |  | const { numberToIdentifier } = require("../Template"); | 
					
						
							| 
									
										
										
										
											2019-02-20 18:32:12 +08:00
										 |  |  | const { assignDeterministicIds } = require("../ids/IdHelpers"); | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	concatComparators, | 
					
						
							|  |  |  | 	compareSelect, | 
					
						
							|  |  |  | 	compareStringsNumeric | 
					
						
							|  |  |  | } = require("../util/comparators"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | /** @typedef {import("../ModuleGraph").ExportsInfo} ExportsInfo */ | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | const OBJECT = []; | 
					
						
							|  |  |  | const ARRAY = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {ExportsInfo} exportsInfo exports info | 
					
						
							|  |  |  |  * @param {boolean} canBeArray can be exports info point to an array | 
					
						
							|  |  |  |  * @returns {boolean} mangle is possible | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const canMangle = (exportsInfo, canBeArray) => { | 
					
						
							|  |  |  | 	if (exportsInfo.otherExportsInfo.used !== UsageState.Unused) return false; | 
					
						
							|  |  |  | 	let hasSomethingToMangle = false; | 
					
						
							|  |  |  | 	const empty = canBeArray ? ARRAY : OBJECT; | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 	for (const exportInfo of exportsInfo.exports) { | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		if (exportInfo.name in empty) { | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 		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
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const comparator = concatComparators( | 
					
						
							|  |  |  | 	// Sort used before unused fields
 | 
					
						
							| 
									
										
										
										
											2019-11-15 17:20:45 +08:00
										 |  |  | 	compareSelect( | 
					
						
							|  |  |  | 		e => e.used !== false, | 
					
						
							|  |  |  | 		(a, b) => (a === b ? 0 : a ? -1 : 1) | 
					
						
							|  |  |  | 	), | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 	// Sort by name
 | 
					
						
							|  |  |  | 	compareSelect(e => e.name, compareStringsNumeric) | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {ExportsInfo} exportsInfo exports info | 
					
						
							|  |  |  |  * @param {boolean} canBeArray can be exports info point to an array | 
					
						
							|  |  |  |  * @returns {void} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const mangleExportsInfo = (exportsInfo, canBeArray) => { | 
					
						
							|  |  |  | 	if (!canMangle(exportsInfo, canBeArray)) return; | 
					
						
							|  |  |  | 	const usedNames = new Set(); | 
					
						
							|  |  |  | 	const mangleableExports = []; | 
					
						
							|  |  |  | 	// Don't rename 1-2 char exports or exports that can't be mangled
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2019-11-05 04:52:54 +08:00
										 |  |  | 		if (typeof exportInfo.usedName !== "string") { | 
					
						
							|  |  |  | 			if ( | 
					
						
							|  |  |  | 				exportInfo.canMangle !== true || | 
					
						
							|  |  |  | 				(name.length === 1 && /^[a-zA-Z0-9_$]/.test(name)) || | 
					
						
							|  |  |  | 				(name.length === 2 && /^[a-zA-Z_$][a-zA-Z0-9_$]|^[1-9][0-9]/.test(name)) | 
					
						
							|  |  |  | 			) { | 
					
						
							|  |  |  | 				exportInfo.usedName = name; | 
					
						
							|  |  |  | 				usedNames.add(name); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				mangleableExports.push(exportInfo); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if ( | 
					
						
							|  |  |  | 			exportInfo.exportsInfoOwned && | 
					
						
							|  |  |  | 			exportInfo.used === UsageState.OnlyPropertiesUsed | 
					
						
							|  |  |  | 		) { | 
					
						
							|  |  |  | 			mangleExportsInfo(exportInfo.exportsInfo, true); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	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.usedName = name; | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		[26, 52], | 
					
						
							|  |  |  | 		52, | 
					
						
							|  |  |  | 		usedNames.size | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | class MangleExportsPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler webpack compiler | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap("MangleExportsPlugin", compilation => { | 
					
						
							|  |  |  | 			const moduleGraph = compilation.moduleGraph; | 
					
						
							|  |  |  | 			compilation.hooks.optimizeCodeGeneration.tap( | 
					
						
							|  |  |  | 				"MangleExportsPlugin", | 
					
						
							|  |  |  | 				modules => { | 
					
						
							|  |  |  | 					for (const module of modules) { | 
					
						
							|  |  |  | 						const exportsInfo = moduleGraph.getExportsInfo(module); | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 						mangleExportsInfo(exportsInfo, false); | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = MangleExportsPlugin; |