diff options
author | 2023-08-02 16:27:36 -0700 | |
---|---|---|
committer | 2023-08-02 16:27:36 -0700 | |
commit | c2a77cf7ec9de9eadf938046bdf78e58561c8a6d (patch) | |
tree | 0f90f1b323061455875333c9f40592b303585973 /src/js/builtins/Module.ts | |
parent | 7656b4b17e91f15b58eeab8f45b78c416ec6a045 (diff) | |
download | bun-c2a77cf7ec9de9eadf938046bdf78e58561c8a6d.tar.gz bun-c2a77cf7ec9de9eadf938046bdf78e58561c8a6d.tar.zst bun-c2a77cf7ec9de9eadf938046bdf78e58561c8a6d.zip |
Rewrite built-in modules to use CommonJS over ESM (#3814)
* stfdsafsd
sadffdsa
stuff
finish commonjs stuff
asdf
not done but work
not done but work
not done yet but this is how far i am
remove files
lol
update built files
uncomment everything in events lol
export default
stuff
* afdsafsd
* its not perfect but almost done
* okay
* cool
* remove temp file
* finish rebase
* revert settings.json
* a
* ch-ch-ch-ch-changes
* okay
* remove this check in release for now
* sxdcfghnjm,
* lkjhgf
* fmt
* filename can be null
* Update NodeModuleModule.h
* weee
* fmt
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/js/builtins/Module.ts')
-rw-r--r-- | src/js/builtins/Module.ts | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/src/js/builtins/Module.ts b/src/js/builtins/Module.ts index 0b5fcafe8..5cf7290ae 100644 --- a/src/js/builtins/Module.ts +++ b/src/js/builtins/Module.ts @@ -1,9 +1,13 @@ -interface Module { +interface CommonJSModuleRecord { + $require(id: string, mod: any): any; + children: CommonJSModuleRecord[]; + exports: any; id: string; + loaded: boolean; + parent: undefined; path: string; - - $require(id: string, mod: any): any; - children: Module[]; + paths: string[]; + require: typeof require; } $getter; @@ -11,7 +15,7 @@ export function main() { return $requireMap.$get(Bun.main); } -export function require(this: Module, id: string) { +export function require(this: CommonJSModuleRecord, id: string) { const existing = $requireMap.$get(id) || $requireMap.$get((id = $resolveSync(id, this.path, false))); if (existing) { // Scenario where this is necessary: @@ -39,20 +43,6 @@ export function require(this: Module, id: string) { return $internalRequire(id); } - let esm = Loader.registry.$get(id); - if (esm?.evaluated && (esm.state ?? 0) >= $ModuleReady) { - const mod = esm.module; - const namespace = Loader.getModuleNamespaceObject(mod); - const exports = - namespace?.[$commonJSSymbol] === 0 || namespace?.default?.[$commonJSSymbol] === 0 - ? namespace.default - : namespace.__esModule - ? namespace - : Object.create(namespace, { __esModule: { value: true } }); - $requireMap.$set(id, $createCommonJSModule(id, exports, true)); - return exports; - } - // To handle import/export cycles, we need to create a module object and put // it into the map before we import it. const mod = $createCommonJSModule(id, {}, false); @@ -76,18 +66,14 @@ export function require(this: Module, id: string) { throw exception; } - esm = Loader.registry.$get(id); + const esm = Loader.registry.$get(id); // If we can pull out a ModuleNamespaceObject, let's do it. if (esm?.evaluated && (esm.state ?? 0) >= $ModuleReady) { const namespace = Loader.getModuleNamespaceObject(esm!.module); return (mod.exports = // if they choose a module - namespace?.[$commonJSSymbol] === 0 || namespace?.default?.[$commonJSSymbol] === 0 - ? namespace.default - : namespace.__esModule - ? namespace - : Object.create(namespace, { __esModule: { value: true } })); + namespace.__esModule ? namespace : Object.create(namespace, { __esModule: { value: true } })); } } @@ -95,6 +81,16 @@ export function require(this: Module, id: string) { return mod.exports; } -export function requireResolve(this: Module, id: string) { +export function requireResolve(this: CommonJSModuleRecord, id: string) { return $resolveSync(id, this.path, false); } + +export function requireNativeModule(id: string) { + // There might be a race condition here? + let esm = Loader.registry.$get(id); + if (esm?.evaluated && (esm.state ?? 0) >= $ModuleReady) { + const exports = Loader.getModuleNamespaceObject(esm.module); + return exports.default; + } + return $requireESM(id).default; +} |