Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/core/register.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as NodeModule from "node:module";
import { addHook } from "pirates";

import { OxcTransformer } from "./index.js";
import { createResolve, initTracing, load } from "./index.js";

// Destructure from NodeModule namespace to support older Node.js versions
const { register, setSourceMapsSupport } = NodeModule;
const { registerHooks, register, setSourceMapsSupport } = NodeModule;

const DEFAULT_EXTENSIONS = new Set([
".js",
Expand All @@ -20,7 +20,25 @@ const DEFAULT_EXTENSIONS = new Set([
".es",
]);

register("@oxc-node/core/esm", import.meta.url);
// Use registerHooks (Node.js >= 24.4) when available, fall back to deprecated register()
if (typeof registerHooks === "function") {
initTracing();

// Wrap resolve to strip fields not recognized by the native binding
function resolve(specifier, context, nextResolve) {
const { parentURL, conditions } = context;
return createResolve(
{ getCurrentDirectory: () => process.cwd() },
specifier,
{ parentURL, conditions },
nextResolve,
);
}

registerHooks({ resolve, load });
} else {
register("@oxc-node/core/esm", import.meta.url);
}

if (typeof setSourceMapsSupport === "function") {
setSourceMapsSupport(true, { nodeModules: true, generatedCode: true });
Expand Down