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.js
It assumes the project structure looks like this:
src/
├── index.html
├── index.ts
├── style.css
└── livereload.js
Line 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 thebuilddirectory.--servedir=buildserves static content from thebuilddirectory if a request path doesn’t match any generated files. This is howindex.htmlgets served.--loader:.html=copytells esbuild to use the copy loader for any files ending.html, copying them unchanged tooutdir. This also gets triggered when the HTML file changes.--inject:src/livereload.jsadds the comments ofsrc/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());