Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b9dc06

Browse files
committed
fix HarmonyEvaluatedImportSpecifierDependency
1 parent 6eff5de commit 9b9dc06

File tree

7 files changed

+59
-2
lines changed

7 files changed

+59
-2
lines changed

lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,43 @@ HarmonyEvaluatedImportSpecifierDependency.Template = class HarmonyEvaluatedImpor
6161
const dep = /** @type {HarmonyEvaluatedImportSpecifierDependency} */ (
6262
dependency
6363
);
64-
const { moduleGraph, runtime } = templateContext;
64+
const { module, moduleGraph, runtime } = templateContext;
6565
const connection = moduleGraph.getConnection(dep);
6666
// Skip rendering depending when dependency is conditional
6767
if (connection && !connection.isTargetActive(runtime)) return;
6868

6969
const exportsInfo = moduleGraph.getExportsInfo(connection.module);
7070
const ids = dep.getIds(moduleGraph);
71-
const value = exportsInfo.isExportProvided(ids);
71+
72+
let value;
73+
74+
const exportsType = connection.module.getExportsType(
75+
moduleGraph,
76+
module.buildMeta.strictHarmonyModule
77+
);
78+
switch (exportsType) {
79+
case "default-with-named": {
80+
value = exportsInfo.isExportProvided(
81+
ids[0] === "default" ? ids.slice(1) : ids
82+
);
83+
break;
84+
}
85+
case "namespace": {
86+
if (ids[0] === "__esModule") {
87+
value = ids.length === 1 || undefined;
88+
} else {
89+
value = exportsInfo.isExportProvided(ids);
90+
}
91+
break;
92+
}
93+
case "dynamic": {
94+
if (ids[0] !== "default") {
95+
value = exportsInfo.isExportProvided(ids.slice(1));
96+
}
97+
break;
98+
}
99+
// default-only could lead to runtime error, when default value is primitive
100+
}
72101

73102
if (typeof value === "boolean") {
74103
source.replace(dep.range[0], dep.range[1] - 1, `${value}`);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.a = 2;
2+
exports.b = 3;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./cjs1");

test/cases/parsing/harmony-export-import-specifier/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import { usedE1, usedE2 } from "./e.js";
77
import { h } from "./h.js";
88
import * as m from "./m";
99
import {object as obj} from "./m";
10+
import cjs from "./cjs2";
1011
import * as o from "./o";
1112
import * as p from "./p";
1213
import * as q from "./q";
1314
import * as so from "./side-effect-free/o";
1415
import * as sm from "./side-effect-free/m";
16+
import json1 from "./some.json";
17+
import json2 from "./some1.json";
18+
import weirdCjs from "./weird-cjs";
1519

1620
it("namespace export as from commonjs should override named export", function () {
1721
expect(x).toBe(1);
@@ -48,6 +52,16 @@ it("should handle 'm in n' case", () => {
4852
expect(obj.aaa).toBe(true);
4953
expect("not_here" in m.object).toBe(false);
5054
expect("not_here" in obj).toBe(false);
55+
expect("__esModule" in q).toBe(true);
56+
expect(() => "value" in q.__esModule).toThrow();
57+
expect(() => "not_here" in json1).toThrow();
58+
expect("not_here" in json2).toBe(false);
59+
expect("a" in json2).toBe(true);
60+
expect("a" in cjs).toBe(true);
61+
expect("not_here" in cjs).toBe(false);
62+
expect("not_here" in weirdCjs).toBe(false);
63+
expect("a" in weirdCjs).toBe(true);
64+
expect(() => "a" in weirdCjs.a).toThrow();
5165
expect("aaa" in o).toBe(true);
5266
expect("aaa" in p).toBe(false);
5367
expect("ccc" in m).toBe(false);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"a": 1
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exports.__esModule = false;
2+
3+
function dynamic(exports) {
4+
exports.a = 1;
5+
}
6+
7+
dynamic(exports);

0 commit comments

Comments
 (0)