forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform-loader.js
More file actions
39 lines (37 loc) · 1.04 KB
/
Copy pathtransform-loader.js
File metadata and controls
39 lines (37 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"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);
}