What it do?
A simple wrapper of esbuild to run your script file.
Motivation
Writing TypeScript is fun, but executing them is not. tsc
itself is just a transpiler, it does not support bundling nor executing.
Now ts-node
comes in, it wraps TypeScript compiler and makes use of Node's require.extensions
to run files, it works fine but does not fully support pure ESM (they will). Besides, invoking type system is also a waste of time when your files become more. Even if you turn-ed off type checking, reading files in node's single thread is also slow.
Now, with esbuild, you can bundle your script into one file and run it much faster.
$ esbuild-dev script.ts
# esbuild --bundle script.ts --outfile=node_modules/.esbuild-dev/script.ts.js
# node node_modules/.esbuild-dev/script.ts.js
That's it. Simple and naive.
Originally, I developed this tool to achieve zero
in some projects.*.js
Alternatives
tsx
It uses Node's native require.extensions
for commonjs and loaders for es modules to achieve similar behavior. There are pros and cons in compare it with mine:
First of all, we're using different functions in esbuild to transform your ts files to js. tsx
uses transform
, while I use build
with bundle enabled.
tsx (transform) | @hyrious/esbuild-dev (build) | |
---|---|---|
Pros |
|
|
Cons |
|
|
Note that I also have a loader mode which is a simpler implementation in that it doesn't hack require.extensions
and in most of the cases it is enough to do e.g. unit test with coverage report.