-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
repl.ts
42 lines (36 loc) · 981 Bytes
/
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
42
import repl, { type REPLEval } from 'repl';
import { transform } from '@esbuild-kit/core-utils';
import { version } from '../package.json';
// Copied from
// https://github.com/nodejs/node/blob/v18.2.0/lib/internal/main/repl.js#L37
console.log(
`Welcome to tsx v${version} (Node.js ${process.version}).\n`
+ 'Type ".help" for more information.',
);
const nodeRepl = repl.start();
const { eval: defaultEval } = nodeRepl;
const preEval: REPLEval = async function (code, context, filename, callback) {
const transformed = await transform(
code,
filename,
{
loader: 'ts',
tsconfigRaw: {
compilerOptions: {
preserveValueImports: true,
},
},
define: {
require: 'global.require',
},
},
).catch(
(error) => {
console.log(error.message);
return { code: '\n' };
},
);
return defaultEval.call(this, transformed.code, context, filename, callback);
};
// @ts-expect-error overriding read-only property
nodeRepl.eval = preEval;