diff options
author | 2021-09-12 00:39:57 -0700 | |
---|---|---|
committer | 2021-09-12 00:39:57 -0700 | |
commit | 350569655bcf4348d521f20cec4da7aee55ae102 (patch) | |
tree | aa278040f04d477739a16aef25991ca1ce7cd4ed /src/runtime.js | |
parent | 092f9ac766ff532cb34587b5d93c401070cc79cf (diff) | |
download | bun-350569655bcf4348d521f20cec4da7aee55ae102.tar.gz bun-350569655bcf4348d521f20cec4da7aee55ae102.tar.zst bun-350569655bcf4348d521f20cec4da7aee55ae102.zip |
Support bundling dynamically imported modules.
Diffstat (limited to 'src/runtime.js')
-rw-r--r-- | src/runtime.js | 91 |
1 files changed, 43 insertions, 48 deletions
diff --git a/src/runtime.js b/src/runtime.js index 52974144b..a90af7427 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -38,11 +38,12 @@ export var __toModule = (module) => { }; var tagSymbol = Symbol("CommonJSTransformed"); +var cjsRequireSymbol = Symbol("CommonJS"); export var __commonJS = (cb, name) => { var mod = {}; var has_run = false; - return function require() { + const requireFunction = function load() { if (has_run) { return mod.exports; } @@ -97,12 +98,13 @@ export var __commonJS = (cb, name) => { return mod.exports; }; -}; -export var __cJS2eSM = (cb, name) => { - return __commonJS(cb, name)(); + requireFunction[cjsRequireSymbol] = true; + return requireFunction; }; +export var __cJS2eSM = __commonJS; + var require_cache = new WeakMap(); export var __BUN_INTERNAL_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = { @@ -119,51 +121,44 @@ export var __BUN_INTERNAL_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = { // ); export var __require = (namespace) => { - var entry = require_cache.get(namespace); - if (typeof entry !== "undefined") { - return entry; - } - - var target = - Object.prototype.hasOwnProperty.call(namespace, "default") && - Object.keys(namespace).length === 1 - ? namespace["default"] - : namespace; - - if (typeof target !== "function") { - throw new __BUN_INTERNAL_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.RequireFailedError( - `Couldn't find module "${ - typeof namespace === "string" - ? namespace - : namespace.name || namespace.displayName || namespace.toString() - }"` - ); - } - - var exports = target(); - require_cache.set(namespace, exports); - return exports; -}; - -if ( - !( - "__BUN_INTERNAL_DO_NOT_USE_OR_YOU_WILL_BE_FIRED__MODULE_LOAD_CACHE" in - globalThis - ) -) { - globalThis.__BUN_INTERNAL_DO_NOT_USE_OR_YOU_WILL_BE_FIRED__MODULE_LOAD_CACHE = - new Map(); -} - -if ( - !( - "__BUN_INTERNAL_DO_NOT_USE_OR_YOU_WILL_BE_FIRED__MODULE_REGISTRY" in - globalThis + if ( + typeof namespace === "object" && + "default" in namespace && + namespace.default[cjsRequireSymbol] ) -) { - globalThis.__BUN_INTERNAL_DO_NOT_USE_OR_YOU_WILL_BE_FIRED__MODULE_REGISTRY = - new Map(); -} + return namespace.default(); + + return namespace; + // // is it an ESM module record? + // if (namespaceType === "object") return namespace; + // // is it a CommonJS module? + + // // i have no idea what it is so i'm just going to try stuff and pray + // var entry = require_cache.get(namespace); + // if (typeof entry !== "undefined") { + // return entry; + // } + + // var target = + // Object.prototype.hasOwnProperty.call(namespace, "default") && + // Object.keys(namespace).length === 1 + // ? namespace["default"] + // : namespace; + + // if (typeof target !== "function") { + // throw new __BUN_INTERNAL_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.RequireFailedError( + // `Couldn't find module "${ + // typeof namespace === "string" + // ? namespace + // : namespace.name || namespace.displayName || namespace.toString() + // }"` + // ); + // } + + // var exports = target(); + // require_cache.set(namespace, exports); + // return exports; +}; export var $$m = __commonJS; |