-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
patch-repl.ts
41 lines (35 loc) · 902 Bytes
/
patch-repl.ts
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
import repl, { type REPLServer, type REPLEval } from 'repl';
import { transform } from '@esbuild-kit/core-utils';
function patchEval(nodeRepl: REPLServer) {
const { eval: defaultEval } = nodeRepl;
const preEval: REPLEval = async function (code, context, filename, callback) {
try {
const transformed = await transform(
code,
filename,
{
loader: 'ts',
tsconfigRaw: {
compilerOptions: {
preserveValueImports: true,
},
},
define: {
require: 'global.require',
},
},
);
code = transformed.code;
} catch {}
return defaultEval.call(this, code, context, filename, callback);
};
// @ts-expect-error overwriting read-only property
nodeRepl.eval = preEval;
}
const { start } = repl;
repl.start = function () {
const nodeRepl = Reflect.apply(start, this, arguments);
patchEval(nodeRepl);
return nodeRepl;
};
export {};