allow to mangle exports of module externals

This commit is contained in:
Tobias Koppers 2021-09-03 12:04:51 +02:00
parent 4e8a621bfa
commit b50d033960
1 changed files with 21 additions and 4 deletions

View File

@ -39,13 +39,28 @@ const comparator = compareSelect(e => e.name, compareStringsNumeric);
/**
* @param {boolean} deterministic use deterministic names
* @param {ExportsInfo} exportsInfo exports info
* @param {boolean} isNamespace is namespace object
* @returns {void}
*/
const mangleExportsInfo = (deterministic, exportsInfo) => {
const mangleExportsInfo = (deterministic, exportsInfo, isNamespace) => {
if (!canMangle(exportsInfo)) return;
const usedNames = new Set();
/** @type {ExportInfo[]} */
const mangleableExports = [];
// 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;
}
}
}
for (const exportInfo of exportsInfo.ownedExports) {
const name = exportInfo.name;
if (!exportInfo.hasUsedName()) {
@ -59,7 +74,7 @@ const mangleExportsInfo = (deterministic, exportsInfo) => {
name.length === 2 &&
/^[a-zA-Z_$][a-zA-Z0-9_$]|^[1-9][0-9]/.test(name)) ||
// Don't rename exports that are not provided
exportInfo.provided !== true
(avoidMangleNonProvided && exportInfo.provided !== true)
) {
exportInfo.setUsedName(name);
usedNames.add(name);
@ -73,7 +88,7 @@ const mangleExportsInfo = (deterministic, exportsInfo) => {
used === UsageState.OnlyPropertiesUsed ||
used === UsageState.Unused
) {
mangleExportsInfo(deterministic, exportInfo.exportsInfo);
mangleExportsInfo(deterministic, exportInfo.exportsInfo, false);
}
}
}
@ -143,8 +158,10 @@ class MangleExportsPlugin {
"MangleExportsPlugin",
modules => {
for (const module of modules) {
const isNamespace =
module.buildMeta && module.buildMeta.exportsType === "namespace";
const exportsInfo = moduleGraph.getExportsInfo(module);
mangleExportsInfo(deterministic, exportsInfo);
mangleExportsInfo(deterministic, exportsInfo, isNamespace);
}
}
);