Skip to content

Commit

Permalink
fix: interop modules with primitive default export (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Nov 29, 2024
1 parent 58d3f5f commit cca319b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"estree-walker": "^3.0.3",
"etag": "^1.8.1",
"fast-glob": "^3.3.2",
"is-installed-globally": "^1.0.0",
"mime": "^4.0.4",
"mlly": "^1.7.3",
"moment-timezone": "^0.5.46",
Expand Down
32 changes: 32 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ function interopDefault(mod: any): any {

const def = mod.default;
const defType = typeof def;
if (def === null || (defType !== "object" && defType !== "function")) {
if (def === null || def === undefined) {
return mod;
}
const defIsObj = defType === "object" || defType === "function";

return new Proxy(mod, {
get(target, prop, receiver) {
Expand All @@ -121,11 +122,13 @@ function interopDefault(mod: any): any {
if (Reflect.has(target, prop)) {
return Reflect.get(target, prop, receiver);
}
let fallback = Reflect.get(def, prop, receiver);
if (typeof fallback === "function") {
fallback = fallback.bind(def);
if (defIsObj) {
let fallback = Reflect.get(def, prop, receiver);
if (typeof fallback === "function") {
fallback = fallback.bind(def);
}
return fallback;
}
return fallback;
},
apply(target, thisArg, args) {
if (typeof target === "function") {
Expand Down
1 change: 1 addition & 0 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports[`fixtures > deps > stdout 1`] = `
npm:defu {}
npm:destr true
npm:etag: true
npm:is-installed-globally false
npm:mime: true
npm:typescript: true
npm:moment-timezone true
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/deps/iig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import isInstalledGlobally from "is-installed-globally";

console.log("npm:is-installed-globally", isInstalledGlobally);
1 change: 1 addition & 0 deletions test/fixtures/deps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "./consola";
import "./defu";
import "./destr";
import "./etag";
import "./iig";
import "./mime";
import "./typescript";
import "./moment";
Expand Down

0 comments on commit cca319b

Please sign in to comment.