Yes, Bun supports hot CSS reloading for CSS-in-JS frameworks like Emotion and Styled Components. Here are the key points:
Hot CSS Reloading with CSS-in-JS
- Bun inserts `@supports` annotations into CSS that tag which files a stylesheet is composed of to enable hot reloading. Browsers ignore these annotations[1].
- When importing CSS in JavaScript-like loaders, Bun will transform the import statement and dispatch a custom event `onimportcss` with the CSS file URL. An event handler is automatically registered when HMR is enabled to insert the CSS file as a `` tag[1][4].
- This event handler can be turned off in a framework's `package.json` or by setting `globalThis["Bun_disableCSSImports"] = true;` in client-side code[1].
- Bun also exposes an API `Bun.getImportedStyles()` for SSR/SSG that returns a list of imported CSS files. This is useful for preventing flash of unstyled content[1][4].
Limitations and Workarounds
- As of Bun v0.1.5, the hot reloading functionality for CSS-in-JS was disabled due to bugs and lack of end-to-end test coverage. A workaround was to set the `BUN_FORCE_HMR=1` flag[2].
- In Bun v1.0, the `bun dev` command was changed to run the "dev" script in your `package.json`. This issue may no longer be applicable in the latest version[2].
- Some developers have used external tools like `watchman` to watch files and send a websocket message to reload the page when changes occur as a workaround for hot reloading[2].
In summary, Bun has built-in support for hot CSS reloading with CSS-in-JS frameworks, but there may be some limitations depending on the version. Workarounds are available to enable hot reloading if needed.
Citations:[1] https://github.com/softwarearchitect817/Bun-fast-JavaScript-runtime
[2] https://github.com/oven-sh/bun/issues/833
[3] https://stackoverflow.com/questions/76427355/how-to-enable-hot-reloading-with-bun-in-a-react-application
[4] https://fossies.org/linux/bun/docs/dev/css.md
[5] https://codeclubanalysis.hashnode.dev/bun-your-all-in-one-solution-for-high-performance-javascript
[6] https://github.com/oven-sh/bun/discussions/7119
[7] https://dev.to/srmagura/why-were-breaking-up-wiht-css-in-js-4g9b
[8] https://blog.logrocket.com/css-vs-css-in-js/