esbuild
esbuild copied to clipboard
esbuild --watch can't run in background when started from shell script
It looks as if you run esbuild --watch as a background process in a shell script (with "&") it exits immediately. Probably related to issue #1449, as it didn't do this in 0.12.16. Example:
#!/bin/sh
./node_modules/.bin/esbuild src/main.ts --outfile=/tmp/bundle.js --watch &
sleep 3
cat /tmp/bundle.js # no file created
To prevent this, it would probably be best to hide the exit-on-stdin-close behavior behind a --watch-stdin option. I don't see any other bundlers with a more elegant solution.
try:
sleep 99999 | esbuild main.ts --outfile=out.mjs --watch &
or
sleep 99999 | nohup esbuild main.ts --outfile=out.mjs --watch &
tl;dr Redirect pipe </dev/zero
Perhaps the clearest and most versatile option here is this:
make esbuild-watch </dev/null &
# or in the above example
./node_modules/.bin/esbuild src/main.ts --outfile=/tmp/bundle.js --watch </dev/zero &
(thanks @ctcarton)
This fills the Stdin pipe with 512 bytes of zeros so it doesn't auto-close.
setsid
Another option is setsid e.g.
In our case we use a Makefile so it looks like this:
setsid make esbuild-watch &
This is Linux-specific i.e. it won't work on Mac or Windows.
true |
An alternative option might be:
true | make esbuild-watch &
- [ ]
I've had a similar problem when I was trying to run that as a preLaunchTask for a VS Code extension. However, in my case the fix was to set a correct "problemWatcher" to be "$esbuild-watch" in .vscode/tasks.json file.
Perhaps this could help someone else struggling with the same problem.