Skip to content

Commit

Permalink
Make this work in bun
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Jun 2, 2024
1 parent 84d3641 commit 2b28c0b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
14 changes: 13 additions & 1 deletion src/compiler/nodeGetBuiltinModule.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit 2b28c0b

Please sign in to comment.