Skip to content

Commit fc7b34d

Browse files
committed
fix: should not mangle when destructuring a reexport
1 parent eaa685e commit fc7b34d

File tree

7 files changed

+51
-5
lines changed

7 files changed

+51
-5
lines changed

lib/dependencies/HarmonyImportSpecifierDependency.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
143143
*/
144144
getReferencedExports(moduleGraph, runtime) {
145145
let ids = this.getIds(moduleGraph);
146-
if (ids.length === 0) return this._getReferencedExportsInDestructuring();
146+
if (ids.length === 0)
147+
return this._getReferencedExportsInDestructuring(moduleGraph);
147148
let namespaceObjectAsContext = this.namespaceObjectAsContext;
148149
if (ids[0] === "default") {
149150
const selfModule = moduleGraph.getParentModule(this);
@@ -160,7 +161,7 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
160161
case "default-only":
161162
case "default-with-named":
162163
if (ids.length === 1)
163-
return this._getReferencedExportsInDestructuring();
164+
return this._getReferencedExportsInDestructuring(moduleGraph);
164165
ids = ids.slice(1);
165166
namespaceObjectAsContext = true;
166167
break;
@@ -178,21 +179,30 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
178179
ids = ids.slice(0, -1);
179180
}
180181

181-
return this._getReferencedExportsInDestructuring(ids);
182+
return this._getReferencedExportsInDestructuring(moduleGraph, ids);
182183
}
183184

184185
/**
186+
* @param {ModuleGraph} moduleGraph module graph
185187
* @param {string[]=} ids ids
186188
* @returns {(string[] | ReferencedExport)[]} referenced exports
187189
*/
188-
_getReferencedExportsInDestructuring(ids) {
190+
_getReferencedExportsInDestructuring(moduleGraph, ids) {
189191
if (this.referencedPropertiesInDestructuring) {
190192
/** @type {ReferencedExport[]} */
191193
const refs = [];
194+
const importedModule = moduleGraph.getModule(this);
195+
const canMangle =
196+
Array.isArray(ids) &&
197+
ids.length > 0 &&
198+
!moduleGraph
199+
.getExportsInfo(importedModule)
200+
.getExportInfo(ids[0])
201+
.isReexport();
192202
for (const key of this.referencedPropertiesInDestructuring) {
193203
refs.push({
194204
name: ids ? ids.concat([key]) : [key],
195-
canMangle: Array.isArray(ids) && ids.length > 0
205+
canMangle
196206
});
197207
}
198208
return refs;

test/configCases/mangle/mangle-with-destructuring-assignment/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as module from "./module";
2+
import { obj3, obj3CanMangle, obj4, obj4CanMangle } from "./reexport?side-effects" // enable side effects to ensure reexport is not skipped
23

34
it("should not mangle export when destructuring module", () => {
45
const { obj: { a, b }, objCanMangle } = module
@@ -19,3 +20,18 @@ it("should mangle export when using module dot property", () => {
1920
expect(module.aaa).toBe("aaa");
2021
expect(module.aaaCanMangle).toBe(true)
2122
});
23+
24+
it("should not mangle export when destructuring module's property is a module", () => {
25+
const { aaa, bbb } = obj3;
26+
expect(aaa).toBe("a");
27+
expect(bbb).toBe("b");
28+
expect(obj3CanMangle).toBe(false)
29+
});
30+
31+
it("should not mangle export when destructuring module's nested property is a module", () => {
32+
const { nested: { obj5, obj5CanMangle } } = obj4;
33+
expect(obj5.aaa).toBe("a");
34+
expect(obj5.bbb).toBe("b");
35+
expect(obj4CanMangle).toBe(true);
36+
expect(obj5CanMangle).toBe(false)
37+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const aaa = "a";
2+
export const bbb = "b";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const aaa = "a";
2+
export const bbb = "b";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * as obj3 from "./module2"
2+
export const obj3CanMangle = __webpack_exports_info__.obj3.canMangle;
3+
4+
import * as reexport2 from "./reexport2?side-effects"
5+
export const obj4 = { nested: reexport2 }
6+
export const obj4CanMangle = __webpack_exports_info__.reexport2.canMangle;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * as obj5 from "./module3"
2+
export const obj5CanMangle = __webpack_exports_info__.obj5.canMangle;

test/configCases/mangle/mangle-with-destructuring-assignment/webpack.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
/** @type {import("../../../../").Configuration} */
22
module.exports = {
3+
module: {
4+
rules: [
5+
{
6+
resourceQuery: /side-effects/,
7+
sideEffects: true
8+
}
9+
]
10+
},
311
optimization: {
412
mangleExports: true,
513
usedExports: true,

0 commit comments

Comments
 (0)