Skip to content

Commit

Permalink
fix: should not mangle when destructuring a reexport
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Feb 18, 2024
1 parent eaa685e commit fc7b34d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/dependencies/HarmonyImportSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
*/
getReferencedExports(moduleGraph, runtime) {
let ids = this.getIds(moduleGraph);
if (ids.length === 0) return this._getReferencedExportsInDestructuring();
if (ids.length === 0)
return this._getReferencedExportsInDestructuring(moduleGraph);
let namespaceObjectAsContext = this.namespaceObjectAsContext;
if (ids[0] === "default") {
const selfModule = moduleGraph.getParentModule(this);
Expand All @@ -160,7 +161,7 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
case "default-only":
case "default-with-named":
if (ids.length === 1)
return this._getReferencedExportsInDestructuring();
return this._getReferencedExportsInDestructuring(moduleGraph);
ids = ids.slice(1);
namespaceObjectAsContext = true;
break;
Expand All @@ -178,21 +179,30 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
ids = ids.slice(0, -1);
}

return this._getReferencedExportsInDestructuring(ids);
return this._getReferencedExportsInDestructuring(moduleGraph, ids);
}

/**
* @param {ModuleGraph} moduleGraph module graph
* @param {string[]=} ids ids
* @returns {(string[] | ReferencedExport)[]} referenced exports
*/
_getReferencedExportsInDestructuring(ids) {
_getReferencedExportsInDestructuring(moduleGraph, ids) {
if (this.referencedPropertiesInDestructuring) {
/** @type {ReferencedExport[]} */
const refs = [];
const importedModule = moduleGraph.getModule(this);
const canMangle =
Array.isArray(ids) &&
ids.length > 0 &&
!moduleGraph
.getExportsInfo(importedModule)
.getExportInfo(ids[0])
.isReexport();
for (const key of this.referencedPropertiesInDestructuring) {
refs.push({
name: ids ? ids.concat([key]) : [key],
canMangle: Array.isArray(ids) && ids.length > 0
canMangle
});
}
return refs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as module from "./module";
import { obj3, obj3CanMangle, obj4, obj4CanMangle } from "./reexport?side-effects" // enable side effects to ensure reexport is not skipped

it("should not mangle export when destructuring module", () => {
const { obj: { a, b }, objCanMangle } = module
Expand All @@ -19,3 +20,18 @@ it("should mangle export when using module dot property", () => {
expect(module.aaa).toBe("aaa");
expect(module.aaaCanMangle).toBe(true)
});

it("should not mangle export when destructuring module's property is a module", () => {
const { aaa, bbb } = obj3;
expect(aaa).toBe("a");
expect(bbb).toBe("b");
expect(obj3CanMangle).toBe(false)
});

it("should not mangle export when destructuring module's nested property is a module", () => {
const { nested: { obj5, obj5CanMangle } } = obj4;
expect(obj5.aaa).toBe("a");
expect(obj5.bbb).toBe("b");
expect(obj4CanMangle).toBe(true);
expect(obj5CanMangle).toBe(false)
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const aaa = "a";
export const bbb = "b";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const aaa = "a";
export const bbb = "b";
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * as obj3 from "./module2"
export const obj3CanMangle = __webpack_exports_info__.obj3.canMangle;

import * as reexport2 from "./reexport2?side-effects"
export const obj4 = { nested: reexport2 }
export const obj4CanMangle = __webpack_exports_info__.reexport2.canMangle;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as obj5 from "./module3"
export const obj5CanMangle = __webpack_exports_info__.obj5.canMangle;
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
module: {
rules: [
{
resourceQuery: /side-effects/,
sideEffects: true
}
]
},
optimization: {
mangleExports: true,
usedExports: true,
Expand Down

0 comments on commit fc7b34d

Please sign in to comment.