Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I watch and run a build? #3687

Open
aarontravass opened this issue Mar 8, 2024 · 8 comments
Open

How do I watch and run a build? #3687

aarontravass opened this issue Mar 8, 2024 · 8 comments

Comments

@aarontravass
Copy link

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

@hyrious
Copy link

hyrious commented Mar 9, 2024

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
Copy link
Author

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
Copy link

hyrious commented Mar 10, 2024

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.

@aarontravass
Copy link
Author

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
Copy link

hyrious commented Mar 10, 2024

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
Copy link
Author

aarontravass commented Mar 12, 2024

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
Copy link

hyrious commented Mar 12, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants