Skip to content

Commit 30c56db

Browse files
fix(repl): error on import statement (#56)
1 parent 6fe2524 commit 30c56db

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/repl.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ const preEval: REPLEval = async function (code, context, filename, callback) {
1717
const transformed = await transform(
1818
code,
1919
filename,
20-
{ loader: 'ts' },
20+
{
21+
loader: 'ts',
22+
tsconfigRaw: {
23+
compilerOptions: {
24+
preserveValueImports: true,
25+
},
26+
},
27+
},
2128
).catch(
2229
(error) => {
2330
console.log(error.message);

tests/specs/repl.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,27 @@ export default testSuite(async ({ describe }) => {
3030

3131
tsxProcess.kill();
3232
}, 5000);
33+
34+
test('errors on import statement', async () => {
35+
const tsxProcess = tsx({
36+
args: [],
37+
});
38+
39+
await new Promise<void>((resolve) => {
40+
tsxProcess.stdout!.on('data', (data: Buffer) => {
41+
const chunkString = data.toString();
42+
43+
if (chunkString.includes('SyntaxError: Cannot use import statement inside the Node.js REPL')) {
44+
return resolve();
45+
}
46+
47+
if (chunkString.includes('> ')) {
48+
tsxProcess.stdin?.write('import fs from "fs"\n');
49+
}
50+
});
51+
});
52+
53+
tsxProcess.kill();
54+
}, 2000);
3355
});
3456
});

0 commit comments

Comments
 (0)