Skip to content

Commit

Permalink
allow to mangle exports of module externals
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Sep 3, 2021
1 parent 4e8a621 commit b50d033
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/optimize/MangleExportsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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);
Expand All @@ -73,7 +88,7 @@ const mangleExportsInfo = (deterministic, exportsInfo) => {
used === UsageState.OnlyPropertiesUsed ||
used === UsageState.Unused
) {
mangleExportsInfo(deterministic, exportInfo.exportsInfo);
mangleExportsInfo(deterministic, exportInfo.exportsInfo, false);
}
}
}
Expand Down Expand Up @@ -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);
}
}
);
Expand Down

0 comments on commit b50d033

Please sign in to comment.