Skip to content

rstackjs/rsbuild-plugin-tailwindcss

 
 

Repository files navigation

rsbuild-plugin-tailwindcss

An Rsbuild plugin to integrate with Tailwind CSS V4.

npm version license downloads

Why?

Tailwind CSS v4 is able to remove unused CSS classes through the @tailwindcss/webpack plugin. It scans utilities based on @source directives (and falls back to **/* when none are defined), which can still be inaccurate or redundant when:

  • Using multiple entries
  • Using a Tailwind CSS-based component library

This plugin automatically wires @tailwindcss/webpack into Rspack and injects a virtual Tailwind utilities import for each JS/TS module, so Tailwind effectively gets a precise @source per module and generates CSS based on usage across all entries and shared components.

Usage

Install:

npm add tailwindcss rsbuild-plugin-tailwindcss@v4 -D

Add plugin to your rsbuild.config.ts:

// rsbuild.config.ts
import { pluginTailwindCSS } from "rsbuild-plugin-tailwindcss";

export default {
  plugins: [pluginTailwindCSS()],
};

Custom Tailwind CSS Configuration

Create a tailwind.config.js file at the root of the project:

/** @type {import('tailwindcss').Config} */
export default {
  theme: {
    colors: {
      blue: "#1fb6ff",
      purple: "#7e5bef",
      pink: "#ff49db",
      orange: "#ff7849",
      green: "#13ce66",
      yellow: "#ffc82c",
      "gray-dark": "#273444",
      gray: "#8492a6",
      "gray-light": "#d3dce6",
    },
  },
};

This will be auto-loaded by Rsbuild and applied by rsbuild-plugin-tailwindcss.

CSS directives (@apply, @variant, etc.)

This plugin automatically wires the official @tailwindcss/postcss plugin into Rsbuild's PostCSS pipeline, so Tailwind CSS directives like @apply, @variant, @utility, and @custom-variant work out of the box without extra configuration.

/* src/styles.css */
@import "tailwindcss/utilities" layer(utilities);

.btn {
  @apply px-4 py-2 rounded bg-blue text-white;
}

Custom theme with @theme

Tailwind CSS v4 also lets you define design tokens using the @theme directive in a CSS file.

Create a theme file, for example src/theme.css:

@import "tailwindcss/theme" layer(theme);

@theme {
  /* extend default theme variables */
  --color-brand-primary: #010203;
}

Then point the plugin to this file so it is imported before utilities:

// rsbuild.config.ts
import { pluginTailwindCSS } from "rsbuild-plugin-tailwindcss";

export default {
  plugins: [
    pluginTailwindCSS({
      theme: "./src/theme.css",
    }),
  ],
};

Now you can use utilities generated from your custom theme variables, for example:

<div class="text-brand-primary">Hello</div>

Options

config

  • Type: string | undefined
  • Default: tailwind.config.js

The path to custom Tailwind CSS configuration. Could be a relative path from the root of the project or an absolute path.

  • Example:
// rsbuild.config.ts
import { pluginTailwindCSS } from "rsbuild-plugin-tailwindcss";

export default {
  plugins: [
    pluginTailwindCSS({
      config: "./config/tailwind.config.js",
    }),
  ],
};

theme

  • Type: string | undefined
  • Default: resolved path to tailwindcss/theme

The path to a CSS file that defines Tailwind theme variables using the @theme directive. This file is imported before Tailwind utilities so that any custom variables can generate additional utilities.

  • Example:
// rsbuild.config.ts
import { pluginTailwindCSS } from "rsbuild-plugin-tailwindcss";

export default {
  plugins: [
    pluginTailwindCSS({
      theme: "./src/theme.css",
    }),
  ],
};
/** src/theme.css */
@import "tailwindcss/theme" layer(theme);

@theme {
  --color-brand-theme: #010204;
}

Credits

Thanks to:

License

MIT.

About

An Rsbuild plugin to integrate with Tailwind CSS V3

Topics

Resources

License

Stars

Watchers

Forks

Contributors