Tailwinding Django
output.css
The File
The goal is to finalize an output.css
definition file covering utility classes declared in templates.
/src/templates/base.html | |
---|---|
Tailwind's build step is intended to be running the entire time during development.
So while I'm modifying applicable templates, the build step will constantly change the output.css
file.
As an example, when I add a never-before used utility class, e.g. bg-orange-400
to a Tailwind-discoverable template fragment, the build process will modify the output.css
so that the definition of bg-orange-500
is created in the output.css
file (which is then cascaded by base.html
to the entire website.)
The Templates
The question then arises, how do I make the files discoverable so that definitions are automatically added?
This is a partial fragment of the present settings:
/src/config/settings/_settings.py | |
---|---|
- In the event that I use Tailwind classes in js files, which are found in
/src/static/js
, I'll want Tailwind to automatically create the definitions. - I'll definitely be using Tailwind classes in html files and these are found in
/src/templates
.
By simple declarations in the config file, Tailwind is instructed to discover utility classes in these filetypes found in specified locations:
/tailwind.config.js | |
---|---|
- Add the locations here or follow the more dynamic approach described by Carlton Gibson
- See
STATICFILES_DIRS
(/src/static
) - See
TEMPLATES[0][DIRS]
(/src/templates
)
The Build Step
Now that we know what to look for, where to look for them, and the eventual destination of the definitions... how do we start the process? Tailwind's build process command, touched on earlier, needs to run separately from Django's runserver
command.
npx tailwindcss \ # build process
-i ./static/css/input.css \ # (1)
-o ./static/css/output.css \ # (2)
--watch # will run in the background
-i
is shorthand for--input
-o
is shorthand for--output
It will simply watch for changes made in Tailwind's required input.css
and the content
location values found in tailwind.config.js
. Every time a change is made, the output.css
file is either created or updated.
Since the command is a bit verbose, I've created a shortcut with just tw
.
Installation
TailwindCSS Native
django-tailwind integrates better but I prefer the manual process in case I need to make adjustments later on.
Reminders Only
The steps here are outlined so that I'm reminded on how I prepared the present tailwind.config.js
Install /node_modules
The node_modules
folder is hosted in the root project folder. These our necessary to continuously generate the output.css
:
npm install -D tailwindcss \
@tailwindcss/typography \
@tailwindcss/forms \
@tailwindcss/aspect-ratio \
@tailwindcss/container-queries # (1)
- Check that the
/node_modules
contains the plugin folders.
To prevent git inclusion of the /node_modules
folder, add to the root folder's .gitignore
file:
Create config file
Initialize the configuration file.
Add installed node_modules
Add plugins installed above to the config file.
/tailwind.config.js | |
---|---|
Entrypoint input.css
For the output.css
to be built, I need to ensure an input.css
, conventional name recommended in Tailwind's installation docs.
I create this under ./static/
with basic directives (but modify it later on):
Compression
Generating output.css
covering Django html templates and other static files will likely result in multiple, large files. This can be optimized. django-compressor
is an optimization tool to make multiple files into a single cacheable unit. Though this also applies to javascript files, the following documentation relates to the output.css
produced by TailwindCSS.
Install compressor
- Where all the staticfiles are gathered after running
python manage.py collectstatic --noinput
- This is where the
/CACHE
folder will be created, i.e.src/static/CACHE
. So after runningpython manage.py compress
, we can expect/css
and/js
subfolders under/CACHE
- Allow compression even when
DEBUG
is True. SeeCOMPRESS_ENABLED
setting indjango-compressor
docs. - "In case you use Django's staticfiles contrib app you have to add Django Compressor's file finder to the
STATICFILES_FINDERS
setting" -django-compressor
quickstart.
Use compress tag
The tailwind-generated output.css
file in the base.html
will now be constantly compressed into a cached, optimized file. See example result.
- Allows use of
{% compress css %}
and{% compress js %}
tags. - All stylesheets included between the
{% compress css %}
and{% endcompress %}
tags will be compressed into a single/static/CACHE/css
file.
Collection before compression
# while in /src; note usage in scripts/web.sh and scripts/run.sh
python manage.py collectstatic --noinput # (1)
python manage.py compress --force # (2)
- Place files in the
STATIC_ROOT
folder,--noinput
flag implies the user is not prompted for any kind of input. - If
COMPRESS_OFFLINE
isFalse
(default), can usepython manage.py compress --force
to override. Thecompress
management command produces themanifest.json
Based on the above flow, static files from many sources (including the admin) are collected and then subsequently compressed into a single css / js file found in the static/CACHE
, mapped out via a generated manifest.json
file.