8000 fix(interopDefault): improve default import interop by kricsleo · Pull Request #377 · unjs/jiti · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(interopDefault): improve default import interop #377

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 2 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ function interopDefault(mod: any): any {

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

return new Proxy(mod, {
Expand All @@ -117,7 +115,7 @@ function interopDefault(mod: any): any {
return true;
}
if (prop === "default") {
return def;
return defIsNil ? mod : def;
}
if (Reflect.has(target, prop)) {
return Reflect.get(target, prop, receiver);
Expand Down
37 changes: 37 additions & 0 deletions test/fixtures/require-esm/_dist/esm-transpiled-cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Transpiled by esbuild: https://hyrious.me/esbuild-repl/?version=0.25.3&t=export+function+fn%28%29+%7B%7D&o=--format%3Dcjs
*
* Original ESM code:
* ```js
* export function fn() {}
* ```
*
* Transpiled CJS code: ↓↓↓
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if ((from && typeof from === "object") || typeof from === "function") {
for (let key of __getOwnProp EB82 Names(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
});
}
return to;
};
var __toCommonJS = (mod) =>
__copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
fn: () => fn,
});
module.exports = __toCommonJS(stdin_exports);
function fn() {}
3 changes: 2 additions & 1 deletion test/fixtures/require-esm/_dist/esm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fn from "./fn.js";
import transpiled from "./esm-transpiled-cjs.js";
import file from "./file";
export default { fn, file };
export default { fn, fn2: transpiled.fn, file };
2 changes: 1 addition & 1 deletion test/fixtures/require-esm/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function run() {
mod = await jiti.import("./_dist/esm.js");
}
mod = mod.default;
if (typeof mod.fn === "function") {
if (typeof mod.fn === "function" && typeof mod.fn2 === "function") {
console.log("Works!");
return;
}
Expand Down
0