Skip to content

Commit

Permalink
fix(interopDefault): assign to function props to allow mixed usage
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 5, 2024
1 parent 006a9fd commit 60882cc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ export function interopDefault(
if (defaultValue === undefined || defaultValue === null) {
return sourceModule;
}
if (typeof defaultValue !== "object") {
const _defaultType = typeof defaultValue;
if (
_defaultType !== "object" &&
!(_defaultType === "function" && !opts.preferNamespace)
) {
return opts.preferNamespace ? sourceModule : defaultValue;
}
for (const key in sourceModule) {
Expand Down
21 changes: 21 additions & 0 deletions test/interop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,25 @@ describe("interopDefault", () => {
}
});
}

it("function as default", () => {
const mod = {
default: () => {},
x: 123,
};

// Default behavior
const interop = interopDefault(mod);
expect(typeof interop).toBe("function");
expect(interop).toBe(mod.default);
expect(interop.x).toBe(123);
expect(interop.default).toBe(mod.default);

// With preferNamespace
const interopNS = interopDefault(mod, { preferNamespace: true });
expect(typeof interopNS).toBe("object");
expect(interopNS).toBe(mod);
expect(interopNS.x).toBe(123);
expect(interopNS.default).toBe(mod.default);
});
});

0 comments on commit 60882cc

Please sign in to comment.