Skip to content

Instantly share code, notes, and snippets.

@qpwo
Last active February 26, 2022 03:34
Show Gist options
  • Save qpwo/185b6950da194f2980c45a5e6d2fc8bd to your computer and use it in GitHub Desktop.
Save qpwo/185b6950da194f2980c45a5e6d2fc8bd to your computer and use it in GitHub Desktop.
use esbuild to get a node repl for a typescript file
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
); }
$ 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