I'm integrating Tailwind CSS v4 with Django using Vite, but I'm facing an issue where Tailwind only detects classes from the Vite app and does not recognize new classes added in Django templates.
What I’ve Done So Far:
import { defineConfig } from 'vite';
import { resolve } from 'path';
import tailwindcss from '@tailwindcss/vite';
export default defineConfig({
plugins: [
tailwindcss(),
],
base: '/static/', // Important for Django to locate assets
build: {
manifest: true,
emptyOutDir: true,
outDir: resolve(__dirname, 'dist'), // Ensure the output folder is 'dist'
rollupOptions: {
input: {
tailwind: resolve(__dirname, 'src/style.css'),
},
},
},
server: {
watch: {
usePolling: true, // Ensures Django templates are watched
ignored: ['!../templates/**/*'], // Make sure templates are included
},
},
});
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [
BASE_DIR / "webapp/dist" # Matches vite config!
]
DJANGO_VITE = {
"default": {
"dev_mode": DEBUG,
}
}
<!-- templates/home.html -->
{% load django_vite %}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tailwind 🤝🏻 Django</title>
{% vite_hmr_client %}
<link rel="stylesheet" href="{% vite_asset_url 'src/style.css' %}" />
</head>
<body>
<h1 class="p-8 text-6xl inline-block font-extrabold text-transparent bg-clip-text bg-gradient-to-r from-blue-500 to-red-500">
Tailwind and Django!
</h1>
<h1 class="text-red-700 text-3xl">geeg</h1>
</body>
</html>
@import "tailwindcss";
The Issue:
What I Need Help With:
How do I correctly specify Django template paths in Tailwind v4 (without tailwind.config.js)?
This is my repo if you want to take a look Github repo
Any help would be greatly appreciated! 🚀
As you surmised, when using the Vite plugin, Tailwind will only scan files in Vite's module graph for class names to generate. Your Django templates wouldn't be in this module graph since they are not handled by Vite.
To work around this, you can use the @source
directive to explicitly point to more files that Tailwind should scan:
/* webapp/src/index.css */
@import "tailwindcss";
@source "../../templates";