-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild-hook.js
53 lines (43 loc) · 1.07 KB
/
esbuild-hook.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* istanbul ignore file */
const Module = require("module");
const { transformSync } = require("esbuild");
const sourceMapSupport = require("source-map-support");
const cache = {};
function esbuildHook(code, filepath) {
const result = transformSync(code, {
target: "node16",
sourcemap: "both",
loader: "ts",
format: "cjs",
sourcefile: filepath,
});
cache[filepath] = {
url: filepath,
code: result.code,
map: result.map,
};
return result.code;
}
sourceMapSupport.install({
environment: "node",
retrieveFile(pathOrUrl) {
const file = cache[pathOrUrl];
if (file) {
return file.code;
} else {
return "";
}
},
});
const defaultLoader = Module._extensions[".js"];
Module._extensions[".ts"] = function (mod, filename) {
if (filename.includes("node_modules")) {
return defaultLoader(mod, filename);
}
const defaultCompile = mod._compile;
mod._compile = function (code) {
mod._compile = defaultCompile;
return mod._compile(esbuildHook(code, filename), filename);
};
defaultLoader(mod, filename);
};