@stylexjs/unplugin is a universal bundler plugin built upon the `unplugin` abstraction, designed to compile StyleX CSS at build time. It aggregates CSS from all transformed modules within a project and injects the resulting styles into an existing CSS asset produced by the bundler. If no such asset is found, it emits a stable fallback CSS file. Currently at version 0.17.6, the package is actively maintained and supports various bundlers including Vite, Rollup, Webpack, Rspack, and esbuild. Its key differentiators lie in providing a unified, declarative API across these diverse build environments, ensuring consolidated and deterministic StyleX output. Furthermore, it offers specialized development helpers via virtual modules like `virtual:stylex:runtime` and `/virtual:stylex.css` to facilitate efficient hot CSS reloads during development, simplifying the integration of StyleX into modern JavaScript build pipelines.
npm install stylex-unpluginVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure `@stylexjs/unplugin` for a Vite and React project, enabling build-time StyleX compilation and Hot Module Replacement (HMR) for development.
Use `const stylex = require('@stylexjs/unplugin').default;` instead of `const stylex = require('@stylexjs/unplugin');` for your bundler configuration.Review the `devMode` documentation and select the appropriate setting for your development workflow. `full` is recommended for most cases to enable HMR for both CSS and the StyleX runtime.
If direct HTML `<script src="/@id/virtual:stylex:runtime">` or `<link href="/virtual:stylex.css">` causes issues, consider using `import('virtual:stylex:runtime')` or `import('virtual:stylex:css-only')` from a client-side shim instead of direct HTML links.Use the `externalPackages` option in `stylex.vite()` to explicitly include additional dependencies that should be processed by the plugin, e.g., `externalPackages: ['your-stylex-library']`.
Ensure your bundler (e.g., Vite, Webpack with `MiniCssExtractPlugin`) is configured to produce at least one CSS output file. If not, be aware that `stylex.css` will be created as a standalone fallback.
Add `metafile: true` to your `esbuild.build()` configuration when integrating `stylex.esbuild()`.
Modify your `require` statement to `const stylexPlugin = require('@stylexjs/unplugin').default;`.Verify `devMode: 'full'` is set in your plugin options. Ensure `<link rel="stylesheet" href="/virtual:stylex.css" />` and `import('virtual:stylex:runtime')` are correctly placed in your HTML shell or client entrypoint. Check your browser's developer console for any errors related to loading these virtual modules.Ensure your bundler produces at least one CSS output file (e.g., configure `MiniCssExtractPlugin` for Webpack, or rely on Vite's default CSS handling). Double-check that `stylexPlugin.<bundler-name>()` is correctly listed and configured within your bundler's plugin array.