Sub topic 4
Add the plugin to vite.config
This chapter assumes you have a Vite project with tailwindcss and @tailwindcss/vite already installed, and that you can read a basic JavaScript or TypeScript module. You will register the Tailwind plugin so Vite can process Tailwind in your CSS.
What the plugin does
Vite builds your app through a pipeline of plugins. Each plugin can transform files as they pass through. The @tailwindcss/vite plugin hooks into that pipeline and handles your CSS files. When it finds Tailwind directives in a stylesheet, it replaces them with generated CSS. It also scans your project files for class names, so only the utilities you use end up in the output.
Installing the package alone does nothing. Vite only runs plugins that are listed in its config file, so you must register the plugin there.
Edit the config file
Open vite.config.ts in the project root. If your project was created from a JavaScript template, the file is vite.config.js. The content is the same.
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [
tailwindcss(),
],
})The file does three things:
- It imports
defineConfigfrom Vite. This helper gives your editor type hints for the config object. - It imports the Tailwind plugin as the default export of
@tailwindcss/vite. The nametailwindcssis your choice, but it is the common one. - It adds the plugin to the
pluginsarray. You must call it as a function,tailwindcss(). Passingtailwindcsswithout the parentheses registers the function itself instead of a plugin object.
If you already have other plugins
Most real projects already use a framework plugin. Add Tailwind to the same array. For a React project, the config looks like this:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [
react(),
tailwindcss(),
],
})The order rarely matters here, because the two plugins work on different kinds of files. Keep the framework plugin first, as the Tailwind documentation does.
What you do not need
In v4, a typical project needs none of the following for the Vite setup:
- A
tailwind.config.jsfile. - A
postcss.config.jsfile. - An
autoprefixerpackage.
The Vite plugin covers the vendor prefixing and import handling that those tools used to provide. If you have leftover v3 files, they can cause confusion, which a later chapter on differences from v3 covers.
Common mistakes
- Wrong file name. Vite also accepts
vite.config.mtsorvite.config.mjs. If two config files exist, check which one Vite loads. - Missing parentheses. Write
tailwindcss(), nottailwindcss. - Package not installed. An error such as "Cannot find module '@tailwindcss/vite'" means the install step was skipped or run in a different folder. Run
npm install tailwindcss @tailwindcss/vitefrom the project root. - Config not reloaded. Vite restarts itself when it detects a config change. If styles still do not appear, stop the dev server and start it again.
Exercise
A teammate registers the plugin as plugins: [tailwindcss] in vite.config.ts. What is wrong, and how do you fix it?
Show answer
The plugin is a function that returns the plugin object, so it must be called. The fix is plugins: [tailwindcss()]. Without the call, Vite receives the function itself and does not run Tailwind on your CSS.
Exercise
Why does installing @tailwindcss/vite with npm not make Tailwind work until you edit vite.config?
Show answer
Vite only runs the plugins listed in the plugins array of its config. An installed package that is not registered there is never part of the build pipeline.