esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

How do I watch and run a build?

Open aarontravass opened this issue 1 year ago • 9 comments

Something similar to tsx watch src/index.ts, is there an option in esbuild? The --watch option will not run the file.

aarontravass avatar Mar 08 '24 22:03 aarontravass

Node.js has a --watch option. You can do something like this:

# concurrently
esbuild --watch main.ts --bundle --platform=node --outdir=.
node --watch main.js

hyrious avatar Mar 09 '24 01:03 hyrious

Thanks for the reply. How performant is that in comparison with let's say tsx or ts-node since both esbuild and Node are listening for file changes?

aarontravass avatar Mar 09 '24 16:03 aarontravass

I didn't run benchmarks. However you may find some clue in their technical details:

  • tsx and ts-node are using esm loaders and require.extensions to hack into Node.js execution and the former runs esbuild.transform() on each loaded file. Their cold start is maybe faster than "build and run" since the IO payload is smaller than a full build.

  • tsx uses a performant FS watcher (e.g. use fsevents on macOS) to watch and restart the Node.js process. While esbuild uses its own polling-based watcher. So tsx is maybe faster in responding to a file change.

However esbuild is quite fast, even a full build often only takes 0.5s. I don't think performance that matters in this situation. Note that the build mode can give you plugins and less dependencies.

hyrious avatar Mar 10 '24 00:03 hyrious

node --watch main.js

Looks like it never ends up running node --watch dist/server.js. Here is what I did

  "build:watch": "esbuild --watch src/server.ts --bundle --platform=node --outdir=dist",
  "node:watch": "doppler run -- node --watch dist/server.js",
  "dev": "pnpm build:watch && pnpm node:watch",

and the logs

 pnpm dev

> pnpm build:watch && pnpm node:watch

> esbuild --watch src/server.ts --bundle --platform=node --outdir=dist

[watch] build finished, watching for changes...

aarontravass avatar Mar 10 '24 21:03 aarontravass

The shell operator a && b means only when the former command exits successfully then the latter command will run. Obviously neither esbuild or node would exit in watch mode.

By concurrently I mean you can start 2 terminals to execute them individually, or using the concurrently npm package to do similar things.

hyrious avatar Mar 10 '24 22:03 hyrious

By concurrently I mean you can start 2 terminals to execute them individually, or using the concurrently npm package to do similar things.

My apologies, I miss understood. Well tbh, that doesn't sound 'developer friendly'. Guess I'll have to stick with tsx for now to watch and run my files. Thanks for the help!

aarontravass avatar Mar 12 '24 02:03 aarontravass

Well tbh, that doesn't sound 'developer friendly'.

There're tons of tools around esbuild to make common tasks have good DX like tsx to execute scripts and vite to develop lib / websites. In fact I also wrote one which wraps esbuild's build mode and takes advantage of esbuild's builtin watch mode to respawn the Node.js process.

hyrious avatar Mar 12 '24 03:03 hyrious

Well tbh, that doesn't sound 'developer friendly'.

There're tons of tools around esbuild to make common tasks have good DX like tsx to execute scripts and vite to develop lib / websites. In fact I also wrote one which wraps esbuild's build mode and takes advantage of esbuild's builtin watch mode to respawn the Node.js process.

Thanks for that

aarontravass avatar Mar 21 '24 04:03 aarontravass