Skip to content

Commit

Permalink
fix: destructing assignment of dynamic import json file
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 29, 2023
2 parents 48a9ecc + 98cde0b commit 4d9152b
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 6 deletions.
32 changes: 26 additions & 6 deletions lib/dependencies/ImportDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,32 @@ class ImportDependency extends ModuleDependency {
* @returns {(string[] | ReferencedExport)[]} referenced exports
*/
getReferencedExports(moduleGraph, runtime) {
return this.referencedExports
? this.referencedExports.map(e => ({
name: e,
canMangle: false
}))
: Dependency.EXPORTS_OBJECT_REFERENCED;
if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
const refs = [];
for (const referencedExport of this.referencedExports) {
if (referencedExport[0] === "default") {
const selfModule = moduleGraph.getParentModule(this);
const importedModule =
/** @type {Module} */
(moduleGraph.getModule(this));
const exportsType = importedModule.getExportsType(
moduleGraph,
/** @type {BuildMeta} */
(selfModule.buildMeta).strictHarmonyModule
);
if (
exportsType === "default-only" ||
exportsType === "default-with-named"
) {
return Dependency.EXPORTS_OBJECT_REFERENCED;
}
}
refs.push({
name: referencedExport,
canMangle: false
});
}
return refs;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/cases/chunks/destructuring-assignment/dir2/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.a = 1;
exports.b = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a": 1}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"a"
11 changes: 11 additions & 0 deletions test/cases/chunks/destructuring-assignment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ it("should get warning on using 'webpackExports' with destructuring assignment",
expect(def).toBe(3);
done();
});

it("should not tree-shake default export for exportsType=default module", async () => {
const { default: object } = await import("./dir2/json/object.json");
const { default: array } = await import("./dir2/json/array.json");
const { default: primitive } = await import("./dir2/json/primitive.json");
expect(object).toEqual({ a: 1 });
expect(array).toEqual(["a"]);
expect(primitive).toBe("a");
const { default: a } = await import("./dir2/a");
expect(a).toEqual({ a: 1, b: 2 });
});
2 changes: 2 additions & 0 deletions test/cases/chunks/inline-options/dir15/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.a = 1;
exports.b = 2;
1 change: 1 addition & 0 deletions test/cases/chunks/inline-options/dir15/json/array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a"]
1 change: 1 addition & 0 deletions test/cases/chunks/inline-options/dir15/json/object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a": 1}
1 change: 1 addition & 0 deletions test/cases/chunks/inline-options/dir15/json/primitive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"a"
16 changes: 16 additions & 0 deletions test/cases/chunks/inline-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,29 @@ if (process.env.NODE_ENV === "production") {
}
);
});

it("should be able to load with webpackFetchPriorty high, low and auto", function () {
return Promise.all([
import(/* webpackFetchPriority: "high"*/ "./dir14/a"),
import(/* webpackFetchPriority: "low"*/ "./dir14/b"),
import(/* webpackFetchPriority: "auto"*/ "./dir14/c"),
])
})

it("should not tree-shake default export for exportsType=default module", async function () {
const jsonObject = await import(/* webpackExports: ["default"] */ "./dir15/json/object.json");
const jsonArray = await import(/* webpackExports: ["default"] */ "./dir15/json/array.json");
const jsonPrimitive = await import(/* webpackExports: ["default"] */ "./dir15/json/primitive.json");
expect(jsonObject.default).toEqual({ a: 1 });
expect(jsonObject.a).toEqual(1);
expect(jsonArray.default).toEqual(["a"]);
expect(jsonArray[0]).toBe("a");
expect(jsonPrimitive.default).toBe("a");
const a = await import(/* webpackExports: ["default"] */"./dir15/a");
expect(a.default).toEqual({ a: 1, b: 2 });
expect(a.a).toBe(1);
expect(a.b).toBe(2);
})
}

function testChunkLoading(load, expectedSyncInitial, expectedSyncRequested) {
Expand Down

0 comments on commit 4d9152b

Please sign in to comment.