Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use Bun's hot CSS reloading with React


Can I use Bun's hot CSS reloading with React


Yes, you can use Bun's hot CSS reloading with React, but it has some limitations:

Hot CSS Reloading with React

- Bun inserts `@supports` annotations into CSS that tag which files a stylesheet is composed of to enable hot reloading. Browsers ignore these annotations.

- 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.

- This event handler can be turned off in a framework's `package.json` or by setting `globalThis["Bun_disableCSSImports"] = true;` in client-side code.

- 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.

Limitations

- 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.

- 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.

- 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.

In summary, Bun has built-in support for hot CSS reloading with React, but there may be some limitations depending on the version. Workarounds are available to enable hot reloading if needed. The functionality is still maturing and may improve in future versions of Bun.

Citations:
[1] https://github.com/facebook/create-react-app/issues/93
[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://github.com/softwarearchitect817/Bun-fast-JavaScript-runtime
[5] https://linkb.hashnode.dev/bunsh-with-react-typescript-tailwindcss-and-storybook
[6] https://github.com/oven-sh/bun/discussions/7119
[7] https://fossies.org/linux/bun/docs/dev/css.md
[8] https://bun.sh/guides/ecosystem/react