Skip to content

Commit

Permalink
allow to see exports for non-enumerable properties
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 2, 2023
1 parent 9324f72 commit 19763cf
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions crates/turbopack-dev/js/src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ function createGetter(obj, key) {
return () => obj[key];
}

/**
* @param {any} obj
* @returns {any} prototype of the object
*/
const getProto = Object.getPrototypeOf
? (obj) => Object.getPrototypeOf(obj)
: (obj) => obj.__proto__;

/** Prototypes that are not expanded for exports */
const LEAF_PROTOTYPES = [null, getProto({}), getProto([]), getProto(getProto)];

/**
* @param {Exports} raw
* @param {EsmNamespaceObject} ns
Expand All @@ -172,8 +183,14 @@ function createGetter(obj, key) {
function interopEsm(raw, ns, allowExportDefault) {
/** @type {Object.<string, () => any>} */
const getters = { __proto__: null };
for (const key in raw) {
getters[key] = createGetter(raw, key);
for (
let current = raw;
typeof current === "object" && !LEAF_PROTOTYPES.includes(current);
current = getProto(current)
) {
for (const key of Object.getOwnPropertyNames(current)) {
getters[key] = createGetter(raw, key);
}
}
if (!(allowExportDefault && "default" in getters)) {
getters["default"] = () => raw;
Expand Down

0 comments on commit 19763cf

Please sign in to comment.