mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00

PR-URL: https://github.com/nodejs/node/pull/57704 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
39 lines
1 KiB
JavaScript
39 lines
1 KiB
JavaScript
"use strict";
|
|
import { fileURLToPath } from "node:url";
|
|
import { isSwcError, wrapAndReThrowSwcError } from "./errors.js";
|
|
import { transformSync } from "./index.js";
|
|
export async function load(url, context, nextLoad) {
|
|
const { format } = context;
|
|
if (format?.endsWith("-typescript")) {
|
|
try {
|
|
const { source } = await nextLoad(url, {
|
|
...context,
|
|
format: "module"
|
|
});
|
|
const { code, map } = transformSync(source.toString(), {
|
|
mode: "transform",
|
|
sourceMap: true,
|
|
filename: fileURLToPath(url)
|
|
});
|
|
let output = code;
|
|
if (map) {
|
|
const base64SourceMap = Buffer.from(map).toString("base64");
|
|
output = `${code}
|
|
|
|
//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`;
|
|
}
|
|
return {
|
|
format: format.replace("-typescript", ""),
|
|
source: `${output}
|
|
|
|
//# sourceURL=${url}`
|
|
};
|
|
} catch (error) {
|
|
if (isSwcError(error)) {
|
|
wrapAndReThrowSwcError(error);
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
return nextLoad(url, context);
|
|
}
|