Run a development server with live reload
esbuild is a great batteries-included bundler for frontend web projects.
This is a one line shell command that will run a development web server and reload the page every time a file changes (broken up into multiple lines here for readability). I tend to put it in the scripts property of project’s package.json file.
esbuild src/index.html src/index.ts src/style.css \
  --bundle --watch \
  --outdir=build --servedir=build \
  --loader:.html=copy \
  --inject:src/livereload.jsIt assumes the project structure looks like this:
src/
├── index.html
├── index.ts
├── style.css
└── livereload.jsLine by line, here’s what the command does:
- esbuild src/index.html src/index.ts src/style.cssruns esbuild using the three files listed as entrypoints.
- --bundlecombines all the imports from the TypeScript entrypoint into a single- .jsfile.
- --watchruns another build whenever an entrypoint or any of its dependencies change.
- --outdir=buildplaces bundled and copied files into the- builddirectory.
- --servedir=buildserves static content from the- builddirectory if a request path doesn’t match any generated files. This is how- index.htmlgets served.
- --loader:.html=copytells esbuild to use the copy loader for any files ending- .html, copying them unchanged to- outdir. This also gets triggered when the HTML file changes.
- --inject:src/livereload.jsadds the comments of- src/livereload.jsto the generated output. The reason to enable it this way is that the build command can simply omit this flag to not include the live reload code in the production bundle.
src/liveroad.js is pulled straight from the esbuild documentation on live reloading:
new EventSource("/esbuild").addEventListener("change", () => location.reload());