Skip to content

Commit 529fad2

Browse files
committed
Adds Standalone CLI support: Use Django-Tailwind CSS without Node.js
1 parent e772a8b commit 529fad2

File tree

30 files changed

+891
-227
lines changed

30 files changed

+891
-227
lines changed

β€Ž.pre-commit-config.yamlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ repos:
5050
- id: ruff-check
5151
args: [ --fix ]
5252
- id: ruff-format
53-
exclude: 'src/tailwind/app_template_v3/{{cookiecutter.app_name}}/apps.py|src/tailwind/app_template_v4/{{cookiecutter.app_name}}/apps.py|src/tailwind/app_template_v4/{{cookiecutter.app_name}}/static_src/package.json'
53+
exclude: 'src/tailwind/app_template_v3/{{cookiecutter.app_name}}/apps.py|src/tailwind/app_template_v4/{{cookiecutter.app_name}}/apps.py|src/tailwind/app_template_v4/{{cookiecutter.app_name}}/static_src/package.json|src/tailwind/app_template_v4_standalone/{{cookiecutter.app_name}}/apps.py'

β€ŽCHANGELOG.mdβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 4.4.0 (Unreleased)
4+
5+
- Standalone Tailwind CSS binary support via [pytailwindcss](https://github.com/timonweb/pytailwindcss);
6+
- `TAILWIND_USE_STANDALONE_BINARY` setting to force standalone binary mode;
7+
- `TAILWIND_STANDALONE_BINARY_VERSION` setting to control standalone binary version (default: `v4.1.16`);
8+
- Automatic detection of standalone vs npm-based installations (checks for `package.json` presence);
9+
- `app_template_v4_standalone/` cookiecutter template for standalone binary apps;
10+
- Updates documentation;
11+
- Node.js is now optional when using standalone binary mode;
12+
313
## 4.3.0
414

515
- Replaces Poetry with UV for dependency management;

β€ŽCLAUDE.mdβ€Ž

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
66

77
Django-Tailwind is a Python package that integrates Tailwind CSS with Django applications. It provides management commands, template tags, and utilities to streamline Tailwind CSS development within Django projects.
88

9+
The package supports two installation modes:
10+
- **npm-based:** Traditional approach using Node.js and npm for maximum flexibility (plugins, PostCSS, etc.)
11+
- **Standalone binary:** Simplified approach using the Tailwind CSS standalone binary via pytailwindcss (no Node.js required)
12+
913
## Development Commands
1014

1115
### Testing
@@ -76,9 +80,11 @@ python manage.py tailwind build
7680

7781
- **Management Command** (`src/tailwind/management/commands/tailwind.py`):
7882
- Main entry point for all Tailwind operations
79-
- Handles `init`, `install`, `build`, `start`, `dev`, `check-updates`, `update` commands
83+
- Handles `init`, `install`, `build`, `start`, `dev`, `check-updates`, `update`, `plugin_install` commands
8084
- Uses cookiecutter templates for app initialization
8185
- `dev` command runs Django server and Tailwind watcher simultaneously using Honcho
86+
- Automatically detects and routes between npm-based and standalone binary modes
87+
- Standalone detection checks for `package.json` presence or `TAILWIND_USE_STANDALONE_BINARY` setting
8288

8389
- **NPM Integration** (`src/tailwind/npm.py`):
8490
- Wrapper around npm commands
@@ -96,23 +102,55 @@ python manage.py tailwind build
96102

97103
### App Templates
98104

99-
Two cookiecutter templates are provided:
100-
- `app_template_v3/` - For Tailwind CSS v3
101-
- `app_template_v4/` - For Tailwind CSS v4
105+
Three cookiecutter templates are provided:
106+
- `app_template_v3/` - For Tailwind CSS v3 (npm-based)
107+
- `app_template_v4/` - For Tailwind CSS v4 (npm-based)
108+
- `app_template_v4_standalone/` - For Tailwind CSS v4 standalone binary
102109

103-
These templates generate Django apps with:
110+
**npm-based templates** (`v3` and `v4`) generate Django apps with:
104111
- `static_src/` directory for source files
105112
- `package.json` with build scripts
106113
- PostCSS configuration
107114
- Base HTML template
108115

116+
**Standalone template** (`v4_standalone`) generates minimal Django apps with:
117+
- `static_src/src/` directory with styles.css
118+
- No `package.json` or npm dependencies
119+
- No PostCSS configuration
120+
- Base HTML template
121+
- Pre-created `static/` directory structure
122+
109123
### Configuration
110124

111125
The package uses Django settings for configuration:
112126
- `TAILWIND_APP_NAME` - Name of the Tailwind app
113127
- `TAILWIND_CSS_PATH` - Path to compiled CSS
114-
- `TAILWIND_DEV_MODE` - Development mode flag
115-
- `NPM_BIN_PATH` - Custom npm binary path
128+
- `TAILWIND_DEV_MODE` - Development mode flag (deprecated)
129+
- `NPM_BIN_PATH` - Custom npm binary path (npm-based only)
130+
- `TAILWIND_USE_STANDALONE_BINARY` - Force standalone binary mode (default: False, auto-detected)
131+
- `TAILWIND_STANDALONE_BINARY_VERSION` - Tailwind CSS standalone binary version (default: "v4.1.16")
132+
133+
### Standalone Binary Implementation
134+
135+
The standalone binary mode uses [pytailwindcss](https://github.com/timonweb/pytailwindcss) to run the Tailwind CSS standalone binary without requiring Node.js.
136+
137+
**How it works:**
138+
1. Detection happens in the management command via `self.is_standalone` flag
139+
2. Checks for `TAILWIND_USE_STANDALONE_BINARY` setting OR absence of `package.json`
140+
3. Routes command execution to either npm methods or standalone methods
141+
142+
**Commands behavior:**
143+
- `init --tailwind-version 4s`: Creates standalone app using `app_template_v4_standalone`
144+
- `install`: Downloads standalone binary via pytailwindcss (auto-install on first use)
145+
- `build`: Runs `pytailwindcss.run()` with minify flag
146+
- `start`: Runs `pytailwindcss.run()` with watch flag
147+
- `dev`: Works with standalone mode (Procfile uses `tailwind start`)
148+
- `check-updates`, `update`, `plugin_install`: Raise CommandError (not supported in standalone)
149+
150+
**Key implementation methods in `tailwind.py`:**
151+
- `tailwind_cli_install_command()`: Downloads binary via pytailwindcss
152+
- `tailwind_cli_build_command()`: Builds CSS with standalone binary
153+
- `tailwind_cli_start_command()`: Starts watcher with standalone binary
116154

117155
## Common Development Patterns
118156

β€ŽREADME.mdβ€Ž

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
This project provides a convenient way to integrate the *Tailwind CSS* framework into a Django project.
1515
It creates a new Django app (named theme by default) that includes all the necessary files and configurations to get
16-
started with *Tailwind CSS* quickly.
16+
started with *Tailwind CSS* quickly and **without even needing to install `Node.js`** if you choose the standalone binary mode.
1717

1818
## Features
1919

2020
* An opinionated *Tailwind CSS* setup that makes your life easier;
21+
* **Two installation modes:** standalone binary (works without `Node.js`) or npm-based (`Node.js` required);
2122
* Hot reloading of CSS, configuration files, and *Django* templates. No more manual page refreshes!
22-
* Out of the box support for CSS imports, Sass-like variables, and nesting;
23+
* Out of the box support for CSS imports and nesting;
2324
* Supports the latest *Tailwind CSS* `v4.x`;
2425
* Start both *Tailwind CSS* and *Django* development servers with a single command;
2526
* An optional DaisyUI integration to spice up your Tailwind templates with pre-built components.
@@ -29,6 +30,8 @@ started with *Tailwind CSS* quickly.
2930

3031
Python 3.11 or newer and Django 4.2.20 or newer.
3132

33+
**Note:** `Node.js` is only required if you choose the npm-based installation. The standalone binary mode does not require Node.js.
34+
3235
## Documentation
3336

3437
The full documentation is at https://django-tailwind.readthedocs.io/ or in the [docs directory](docs/index.md) of this

β€Ždocs/index.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ To avoid confusion, we'll use:
1313
Contents
1414
--------
1515
* [Installation](installation.md)
16+
* [Standalone vs npm-based](standalone-vs-npm.md)
1617
* [Usage](usage.md)
1718
* [Settings](settings.md)
1819
* [Template tags](templatetags.md)

β€Ždocs/installation.mdβ€Ž

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@
4242
python manage.py tailwind init
4343
```
4444

45-
> Note: By default, we create an app compatible with Tailwind CSS version 4. If you want to create an app compatible
46-
> with Tailwind CSS version 3, you can use the `--tailwind-version 3` flag:
45+
You will need to provide the name of the app and choose the installation method: standalone binary or npm-based.
4746

48-
```bash
49-
python manage.py tailwind init --tailwind-version 3
50-
```
47+
> See [Standalone vs npm-based](./standalone-vs-npm.md) for a detailed comparison.
5148
5249
4. Add your newly created `'theme'` app to `INSTALLED_APPS` in `settings.py`:
5350

@@ -158,7 +155,7 @@
158155
159156
## Optional configurations
160157
161-
### @source directive configuration (for Tailwind CSS v4)
158+
### @source directive configuration (for npm-based v4 and standalone v4)
162159
163160
The `content` section from Tailwind CSS v3 has been replaced with the `@source` directive in Tailwind CSS v4.
164161
The `@source` directive is a new way to specify the source files that Tailwind CSS should scan for class names. It's
@@ -177,7 +174,7 @@ three directories above the `style.css` file. Depending on your project structur
177174
For more information about setting `@source`, check out the *"Source Configuration"* page of the Tailwind CSS docs:
178175
[https://tailwindcss.com/docs/detecting-classes-in-source-files#explicitly-registering-sources](https://tailwindcss.com/docs/detecting-classes-in-source-files#explicitly-registering-sources).
179176
180-
### Content (formerly Purge) rules configuration (for Tailwind CSS v3)
177+
### Content (formerly Purge) rules configuration (for npm-based Tailwind CSS v3 only)
181178
182179
The `content` section of your `tailwind.config.js` file is where you configure the paths to all of your HTML templates,
183180
JavaScript components, and any other source files that contain *Tailwind* class names.
@@ -211,9 +208,11 @@ HTML files (or files containing HTML content, such as `.vue` or `.jsx` files) ar
211208
For more information about setting `content`, check out the *"Content Configuration"* page of the Tailwind CSS
212209
docs: [https://tailwindcss.com/docs/content-configuration](https://tailwindcss.com/docs/content-configuration).
213210
214-
### Configuration of the path to the `npm` executable
211+
### Configuration of the path to the `npm` executable (npm-based installation only)
212+
213+
> **Note:** This section only applies to npm-based installations. Skip if using the standalone binary mode.
215214
216-
*Tailwind CSS* requires *Node.js* to be installed on your machine.
215+
For npm-based installations, *Tailwind CSS* requires *Node.js* to be installed on your machine.
217216
*Node.js* is a *JavaScript* runtime that allows you to run *JavaScript* code outside the browser. Most (if not all) of
218217
the current frontend tools depend on *Node.js*.
219218

β€Ždocs/plugins.mdβ€Ž

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# Installing Tailwind CSS Plugins
22

3+
> **Important:** Plugin installation via the `plugin_install` command is **only available for npm-based installations**. If you're using the standalone binary mode (initialized with `--tailwind-version 4s`), the `plugin_install` command is not supported. Standalone installations are limited to core Tailwind CSS features only.
4+
35
Django Tailwind provides built-in support for managing Tailwind CSS plugins. This guide covers how to install, configure, and manage plugins in your Tailwind CSS projects.
46

57
## Overview
68

79
Tailwind CSS plugins extend the framework's functionality by adding new utilities, components, and base styles. Django Tailwind makes it easy to install and configure these plugins through management commands.
810

9-
## Installing Plugins
11+
## Installing Plugins (npm-based Installation Only)
12+
13+
> **Note:** All commands in this section require an npm-based installation. These commands will not work with standalone binary installations.
1014
1115
### Using the plugin_install Command
1216

@@ -106,11 +110,11 @@ python manage.py tailwind plugin_install @tailwindcss/forms
106110
- Better default appearance for form controls
107111
- Easy to customize with Tailwind utilities
108112

109-
## Installing Plugins During Project Initialization
113+
## Installing Plugins During Project Initialization (npm-based Only)
110114

111115
### DaisyUI Integration
112116

113-
For Tailwind v4 projects, you can include DaisyUI directly during project initialization:
117+
For npm-based Tailwind v4 projects, you can include DaisyUI directly during project initialization:
114118

115119
```bash
116120
python manage.py tailwind init --include-daisy-ui

β€Ždocs/settings.mdβ€Ž

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ python manage.py tailwind init
1010
```
1111
Please refer to the [Installation](installation.md) section for more information on the installation process.
1212

13-
## `TAILWIND_DEV_MODE` (deprecated)
14-
Determines whether the `browser-sync` snippet is added to the page via the `{% tailwind_css %}` tag. It is set to `False` by default. If you use a legacy pre-`3.1.0` configuration and rely on `browser-sync`, add `TAILWIND_DEV_MODE=True` to your `settings.py`.
13+
## `NPM_BIN_PATH` (npm-based installation only)
14+
15+
> **Note:** This setting only applies to npm-based installations. Skip if using the standalone binary mode.
1516
16-
## `NPM_BIN_PATH`
1717
This defines the path to the `npm` executable on your system.
1818

1919
> *Tailwind CSS* requires you to have *Node.js* installed on your machine.
@@ -32,6 +32,98 @@ Please note that on *Windows* the path might look different (pay attention to th
3232
NPM_BIN_PATH = r"C:\Program Files\nodejs\npm.cmd"
3333
```
3434

35+
## `TAILWIND_USE_STANDALONE_BINARY`
36+
37+
This setting determines whether to use the Tailwind CSS standalone binary instead of npm-based installation.
38+
39+
The default value is:
40+
```python
41+
TAILWIND_USE_STANDALONE_BINARY = False
42+
```
43+
44+
When set to `True`, Django Tailwind will use the [pytailwindcss](https://github.com/timonweb/pytailwindcss) package to run the Tailwind CSS standalone binary. This eliminates the need for Node.js and npm.
45+
46+
> **Note:** In most cases, you don't need to set this manually. Django Tailwind automatically detects standalone installations by checking for the presence of `package.json` in your theme app. If you initialized your app with `--tailwind-version 4s`, this detection happens automatically.
47+
48+
To explicitly enable standalone mode:
49+
50+
```python
51+
# settings.py
52+
TAILWIND_USE_STANDALONE_BINARY = True
53+
```
54+
55+
## `TAILWIND_STANDALONE_BINARY_VERSION`
56+
57+
This setting specifies which version of the Tailwind CSS standalone binary to use.
58+
59+
The default value is:
60+
```python
61+
TAILWIND_STANDALONE_BINARY_VERSION = "v4.1.16"
62+
```
63+
64+
You can specify any valid Tailwind CSS version tag. To upgrade to a newer version:
65+
66+
```python
67+
# settings.py
68+
TAILWIND_STANDALONE_BINARY_VERSION = "v4.2.0"
69+
```
70+
71+
After changing this setting, run `python manage.py tailwind install` to download the new binary version.
72+
73+
> **Note:** This setting only applies when using standalone binary mode. For npm-based installations, the version is controlled by the `package.json` file in your theme app.
74+
75+
**Finding available versions:**
76+
77+
Visit the [Tailwind CSS releases page](https://github.com/tailwindlabs/tailwindcss/releases) to see all available versions. Use the tag name (e.g., `v4.1.16`) as the value for this setting.
78+
79+
## `TAILWIND_STANDALONE_START_COMMAND_ARGS`
80+
81+
> **Note:** This setting only applies when using standalone binary mode.
82+
83+
This setting defines the command-line arguments passed to the Tailwind CSS standalone binary when running in watch mode (via `python manage.py tailwind start`).
84+
85+
The default value is:
86+
```python
87+
TAILWIND_STANDALONE_START_COMMAND_ARGS = (
88+
"-i static_src/src/styles.css -o static/css/dist/styles.css --watch"
89+
)
90+
```
91+
92+
You can customize this to use different input/output paths or add additional Tailwind CLI options:
93+
94+
```python
95+
# settings.py
96+
TAILWIND_STANDALONE_START_COMMAND_ARGS = (
97+
"-i theme/static_src/input.css -o theme/static/output.css --watch --minify"
98+
)
99+
```
100+
101+
> **Note:** The output path automatically uses the value from `TAILWIND_CSS_PATH` in the default configuration. If you override this setting, ensure your paths are correct.
102+
103+
## `TAILWIND_STANDALONE_BUILD_COMMAND_ARGS`
104+
105+
> **Note:** This setting only applies when using standalone binary mode.
106+
107+
This setting defines the command-line arguments passed to the Tailwind CSS standalone binary when building for production (via `python manage.py tailwind build`).
108+
109+
The default value is:
110+
```python
111+
TAILWIND_STANDALONE_BUILD_COMMAND_ARGS = (
112+
"-i static_src/src/styles.css -o static/css/dist/styles.css --minify"
113+
)
114+
```
115+
116+
You can customize this to use different input/output paths or modify build options:
117+
118+
```python
119+
# settings.py
120+
TAILWIND_STANDALONE_BUILD_COMMAND_ARGS = (
121+
"-i theme/static_src/input.css -o theme/static/output.css --minify --optimize"
122+
)
123+
```
124+
125+
> **Note:** The output path automatically uses the value from `TAILWIND_CSS_PATH` in the default configuration. If you override this setting, ensure your paths match your project structure.
126+
35127
## `TAILWIND_CSS_PATH`
36128
This defines the path to the generated *Tailwind CSS* stylesheet. If you created a theme app via the `python manage.py tailwind init` command, you likely don't need to change this value.
37129

@@ -41,3 +133,6 @@ The default value is:
41133
```python
42134
TAILWIND_CSS_PATH = "css/dist/styles.css"
43135
```
136+
137+
## `TAILWIND_DEV_MODE` (deprecated)
138+
Determines whether the `browser-sync` snippet is added to the page via the `{% tailwind_css %}` tag. It is set to `False` by default. If you use a legacy pre-`3.1.0` configuration and rely on `browser-sync`, add `TAILWIND_DEV_MODE=True` to your `settings.py`.

β€Ždocs/standalone-vs-npm.mdβ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Standalone vs npm-based installation
2+
3+
Django Tailwind offers two methods for integrating Tailwind CSS into your Django project: using an npm-based setup or a
4+
standalone binary. Each method has its own advantages and trade-offs. The table below compares the two approaches to help
5+
you decide which one is best suited for your needs.
6+
7+
| Feature | npm-based | Standalone Binary |
8+
|----------------------|---------------------------|-----------------------|
9+
| **Node.js Required** | βœ… Yes | ❌ No |
10+
| **Setup Complexity** | Moderate | Simple |
11+
| **Tailwind Version** | v3 or v4 | v4 only |
12+
| **Plugin Support** | βœ… Full | ❌ None |
13+
| **DaisyUI Support** | βœ… Yes | ❌ No |
14+
| **Custom PostCSS** | βœ… Yes | ❌ No |
15+
| **Plugin Install** | `tailwind plugin_install` | ❌ Not available |
16+
| **package.json** | βœ… Created | ❌ Not created |
17+
| **node_modules** | βœ… Created | ❌ Not created |
18+
| **Best For** | Teams, complex projects | Solo, simple projects |
19+
20+
**Choose npm-based if you:**
21+
- Need Tailwind plugins (DaisyUI)
22+
- Want to customize PostCSS configuration
23+
- Prefer managing dependencies with package.json
24+
- Need the plugin management commands
25+
26+
**Choose standalone binary if you:**
27+
- Want the simplest possible setup
28+
- Don't have or don't want to install Node.js
29+
- Don't need plugins or advanced customization
30+
- Are building a basic project with core Tailwind features

0 commit comments

Comments
Β (0)