A Vite plugin for Tailwind CSS v4 that provides integration with the Vite build tool for utility-first CSS framework development
npx @tessl/cli install tessl/npm-tailwindcss--vite@4.1.0@tailwindcss/vite is a Vite plugin for Tailwind CSS v4 that provides seamless integration with the Vite build tool. It handles candidate scanning from source files, CSS generation in both development and production modes, and CSS optimization including minification.
npm install @tailwindcss/viteimport tailwindcss from "@tailwindcss/vite";For CommonJS:
const tailwindcss = require("@tailwindcss/vite").default;When working with Vite plugin types:
import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite';
import tailwindcss from "@tailwindcss/vite";import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [
tailwindcss(),
// ... other plugins
],
});The plugin is built around a three-phase approach:
The plugin automatically detects CSS files that should be processed by Tailwind CSS and transforms them through the Tailwind compilation pipeline.
Creates a Vite plugin array for Tailwind CSS v4 integration. The function takes no parameters and returns three plugins that handle different phases of the build process.
/**
* Creates a Vite plugin array for Tailwind CSS v4 integration
* @returns Array of three Vite plugins handling scan, serve, and build phases
*/
declare function tailwindcss(): Plugin[];
export default tailwindcss;// Types imported from 'vite' package
interface Plugin {
name: string;
enforce?: 'pre' | 'post';
apply?: 'build' | 'serve' | ((config: any, env: { command: string, mode: string }) => boolean);
configureServer?: (server: ViteDevServer) => void;
configResolved?: (config: ResolvedConfig) => void;
transform?: (code: string, id: string, options?: any) => any | Promise<any>;
}
interface ViteDevServer {
// Vite development server instance used in configureServer hook
}
interface ResolvedConfig {
// Vite resolved configuration containing build settings, resolvers, and options
root: string;
build: {
cssMinify: boolean | string;
ssr: boolean | string;
};
css: {
devSourcemap?: boolean;
};
createResolver: (options: any) => (id: string, base: string, skipSelf?: boolean, ssr?: boolean) => Promise<string | false | undefined>;
resolve: any;
}The returned plugin array contains three plugins:
@tailwindcss/vite:scan - Pre-enforcement plugin that:
@tailwindcss/vite:generate:serve - Development mode plugin that:
@tailwindcss/vite:generate:build - Production build plugin that:
The plugin automatically processes CSS files that match these criteria:
.css extension&lang.css query parameter?index=\d+\.css$)The plugin excludes:
?worker, ?sharedworker, ?raw, ?url)?commonjs-proxy).vite directoryThe plugin leverages several Tailwind CSS workspace packages:
^5.2.0 || ^6 || ^7 - Required Vite version for compatibility