Skip to content

Commit 9593d10

Browse files
committed
Proper daisyUI support.
1 parent 2532222 commit 9593d10

File tree

8 files changed

+104
-12
lines changed

8 files changed

+104
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* If it is a relative path, this path is considered relative to `settings.BASE_DIR`.
2828
* The last option is an absolute path.
2929

30+
- Proper daisyUI support using [tailwind-cli-extra](https://github.com/dobicinaitis/tailwind-cli-extra).
31+
3032
## 4.1.0
3133

3234
- TAILWIND_CLI_VERSION defaults to "latest" now. In case it is set to this value,

docs/settings.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ your project.
6565
> TAILWIND_CLI_ASSET_NAME = "tailwindcss-extra"
6666
> ```
6767

68-
6968
`TAILWIND_CLI_SRC_CSS`
7069
**Default**: `None`
7170

@@ -82,3 +81,21 @@ your project.
8281

8382
The name of the output file. This file is stored relative to the first element of the
8483
`STATICFILES_DIRS` array.
84+
85+
`TAILWIND_CLI_USE_DAISY_UI`:
86+
: **Default**: `False`
87+
88+
This switch determines what content is written to `TAILWIND_CLI_SRC_CSS` if it is automatically created by the library.
89+
90+
The default is:
91+
```css
92+
@import "tailwindcss";
93+
```
94+
95+
If `TAILWIND_CLI_USE_DAISY_UI = True` is put into the `settings.py` of your project, this is the output:
96+
```css
97+
@import "tailwindcss";
98+
@plugin "daisyui";
99+
```
100+
101+
This switch can also be used as a shortcut to activate daisyUI and change `TAILWIND_CLI_SRC_REPO` and `TAILWIND_CLI_ASSET_NAME` as described above to fetch [tailwind-cli-extra](https://github.com/dobicinaitis/tailwind-cli-extra/releases/latest/).

docs/usage.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,32 @@ tailwind-sidecar:
105105
106106
## Use with daisyUI
107107
108-
If you plan to use the [daisyUI](https://daisyui.com) components in your project, I highly recommend the custom CLI build [tailwind-cli-extra](https://github.com/dobicinaitis/tailwind-cli-extra) by [Andris Dobičinaitis](https://github.com/dobicinaitis).
108+
If you plan to use [daisyUI](https://daisyui.com), there is an easy way to solve this with this library.
109109
110-
Just add the following settings to your `settings.py`file.
110+
```python
111+
TAILWIND_CLI_USE_DAISY_UI = True
112+
```
113+
114+
Setting this, the library switches from using the default TailwindCSS CLI to the one provided by [Andris Dobičinaitis](https://github.com/dobicinaitis) and his [tailwind-cli-extra](https://github.com/dobicinaitis/tailwind-cli-extra) project. It also causes the library to create a proper default config that activates the daisyUI plugin.
115+
116+
But of course you can do it manually, too. Just configure a repository where the library should pull the CLI from and activate the daisyUI support.
111117
112118
```python
113119
TAILWIND_CLI_SRC_REPO = "dobicinaitis/tailwind-cli-extra"
114120
TAILWIND_CLI_ASSET_NAME = "tailwindcss-extra"
121+
TAILWIND_CLI_USE_DAISY_UI = True
115122
```
123+
124+
Or provide your custom configuration, too.
125+
126+
```python
127+
TAILWIND_CLI_SRC_REPO = "dobicinaitis/tailwind-cli-extra"
128+
TAILWIND_CLI_ASSET_NAME = "tailwindcss-extra"
129+
TAILWIND_CLI_SRC_CSS = "etc/source.css"
130+
```
131+
132+
## Use with WhiteNoise
133+
134+
If you are using [WhiteNoise](https://whitenoise.readthedocs.io/en/latest/) to serve your static assets, you must not put your custom Tailwind configuration file inside any of the directories for static files. WhiteNoise stumbles across the `@import "tailwindcss";` statement, because it can't resolve it.
135+
136+
If you want to use a custom configuration for Tailwind CSS, put it somewhere else in the project.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ known-first-party = ["django_tailwind_cli"]
122122
ban-relative-imports = "all"
123123

124124
[tool.ruff.lint.per-file-ignores]
125-
"tests/**/*" = ["PLR2004", "S101", "TID252", "ARG001"]
125+
"tests/**/*" = ["PLR2004", "S101", "TID252", "ARG001", "FBT001"]
126126
"tests/snapshots/*" = ["ALL"]
127127
"**/migrations/*" = ["ALL"]
128128

src/django_tailwind_cli/config.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Config:
2222
dist_css_base: str
2323
src_css: Optional[Path]
2424
automatic_download: bool = True
25+
use_daisy_ui: bool = False
2526

2627
@property
2728
def watch_cmd(self) -> list[str]:
@@ -62,14 +63,18 @@ def get_version() -> tuple[str, Version]:
6263
ValueError: If the TAILWIND_CLI_SRC_REPO setting is None when the version is set to
6364
"latest".
6465
"""
66+
use_daisy_ui = getattr(settings, "TAILWIND_CLI_USE_DAISY_UI", False)
6567
version_str = getattr(settings, "TAILWIND_CLI_VERSION", "latest")
66-
repo_url = getattr(settings, "TAILWIND_CLI_SRC_REPO", "tailwindlabs/tailwindcss")
68+
repo_url = getattr(
69+
settings,
70+
"TAILWIND_CLI_SRC_REPO",
71+
"tailwindlabs/tailwindcss" if not use_daisy_ui else "dobicinaitis/tailwind-cli-extra",
72+
)
6773
if not repo_url:
6874
raise ValueError("TAILWIND_CLI_SRC_REPO must not be None.")
6975

7076
if version_str == "latest":
7177
r = requests.get(f"https://github.com/{repo_url}/releases/latest/", timeout=2)
72-
print(r.headers)
7378
if r.ok and "location" in r.headers:
7479
version_str = r.headers["location"].rstrip("/").split("/")[-1].replace("v", "")
7580
return version_str, Version.parse(version_str)
@@ -90,6 +95,9 @@ def get_config() -> Config:
9095
if settings.STATICFILES_DIRS is None or len(settings.STATICFILES_DIRS) == 0:
9196
raise ValueError("STATICFILES_DIRS is empty. Please add a path to your static files.")
9297

98+
# daisyUI support
99+
use_daisy_ui = getattr(settings, "TAILWIND_CLI_USE_DAISY_UI", False)
100+
93101
# Determine the system and machine we are running on
94102
system = platform.system().lower()
95103
system = "macos" if system == "darwin" else system
@@ -107,7 +115,11 @@ def get_config() -> Config:
107115
version_str, version = get_version()
108116

109117
# Determine the asset name
110-
if not (asset_name := getattr(settings, "TAILWIND_CLI_ASSET_NAME", "tailwindcss")):
118+
if not (
119+
asset_name := getattr(
120+
settings, "TAILWIND_CLI_ASSET_NAME", "tailwindcss" if not use_daisy_ui else "tailwindcss-extra"
121+
)
122+
):
111123
raise ValueError("TAILWIND_CLI_ASSET_NAME must not be None.")
112124

113125
# Determine the full path to the CLI
@@ -121,7 +133,11 @@ def get_config() -> Config:
121133
cli_path = cli_path.expanduser() / f"{asset_name}-{system}-{machine}-{version_str}{extension}"
122134

123135
# Determine the download url for the cli
124-
repo_url = getattr(settings, "TAILWIND_CLI_SRC_REPO", "tailwindlabs/tailwindcss")
136+
repo_url = getattr(
137+
settings,
138+
"TAILWIND_CLI_SRC_REPO",
139+
"tailwindlabs/tailwindcss" if not use_daisy_ui else "dobicinaitis/tailwind-cli-extra",
140+
)
125141
download_url = (
126142
f"https://github.com/{repo_url}/releases/download/v{version_str}/{asset_name}-{system}-{machine}{extension}"
127143
)
@@ -154,4 +170,5 @@ def get_config() -> Config:
154170
dist_css_base=dist_css_base,
155171
src_css=src_css,
156172
automatic_download=automatic_download,
173+
use_daisy_ui=use_daisy_ui,
157174
)

src/django_tailwind_cli/management/commands/tailwind.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ def _download_cli(*, force_download: bool = False) -> None:
279279

280280

281281
DEFAULT_SOURCE_CSS = '@import "tailwindcss";\n'
282+
DAISY_UI_SOURCE_CSS = '@import "tailwindcss";\n@plugin "daisyui";\n'
282283

283284

284285
def _create_standard_config() -> None:
@@ -287,7 +288,10 @@ def _create_standard_config() -> None:
287288

288289
if c.src_css and not c.src_css.exists():
289290
c.src_css.parent.mkdir(parents=True, exist_ok=True)
290-
c.src_css.write_text(DEFAULT_SOURCE_CSS)
291+
if c.use_daisy_ui:
292+
c.src_css.write_text(DAISY_UI_SOURCE_CSS)
293+
else:
294+
c.src_css.write_text(DEFAULT_SOURCE_CSS)
291295
typer.secho(
292296
f"Created Tailwind Source CSS at '{c.src_css}'",
293297
fg=typer.colors.GREEN,

tests/test_config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,26 @@ def test_watch_cmd():
227227
str(c.dist_css),
228228
"--watch",
229229
]
230+
231+
232+
def test_daisy_ui_support(
233+
settings: SettingsWrapper,
234+
mocker: MockerFixture,
235+
):
236+
settings.TAILWIND_CLI_USE_DAISY_UI = True
237+
request_get = mocker.patch("requests.get")
238+
request_get.return_value.headers = {
239+
"location": "https://github.com/dobicinaitis/tailwind-cli-extra/releases/tag/v2.1.4"
240+
}
241+
242+
c = get_config()
243+
244+
assert c.use_daisy_ui
245+
assert "tailwindcss-extra" in str(c.cli_path)
246+
assert "dobicinaitis/tailwind-cli-extra" in c.download_url
247+
248+
r_version_str, r_version = get_version()
249+
assert r_version_str == "2.1.4"
250+
assert r_version.major == 2
251+
assert r_version.minor == 1
252+
assert r_version.patch == 4

tests/test_management_commands.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pytest_mock import MockerFixture
99

1010
from django_tailwind_cli.config import get_config
11-
from django_tailwind_cli.management.commands.tailwind import DEFAULT_SOURCE_CSS
11+
from django_tailwind_cli.management.commands.tailwind import DAISY_UI_SOURCE_CSS, DEFAULT_SOURCE_CSS
1212

1313

1414
@pytest.fixture(autouse=True, params=["4.0.0"])
@@ -32,13 +32,21 @@ def test_calling_unknown_subcommand():
3232
call_command("tailwind", "not_a_valid_command")
3333

3434

35-
def test_create_src_css_if_non_exists(settings: LazySettings, tmp_path: Path):
35+
@pytest.mark.parametrize(
36+
"use_daisy_ui",
37+
[True, False],
38+
)
39+
def test_create_src_css_if_non_exists(settings: LazySettings, tmp_path: Path, use_daisy_ui: bool):
40+
settings.TAILWIND_CLI_USE_DAISY_UI = use_daisy_ui
3641
c = get_config()
3742
assert c.src_css is not None
3843
assert not c.src_css.exists()
3944
call_command("tailwind", "build")
4045
assert c.src_css.exists()
41-
assert DEFAULT_SOURCE_CSS == c.src_css.read_text()
46+
if use_daisy_ui:
47+
assert DAISY_UI_SOURCE_CSS == c.src_css.read_text()
48+
else:
49+
assert DEFAULT_SOURCE_CSS == c.src_css.read_text()
4250

4351

4452
def test_with_existing_src_css(settings: LazySettings, tmp_path: Path):

0 commit comments

Comments
 (0)