Last active
February 26, 2022 03:34
-
-
Save qpwo/185b6950da194f2980c45a5e6d2fc8bd to your computer and use it in GitHub Desktop.
use esbuild to get a node repl for a typescript file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function import-ts-repl() { ( | |
set -e | |
file=$1 | |
rm -f ${file}.js ${file}.js.map | |
esbuild ${file} --target=node14 --format=cjs --sourcemap --outfile=${file}.js --platform=node --bundle | |
node -r ${file}.js | |
rm -f ${file}.js ${file}.js.map | |
); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat example.ts | |
function f() { | |
return 5 | |
} | |
global.f = f | |
const x: number = f() | |
global.x = x | |
console.log(x) | |
$ import-ts-repl ./example.ts | |
example.ts.js 133b | |
example.ts.js.map 291b | |
⚡ Done in 5ms | |
5 | |
Welcome to Node.js v17.6.0. | |
Type ".help" for more information. | |
> x * x | |
25 | |
> y = x + 7 | |
12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment