I will show how it is implemented for me and works with optimization.
Create frontend dir in in the root directory of the project:
├── backend
│ ├── backend
│ │ ├── asgi.py
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ └── manage.py
├── frontend
└── venv
install tailwind (see docs)
cd frontend
npm install -D tailwindcss postcss postcss-cli autoprefixer cssnano
configurate tailwind:
package.json
"scripts": {
"build": "postcss app/css/main.css -o app/css/main.min.css"
},
"devDependencies": {
"autoprefixer": "^10.4.8",
"postcss": "^8.4.16",
"postcss-cli": "^10.0.0",
"tailwindcss": "^3.1.8"
}
}
create postcss.config.js:
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
cssnano:{ # optimizes for production
preset: 'default'
},
}
}
create tailwind.config.js:
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: 'class',
content: ["../backend/*/templates/*.{html, js}", "../backend/*/templates/components/*.{html, js}"],
theme: {
extend: {},
},
plugins: []
}
create a css folder:
mkdir app
cd app
mkdir css
cd css
Inside css folder create main.css and cofigurate in:
main.css
@tailwind base;
@tailwind components;
@tailwind utilities;
Now will back and run the script:
cd ../../
npm run bild
This will create a minified css file in your css folder (main.min.css)
And configurate settings.py:
CORE_DIR = Path(__file__).resolve().parent.parent.parent
FRONT_DIR = CORE_DIR / 'frontend'
STATICFILES_DIRS = FRONT_DIR / 'app/'
Now to call static use in your template:
base.html
{% load static %}
<link rel="stylesheet" href="{% static 'css/main.min.css' %}">
The use of cssnano is recommended by the tailwind. Purge not supported in new versions. see docs