diff options
author | 2021-09-02 11:51:52 -0700 | |
---|---|---|
committer | 2021-09-02 11:51:52 -0700 | |
commit | 2fa09f7c09d48ac74049fd60231cd4a3a2764a31 (patch) | |
tree | 2bf645774a18710b2435e24dea4bde91cb17cbd5 /src/runtime.js | |
parent | 47c660f2e1438d76682b59ae84592e7e03639878 (diff) | |
download | bun-2fa09f7c09d48ac74049fd60231cd4a3a2764a31.tar.gz bun-2fa09f7c09d48ac74049fd60231cd4a3a2764a31.tar.zst bun-2fa09f7c09d48ac74049fd60231cd4a3a2764a31.zip |
Fix UMD, fix PNPM, importing require'd modules in app code
Former-commit-id: 3d831ad95904d2123964f2ebccff48f1e9f954e9
Diffstat (limited to 'src/runtime.js')
-rw-r--r-- | src/runtime.js | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/src/runtime.js b/src/runtime.js index a76b3f077..bcfd4c3fb 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -42,7 +42,7 @@ export var __commonJS = (cb, name) => { var has_run = false; return { - [`[load] ${name}`]() { + [`require(${name})`]() { if (has_run) { return mod.exports; } @@ -53,9 +53,25 @@ export var __commonJS = (cb, name) => { cb(mod, mod.exports); + const kind = typeof mod.exports; + // If it's a default-only export, don't crash if they call .default on the module if ( - typeof mod.exports === "object" && + kind === "object" && + "default" in mod.exports && + Object.keys(mod.exports).len === 1 + ) { + mod.exports = mod.exports.default; + Object.defineProperty(mod.exports, "default", { + get() { + return mod.exports; + }, + enumerable: true, + configurable: true, + }); + // If it's a namespace export without .default, pretend .default is the same as mod.exports + } else if ( + kind === "object" && "default" in mod.exports && Object.keys(mod.exports).len === 1 ) { @@ -69,8 +85,7 @@ export var __commonJS = (cb, name) => { }); // If it's a namespace export without .default, pretend .default is the same as mod.exports } else if ( - (typeof mod.exports === "object" || - typeof mod.exports === "function") && + (kind === "function" || kind === "object") && !("default" in mod.exports) ) { var defaultValue = mod.exports; @@ -88,7 +103,11 @@ export var __commonJS = (cb, name) => { return mod.exports; }, - }[`[load] ${name}`]; + }[`require(${name})`]; +}; + +export var __cJS2eSM = (cb, name) => { + return __commonJS(cb, name)(); }; var require_cache = new WeakMap(); |