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

refactor(interopDefault): simplify implementation #243

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 11 additions & 25 deletions src/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,17 @@ export function interopDefault(
return opts.preferNamespace ? sourceModule : defaultValue;
}
for (const key in sourceModule) {
if (key === "default") {
try {
if (!(key in defaultValue)) {
Object.defineProperty(defaultValue, key, {
enumerable: false,
configurable: false,
get() {
return defaultValue;
},
});
}
} catch {}
} else {
try {
if (!(key in defaultValue)) {
Object.defineProperty(defaultValue, key, {
enumerable: true,
configurable: true,
get() {
return sourceModule[key];
},
});
}
} catch {}
}
try {
if (!(key in defaultValue)) {
Object.defineProperty(defaultValue, key, {
enumerable: key !== "default",
configurable: key !== "default",
get() {
return sourceModule[key];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a small chance of behavior regression here.

In the current implementation, we were using the (cached) value of sourceModule.default via defaultValue now we dynamically access it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless the supplied module comes with interop code (which already defines default as a getter), I don't anticipate any changes in behavior here.

Furthermore, the appropriate behavior is either to cache all keys (getter) or not cache any at all, instead of caching default only.

},
});
}
} catch {}
}
return defaultValue;
}