StyleX Unplugin Bundler Plugin

0.17.6 · active · verified Tue Apr 21

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

Common errors

Warnings

Install

Imports

Quickstart

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.

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import stylexPlugin from '@stylexjs/unplugin';

export default defineConfig({
  plugins: [
    stylexPlugin.vite({
      // Optional: Manually include packages that use StyleX and should be transformed.
      // externalPackages: ['lib-using-stylex'],
      // devMode: 'full' // Controls HMR behavior: 'full' | 'css-only' | 'off'
    }),
    react(),
  ],
});

// To enable HMR in development, add the following to your root HTML file (e.g., public/index.html):
/*
<link rel="stylesheet" href="/virtual:stylex.css" />
<script type="module">
  import 'virtual:stylex:runtime'; // or 'virtual:stylex:css-only' if only CSS HMR is needed
</script>
*/

view raw JSON →