1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { UsageState } = require("../ExportsInfo");
|
---|
9 | const {
|
---|
10 | numberToIdentifier,
|
---|
11 | NUMBER_OF_IDENTIFIER_START_CHARS,
|
---|
12 | NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS
|
---|
13 | } = require("../Template");
|
---|
14 | const { assignDeterministicIds } = require("../ids/IdHelpers");
|
---|
15 | const { compareSelect, compareStringsNumeric } = require("../util/comparators");
|
---|
16 |
|
---|
17 | /** @typedef {import("../Compiler")} Compiler */
|
---|
18 | /** @typedef {import("../ExportsInfo")} ExportsInfo */
|
---|
19 | /** @typedef {import("../ExportsInfo").ExportInfo} ExportInfo */
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @param {ExportsInfo} exportsInfo exports info
|
---|
23 | * @returns {boolean} mangle is possible
|
---|
24 | */
|
---|
25 | const canMangle = exportsInfo => {
|
---|
26 | if (exportsInfo.otherExportsInfo.getUsed(undefined) !== UsageState.Unused)
|
---|
27 | return false;
|
---|
28 | let hasSomethingToMangle = false;
|
---|
29 | for (const exportInfo of exportsInfo.exports) {
|
---|
30 | if (exportInfo.canMangle === true) {
|
---|
31 | hasSomethingToMangle = true;
|
---|
32 | }
|
---|
33 | }
|
---|
34 | return hasSomethingToMangle;
|
---|
35 | };
|
---|
36 |
|
---|
37 | // Sort by name
|
---|
38 | const comparator = compareSelect(e => e.name, compareStringsNumeric);
|
---|
39 | /**
|
---|
40 | * @param {boolean} deterministic use deterministic names
|
---|
41 | * @param {ExportsInfo} exportsInfo exports info
|
---|
42 | * @returns {void}
|
---|
43 | */
|
---|
44 | const mangleExportsInfo = (deterministic, exportsInfo) => {
|
---|
45 | if (!canMangle(exportsInfo)) return;
|
---|
46 | const usedNames = new Set();
|
---|
47 | /** @type {ExportInfo[]} */
|
---|
48 | const mangleableExports = [];
|
---|
49 | for (const exportInfo of exportsInfo.ownedExports) {
|
---|
50 | const name = exportInfo.name;
|
---|
51 | if (!exportInfo.hasUsedName()) {
|
---|
52 | if (
|
---|
53 | // Can the export be mangled?
|
---|
54 | exportInfo.canMangle !== true ||
|
---|
55 | // Never rename 1 char exports
|
---|
56 | (name.length === 1 && /^[a-zA-Z0-9_$]/.test(name)) ||
|
---|
57 | // Don't rename 2 char exports in deterministic mode
|
---|
58 | (deterministic &&
|
---|
59 | name.length === 2 &&
|
---|
60 | /^[a-zA-Z_$][a-zA-Z0-9_$]|^[1-9][0-9]/.test(name)) ||
|
---|
61 | // Don't rename exports that are not provided
|
---|
62 | exportInfo.provided !== true
|
---|
63 | ) {
|
---|
64 | exportInfo.setUsedName(name);
|
---|
65 | usedNames.add(name);
|
---|
66 | } else {
|
---|
67 | mangleableExports.push(exportInfo);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | if (exportInfo.exportsInfoOwned) {
|
---|
71 | const used = exportInfo.getUsed(undefined);
|
---|
72 | if (
|
---|
73 | used === UsageState.OnlyPropertiesUsed ||
|
---|
74 | used === UsageState.Unused
|
---|
75 | ) {
|
---|
76 | mangleExportsInfo(deterministic, exportInfo.exportsInfo);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | if (deterministic) {
|
---|
81 | assignDeterministicIds(
|
---|
82 | mangleableExports,
|
---|
83 | e => e.name,
|
---|
84 | comparator,
|
---|
85 | (e, id) => {
|
---|
86 | const name = numberToIdentifier(id);
|
---|
87 | const size = usedNames.size;
|
---|
88 | usedNames.add(name);
|
---|
89 | if (size === usedNames.size) return false;
|
---|
90 | e.setUsedName(name);
|
---|
91 | return true;
|
---|
92 | },
|
---|
93 | [
|
---|
94 | NUMBER_OF_IDENTIFIER_START_CHARS,
|
---|
95 | NUMBER_OF_IDENTIFIER_START_CHARS *
|
---|
96 | NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS
|
---|
97 | ],
|
---|
98 | NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS,
|
---|
99 | usedNames.size
|
---|
100 | );
|
---|
101 | } else {
|
---|
102 | const usedExports = [];
|
---|
103 | const unusedExports = [];
|
---|
104 | for (const exportInfo of mangleableExports) {
|
---|
105 | if (exportInfo.getUsed(undefined) === UsageState.Unused) {
|
---|
106 | unusedExports.push(exportInfo);
|
---|
107 | } else {
|
---|
108 | usedExports.push(exportInfo);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | usedExports.sort(comparator);
|
---|
112 | unusedExports.sort(comparator);
|
---|
113 | let i = 0;
|
---|
114 | for (const list of [usedExports, unusedExports]) {
|
---|
115 | for (const exportInfo of list) {
|
---|
116 | let name;
|
---|
117 | do {
|
---|
118 | name = numberToIdentifier(i++);
|
---|
119 | } while (usedNames.has(name));
|
---|
120 | exportInfo.setUsedName(name);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | };
|
---|
125 |
|
---|
126 | class MangleExportsPlugin {
|
---|
127 | /**
|
---|
128 | * @param {boolean} deterministic use deterministic names
|
---|
129 | */
|
---|
130 | constructor(deterministic) {
|
---|
131 | this._deterministic = deterministic;
|
---|
132 | }
|
---|
133 | /**
|
---|
134 | * Apply the plugin
|
---|
135 | * @param {Compiler} compiler the compiler instance
|
---|
136 | * @returns {void}
|
---|
137 | */
|
---|
138 | apply(compiler) {
|
---|
139 | const { _deterministic: deterministic } = this;
|
---|
140 | compiler.hooks.compilation.tap("MangleExportsPlugin", compilation => {
|
---|
141 | const moduleGraph = compilation.moduleGraph;
|
---|
142 | compilation.hooks.optimizeCodeGeneration.tap(
|
---|
143 | "MangleExportsPlugin",
|
---|
144 | modules => {
|
---|
145 | for (const module of modules) {
|
---|
146 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
---|
147 | mangleExportsInfo(deterministic, exportsInfo);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | );
|
---|
151 | });
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | module.exports = MangleExportsPlugin;
|
---|