evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
38.16k stars 1.15k forks source link

How do I watch and run a build? #3687

Open aarontravass opened 8 months ago

aarontravass commented 8 months ago

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

hyrious commented 8 months ago

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
aarontravass commented 8 months ago

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?

hyrious commented 8 months ago

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

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.

aarontravass commented 8 months ago
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...
hyrious commented 8 months ago

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.

aarontravass commented 8 months ago

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!

hyrious commented 8 months ago

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.

aarontravass commented 7 months ago

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