fix: mangle destructuring default in namespace import

This commit is contained in:
Alexander Akait 2024-06-21 16:45:48 +03:00 committed by GitHub
commit e38e2bc31b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 3 deletions

View File

@ -344,13 +344,13 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
}
if (dep.referencedPropertiesInDestructuring) {
const prefixedIds = ids[0] === "default" ? ids.slice(1) : ids;
for (let {
id,
shorthand,
range
} of dep.referencedPropertiesInDestructuring) {
const concatedIds = ids.concat([id]);
if (concatedIds[0] === "default") concatedIds.shift();
const concatedIds = prefixedIds.concat([id]);
const module = moduleGraph.getModule(dep);
const used = moduleGraph
.getExportsInfo(module)

View File

@ -1,8 +1,8 @@
import path from "path";
import * as module from "./module";
import { obj3, obj3CanMangle, obj4, obj4CanMangle } from "./reexport?side-effects" // enable side effects to ensure reexport is not skipped
import data from "./data.json";
import data2 from "./data.json?2";
import path from "path";
it("should mangle export when destructuring module", () => {
const { obj: { a, b }, objCanMangle } = module
@ -39,6 +39,12 @@ it("should not mangle export when destructuring module's nested property is a mo
expect(obj5CanMangle).toBe(false); // obj5 is used in unknown way
});
it("should mangle default in namespace import", async () => {
const { default: foo, defaultCanMangle } = module;
expect(foo).toBe("default");
expect(defaultCanMangle).toBe(true);
});
it("should mangle when destructuring json", async () => {
const { obj: {
"arr": [

View File

@ -6,3 +6,6 @@ export const objCanMangle = __webpack_exports_info__.obj.canMangle;
export const obj2 = { a: "a", b: "b" }
export const obj2CanMangle = __webpack_exports_info__.obj2.canMangle;
export default "default";
export const defaultCanMangle = __webpack_exports_info__.default.canMangle;