diff options
author | 2022-09-07 19:56:11 -0700 | |
---|---|---|
committer | 2022-09-07 19:56:11 -0700 | |
commit | d7759b88eee013dfb6fe7d7fbb9864a1c4b06269 (patch) | |
tree | 2b3a74df6da4cf85ad33c55b9c2298ae1531ca16 /src/runtime.js | |
parent | 25e4fcf5c82af1e3af5bb103c68d5e110cfbc30f (diff) | |
download | bun-d7759b88eee013dfb6fe7d7fbb9864a1c4b06269.tar.gz bun-d7759b88eee013dfb6fe7d7fbb9864a1c4b06269.tar.zst bun-d7759b88eee013dfb6fe7d7fbb9864a1c4b06269.zip |
Handle `default` better with ESM node
Diffstat (limited to 'src/runtime.js')
-rw-r--r-- | src/runtime.js | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/src/runtime.js b/src/runtime.js index c70f1207d..10223e712 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -56,6 +56,7 @@ var tagSymbol = Symbol.for("CommonJSTransformed"); var cjsRequireSymbol = Symbol.for("CommonJS"); export var __commonJS = (cb, name) => { var mod; + var origExports; var has_run = false; const requireFunction = function load() { @@ -66,27 +67,46 @@ export var __commonJS = (cb, name) => { has_run = true; cb(((mod = { exports: {} }), mod), mod.exports); - const kind = typeof mod.exports; + var mod_exports = (origExports = mod.exports); + + const kind = typeof mod_exports; + + if ((kind === "object" || kind === "function") && !mod_exports[tagSymbol]) { + const extensible = Object.isExtensible(mod_exports); + if (!extensible) { + // slow path: it's a function we need to wrap + // example: webpack + if (kind === "function") { + mod_exports = function () { + return origExports.apply(this, arguments); + }; + Object.setPrototypeOf(mod_exports, __getProtoOf(origExports)); + Object.defineProperties( + mod_exports, + Object.getOwnPropertyDescriptors(origExports) + ); + } else { + mod_exports = __create( + __getProtoOf(mod_exports), + Object.getOwnPropertyDescriptors(mod_exports) + ); + } + } - if ( - (kind === "object" || kind === "function") && - !mod.exports[tagSymbol] && - Object.isExtensible(mod.exports) - ) { - Object.defineProperty(mod.exports, tagSymbol, { + Object.defineProperty(mod_exports, tagSymbol, { value: true, enumerable: false, configurable: false, }); - if (!("default" in mod.exports)) { - Object.defineProperty(mod.exports, "default", { + if (!("default" in mod_exports)) { + Object.defineProperty(mod_exports, "default", { get() { - return mod.exports; + return origExports; }, set(v) { if (v === mod.exports) return; - mod.exports = v; + origExports = v; return true; }, // enumerable: false is important here @@ -94,9 +114,18 @@ export var __commonJS = (cb, name) => { configurable: true, }); } + + if (!extensible) { + // can only be frozen if it's not extensible + if (Object.isFrozen(origExports)) { + Object.freeze(mod_exports); + } else { + Object.preventExtensions(mod_exports); + } + } } - return mod.exports; + return mod_exports; }; requireFunction[cjsRequireSymbol] = true; |