From 2b28c0b55f17bdbad76ca6ce3e3ed68eb4fa252b Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Sun, 2 Jun 2024 00:08:37 -0700 Subject: [PATCH] Make this work in bun --- src/compiler/core.ts | 3 +-- src/compiler/nodeGetBuiltinModule.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index a9005487dd901..3070c7945460c 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -2729,6 +2729,5 @@ export function isNodeLikeSystem(): boolean { // use in performanceCore.ts. return typeof process !== "undefined" && !!process.nextTick - && !(process as any).browser - && typeof (process as any).getBuiltinModule === "function"; + && !(process as any).browser; } diff --git a/src/compiler/nodeGetBuiltinModule.ts b/src/compiler/nodeGetBuiltinModule.ts index e8c26c8df53e8..fff601c830c9f 100644 --- a/src/compiler/nodeGetBuiltinModule.ts +++ b/src/compiler/nodeGetBuiltinModule.ts @@ -1,4 +1,16 @@ -export function nodeCreateRequire(path: string): NodeJS.Require { +export function nodeCreateRequire(path: string): (id: string) => any { + /* eslint-disable no-restricted-globals */ + // If we're running in an environment that already has `require`, use it. + // We're probably in bun or a bundler that provides `require` even within ESM. + if (typeof require === "function" && typeof require.resolve === "function") { + return id => { + const p = require.resolve(id, { paths: [path] }); + return require(p); + }; + } + /* eslint-enable no-restricted-globals */ + + // Otherwise, try and build a `require` function from the `module` module. const mod = nodeGetBuiltinModule("module") as typeof import("module") | undefined; if (!mod) throw new Error("missing node:module"); return mod.createRequire(path);