Skip to content

Commit

Permalink
feat: use smarter proxy for interopDefault (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Oct 4, 2024
1 parent 8826047 commit da70d24
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 35 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"changelogen": "^0.5.7",
"config": "^3.3.12",
"consola": "^3.2.3",
"defu": "^6.1.4",
"destr": "^2.0.3",
"escape-string-regexp": "^5.0.0",
"eslint": "^9.11.1",
Expand Down Expand Up @@ -125,7 +126,8 @@
"webpack-bundle-analyzer": "^4.10.2",
"webpack-cli": "^5.1.4",
"webpack-license-plugin": "^4.5.0",
"yoctocolors": "^2.1.1"
"yoctocolors": "^2.1.1",
"zod": "^3.23.8"
},
"packageManager": "pnpm@9.11.0"
}
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

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

73 changes: 42 additions & 31 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,41 +102,52 @@ export function jitiInteropDefault(ctx: Context, mod: any) {
return ctx.opts.interopDefault ? interopDefault(mod) : mod;
}

// Source: https://github.com/unjs/mlly/blob/2348417d25522b98ed60ccc10eb030abb2f65744/src/cjs.ts#L59C1-L93C2
function interopDefault(
sourceModule: any,
opts: { preferNamespace?: boolean } = {},
): any {
if (!isObject(sourceModule) || !("default" in sourceModule)) {
return sourceModule;
function interopDefault(mod: any): any {
if (!mod || !("default" in mod)) {
return mod;
}
const defaultValue = sourceModule.default;
if (defaultValue === undefined || defaultValue === null) {
return sourceModule;

const defaultExport = mod.default;
if (defaultExport === undefined || defaultExport === null) {
return mod;
}
const _defaultType = typeof defaultValue;
if (
_defaultType !== "object" &&
!(_defaultType === "function" && !opts.preferNamespace)
) {
return opts.preferNamespace ? sourceModule : defaultValue;

const modKeys = Object.getOwnPropertyNames(mod);
if (modKeys.length === 1 || (modKeys.length === 2 && "__esModule" in mod)) {
return defaultExport;
}
for (const key in sourceModule) {
try {
if (!(key in defaultValue)) {
Object.defineProperty(defaultValue, key, {
enumerable: key !== "default",
configurable: key !== "default",
get() {
return sourceModule[key];
},
});
}
} catch {
// Ignore error
}

const defaultExportType = typeof defaultExport;
if (defaultExportType !== "object" && defaultExportType !== "function") {
return mod;
}
return defaultValue;

return new Proxy(mod, {
get(target, prop, receiver) {
if (prop === "__esModule") {
return true;
}
if (prop === "default") {
return defaultExport;
}
if (Reflect.has(target, prop)) {
return Reflect.get(target, prop, receiver);
}
let fallback = Reflect.get(defaultExport, prop, receiver);
if (typeof fallback === "function") {
fallback = fallback.bind(defaultExport);
}
return fallback;
},
apply(target, thisArg, args) {
if (typeof target === "function") {
return Reflect.apply(target, thisArg, args);
}
if (defaultExportType === "function") {
return Reflect.apply(defaultExport, thisArg, args);
}
},
});
}

export function normalizeWindowsImportId(id: string) {
Expand Down
9 changes: 6 additions & 3 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ exports[`fixtures > data-uri > stdout 1`] = `""`;

exports[`fixtures > deps > stdout 1`] = `
"npm:config: true
npm:defu {}
npm:destr true
npm:etag: true
npm:mime: true
npm:typescript: true
npm:moment-timezone true"
npm:moment-timezone true
npm:zod: true"
`;

exports[`fixtures > env > stdout 1`] = `
Expand All @@ -35,8 +38,8 @@ exports[`fixtures > error-runtime > stderr 1`] = `"test error"`;
exports[`fixtures > error-runtime > stdout 1`] = `""`;
exports[`fixtures > esm > stdout 1`] = `
"{ utilsLib: { utils: { default: 'default' }, version: '123' } }
{ utils: { default: 'default' } }
"{ utilsLib: { utils: { a: 'a', default: 'default' }, version: '123' } }
{ utils: { a: 'a', default: 'default' } }
{
file: '<cwd>/test.js',
dir: '<cwd>',
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/deps/defu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import defuDefault from "defu";
import { defu } from "defu";

console.log("npm:defu", defu({}) && defuDefault({}));
7 changes: 7 additions & 0 deletions test/fixtures/deps/destr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { destr } from "destr";
import destrDefault from "destr";

console.log(
"npm:destr",
destr("true") === true && destrDefault("true") === true,
);
3 changes: 3 additions & 0 deletions test/fixtures/deps/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import "./config";
import "./consola";
import "./defu";
import "./destr";
import "./etag";
import "./mime";
import "./typescript";
import "./moment";
import "./zod";
4 changes: 4 additions & 0 deletions test/fixtures/deps/zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @ts-ignore
import { z } from "zod";

console.log("npm:zod:", z.string().parse("hello world") === "hello world");

0 comments on commit da70d24

Please sign in to comment.