261026.1 · b6bf196ae1e954da44a0f35532cd30b2e0969202
feat: use module.registerHooks for amaro/strip and amaro/transform
Use synchronous module.registerHooks() when available, so the loaders also apply to require() of TypeScript and avoid the loader thread. Fall back to module.register() on Node.js versions without it.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
reading declarations
Changes to src/. All files
src/register/register-strip.mjs
@@ -1,3 +1,9 @@-import { register } from "node:module";+import module from "node:module"; -register("./strip-loader.js", import.meta.url);+if (typeof module.registerHooks === "function") {+ const { default: loader } = await import("./strip-loader.js");+ module.registerHooks({ load: loader.loadSync });+} else {+ // Node.js versions without `module.registerHooks()`.+ module.register("./strip-loader.js", import.meta.url);+}
src/register/register-transform.mjs
@@ -1,4 +1,4 @@-import { register } from "node:module";+import module from "node:module"; import { emitWarning, env, execArgv } from "node:process"; const hasSourceMaps =@@ -9,4 +9,10 @@ if (!hasSourceMaps) { emitWarning("Source maps are disabled, stack traces will not be accurate"); } -register("./transform-loader.js", import.meta.url);+if (typeof module.registerHooks === "function") {+ const { default: loader } = await import("./transform-loader.js");+ module.registerHooks({ load: loader.loadSync });+} else {+ // Node.js versions without `module.registerHooks()`.+ module.register("./transform-loader.js", import.meta.url);+}
@@ -3,40 +3,62 @@ import { fileURLToPath } from "node:url"; import { isSwcError, wrapAndReThrowSwcError } from "./errors.js"; import { transformSync } from "./index.js"; +type NextLoad = (+ url: string,+ context?: Partial<LoadHookContext>,+) => LoadFnOutput;++function strip(url: string, format: string, source: LoadFnOutput["source"]) {+ try {+ // biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source+ const { code } = transformSync(source!.toString(), {+ mode: "strip-only",+ filename: fileURLToPath(url),+ });+ return {+ format: format.replace("-typescript", ""),+ // Source map is not necessary in strip-only mode. However, to map the source+ // file in debuggers to the original TypeScript source, add a sourceURL magic+ // comment to hint that it is a generated source.+ source: `${code}\n\n//# sourceURL=${url}`,+ };+ } catch (error: unknown) {+ if (isSwcError(error)) {+ wrapAndReThrowSwcError(error);+ }+ // If the error is not an SwcError, rethrow it+ throw error;+ }+}++// Synchronous hook for `module.registerHooks()`.+export function loadSync(+ url: string,+ context: LoadHookContext,+ nextLoad: NextLoad,+) {+ const { format } = context;+ if (format?.endsWith("-typescript")) {+ const { source } = nextLoad(url, { ...context, format });+ return strip(url, format, source);+ }+ return nextLoad(url, context);+}++// Asynchronous hook for `module.register()`, used on Node.js versions that+// do not provide `module.registerHooks()`. export async function load( url: string, context: LoadHookContext, nextLoad: ( url: string,- context?: LoadHookContext,+ context?: Partial<LoadHookContext>, ) => LoadFnOutput | Promise<LoadFnOutput>, ) { const { format } = context; if (format?.endsWith("-typescript")) {- try {- const { source } = await nextLoad(url, {- ...context,- format,- });- // biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source- const { code } = transformSync(source!.toString(), {- mode: "strip-only",- filename: fileURLToPath(url),- });- return {- format: format.replace("-typescript", ""),- // Source map is not necessary in strip-only mode. However, to map the source- // file in debuggers to the original TypeScript source, add a sourceURL magic- // comment to hint that it is a generated source.- source: `${code}\n\n//# sourceURL=${url}`,- };- } catch (error: unknown) {- if (isSwcError(error)) {- wrapAndReThrowSwcError(error);- }- // If the error is not an SwcError, rethrow it- throw error;- }+ const { source } = await nextLoad(url, { ...context, format });+ return strip(url, format, source); } return nextLoad(url, context); }
@@ -4,47 +4,72 @@ import type { Options } from "../lib/wasm"; import { isSwcError, wrapAndReThrowSwcError } from "./errors.js"; import { transformSync } from "./index.js"; +type NextLoad = (+ url: string,+ context?: Partial<LoadHookContext>,+) => LoadFnOutput;++function transform(+ url: string,+ format: string,+ source: LoadFnOutput["source"],+) {+ try {+ // biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source+ const { code, map } = transformSync(source!.toString(), {+ mode: "transform",+ sourceMap: true,+ filename: fileURLToPath(url),+ } as Options);++ let output = code;++ if (map) {+ const base64SourceMap = Buffer.from(map).toString("base64");+ output = `${code}\n\n//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`;+ }++ return {+ format: format.replace("-typescript", ""),+ source: `${output}\n\n//# sourceURL=${url}`,+ };+ } catch (error) {+ if (isSwcError(error)) {+ wrapAndReThrowSwcError(error);+ }+ // If the error is not an SwcError, rethrow it+ throw error;+ }+}++// Synchronous hook for `module.registerHooks()`.+export function loadSync(+ url: string,+ context: LoadHookContext,+ nextLoad: NextLoad,+) {+ const { format } = context;+ if (format?.endsWith("-typescript")) {+ const { source } = nextLoad(url, { ...context, format });+ return transform(url, format, source);+ }+ return nextLoad(url, context);+}++// Asynchronous hook for `module.register()`, used on Node.js versions that+// do not provide `module.registerHooks()`. export async function load( url: string, context: LoadHookContext, nextLoad: ( url: string,- context?: LoadHookContext,+ context?: Partial<LoadHookContext>, ) => LoadFnOutput | Promise<LoadFnOutput>, ) { const { format } = context; if (format?.endsWith("-typescript")) {- try {- const { source } = await nextLoad(url, {- ...context,- format,- });-- // biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source- const { code, map } = transformSync(source!.toString(), {- mode: "transform",- sourceMap: true,- filename: fileURLToPath(url),- } as Options);-- let output = code;-- if (map) {- const base64SourceMap = Buffer.from(map).toString("base64");- output = `${code}\n\n//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`;- }-- return {- format: format.replace("-typescript", ""),- source: `${output}\n\n//# sourceURL=${url}`,- };- } catch (error) {- if (isSwcError(error)) {- wrapAndReThrowSwcError(error);- }- // If the error is not an SwcError, rethrow it- throw error;- }+ const { source } = await nextLoad(url, { ...context, format });+ return transform(url, format, source); } return nextLoad(url, context); }