Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: interop modules with primitive default export #343

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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