-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (50 loc) · 1.46 KB
/
index.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
54
55
56
57
58
59
const { resolve } = require("path");
const { buildSync } = require("esbuild");
const Module = require("module");
const sourceMapSupport = require("source-map-support");
sourceMapSupport.install({
environment: "node",
hookRequire: true,
});
/**
* @typedef {Object} Options
* @property {string} filePath
* @property {string[]=} preloadedModules
* @property {string[]} scriptArgs
* @property {string=} tsConfigPath
*/
/**
* @param options {Options}
*/
exports.run = function ({
filePath,
scriptArgs,
preloadedModules = [],
tsConfigPath,
}) {
function handler(/**@type {Module} */ module, /**@type {string} */ filename) {
const result = buildSync({
entryPoints: [filename],
write: false,
platform: "node",
format: "cjs",
sourcemap: "inline",
tsconfig: tsConfigPath,
});
const originalCompile = /** @type {any}) */ (module)._compile.bind(module);
/** @type {any}) */ (module)._compile = function (
/** @type {Module} */ _,
/** @type {string} */ filename
) {
return originalCompile(result.outputFiles[0].text, filename);
};
return /** @type {any}) */ (Module)._extensions[".js"](module, filename);
}
/** @type {any} */ (Module)._extensions[".ts"] = handler;
/** @type {any} */ (Module)._extensions[".tsx"] = handler;
process.argv = [process.argv[1], filePath, ...scriptArgs];
for (const preloadedModule of preloadedModules) {
require(preloadedModule);
}
Module.runMain();
};