Skip to content

Commit 2532222

Browse files
committed
Better handling for source.css File.
1 parent 9296666 commit 2532222

File tree

6 files changed

+58
-54
lines changed

6 files changed

+58
-54
lines changed

CHANGELOG.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@
22

33
## 4.2.0
44

5-
> [!WARNING]
5+
> [!CAUTION]
66
> Version 4.2.x and onward only support Tailwind CSS 4.x. If you are using Tailwind CSS 3.x,
77
> please use version 2.21.1 of this library. Version 4.0.x or 4.1.x still support Tailwind CSS 3.x, but
88
> these versions are not recommended to use for new projects.
99
10+
> [!CAUTION]
11+
> This version includes changes to the handling of `TAILWIND_CLI_PATH` and `TAILWIND_CLI_SRC_CSS`.
12+
1013
- Default fallback version is now 4.1.3.
1114
- Removed support for Tailwind CSS 3.x. The main reason is that this makes the library easier to maintain and also add better support for custom CLI builds which support DaisyUI and other plugins.
1215
- django-tailwind-cli now uses proper platform directories to store the CLI binary.
13-
- The proper directories are powered by [platformdirs](https://pypi.org/project/platformdirs/).
14-
- TAILWIND_CLI_PATH is now optional by default.
15-
- If TAILWIND_CLI_PATH is None, the binary is stored in `user_data_dir("django-tailwind-cli", "django-commons")`.
16-
- The library doesn't rely anylonger on a properly set `settings.BASE_DIR`.
17-
- TAILWIND_CLI_ASSET_NAME is also used for the downloaded CLI to distinguish between `tailwindcss` and `tailwindcss-extra`.
16+
17+
* The proper directories are powered by [platformdirs](https://pypi.org/project/platformdirs/).
18+
* `TAILWIND_CLI_PATH` is now optional by default.
19+
* If `TAILWIND_CLI_PATH` is None, the binary is stored in `user_data_dir("django-tailwind-cli", "django-commons")`.
20+
* The library doesn't rely anylonger on a properly set `settings.BASE_DIR`.
21+
22+
- `TAILWIND_CLI_ASSET_NAME` is also used for the downloaded CLI to distinguish between `tailwindcss` and `tailwindcss-extra`.
23+
- The behaviour of `TAILWIND_CLI_SRC_CSS` has been changed. The reasoning behind this change was, less default code inside the project and whitenoise had issues with a CSS file inside an asset folder referencing non-existing imports.
24+
25+
* It defaults to `None`.
26+
* If it is `None` the default input file is created in the user cache directory.
27+
* If it is a relative path, this path is considered relative to `settings.BASE_DIR`.
28+
* The last option is an absolute path.
1829

1930
## 4.1.0
2031

docs/settings.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ your project.
2929

3030
This allows you to override the default of the library where to store the CLI binary.
3131

32-
The default behaviour is to store the CLI binary in directory return by this call `platformdirs.user_data_dir("django-tailwind-cli", "django-commons")`. Checkout [platformdirs](https://pypi.org/project/platformdirs/) for details.
32+
The default behaviour is to store the CLI binary in the directory returned by this call `platformdirs.user_data_dir("django-tailwind-cli", "django-commons")`. Checkout [platformdirs](https://pypi.org/project/platformdirs/) for details.
3333

3434
But if you want to store it elsewhere or plan to use a custom build binary stored locally, change this setting either to a path to a directory or the full path to the binary. If it points to a directory, this is the download destination otherwise it directly tries to use the referenced binary.
3535

36-
!!! warning
37-
38-
If you use the new option from **2.7.0** but haven't installed a binary before running any of the management commands, these commands will treat the configured path as a directory and create it, if it is missing. Afterwards the official CLI will be downloaded to this path.
39-
40-
In case you want to use the new behaviour, it is highly recommended to also set the new setting `TAILWIND_CLI_AUTOMATIC_DOWNLOAD` to `False`.
36+
> [!Warning]
37+
> If you use the new option from **2.7.0** but haven't installed a binary before running any of the management commands, these commands will treat the configured path as a directory and create it, if it is missing. Afterwards the official CLI will be downloaded to this path.
38+
>
39+
> In case you want to use the new behaviour, it is highly recommended to also set the new setting `TAILWIND_CLI_AUTOMATIC_DOWNLOAD` to `False`.
4140

4241
`TAILWIND_CLI_AUTOMATIC_DOWNLOAD`
4342
: **Default**: `True`
@@ -49,9 +48,8 @@ your project.
4948

5049
Specifies the repository from which the CLI is downloaded. This is useful if you are using a customized version of the CLI, such as [tailwind-cli-extra](https://github.com/dobicinaitis/tailwind-cli-extra).
5150

52-
!!! warning
53-
54-
If you use this option, ensure that you update the `TAILWIND_CLI_VERSION` to match the version of the customized CLI you are using. Additionally, you may need to update the `TAILWIND_CLI_ASSET_NAME` if the asset name is different. See the example below.
51+
> [!Warning]
52+
> If you use this option, ensure that you update the `TAILWIND_CLI_VERSION` to match the version of the customized CLI you are using. Additionally, you may need to update the `TAILWIND_CLI_ASSET_NAME` if the asset name is different. See the example below.
5553

5654
`TAILWIND_CLI_ASSET_NAME`:
5755
: **Default**: `"tailwindcss"`
@@ -60,22 +58,24 @@ your project.
6058

6159
This option is particularly useful if the customized repository you are using has a different name for the Tailwind CLI asset. For example, the asset name for [tailwind-cli-extra](https://github.com/dobicinaitis/tailwind-cli-extra/releases/latest/) is `tailwindcss-extra`.
6260

63-
!!! Note
64-
65-
Here is a full example of using a custom repository and asset name:
61+
> [!Note]
62+
> Here is a full example of using a custom repository and asset name:
63+
> ```python
64+
> TAILWIND_CLI_SRC_REPO = "dobicinaitis/tailwind-cli-extra"
65+
> TAILWIND_CLI_ASSET_NAME = "tailwindcss-extra"
66+
> ```
6667

67-
```python
68-
TAILWIND_CLI_SRC_REPO = "dobicinaitis/tailwind-cli-extra"
69-
TAILWIND_CLI_ASSET_NAME = "tailwindcss-extra"
70-
TAILWIND_CLI_VERSION = "2.1.4"
71-
```
7268

7369
`TAILWIND_CLI_SRC_CSS`
74-
**Default** (for Tailwind 4.x): `css/source.css`
70+
**Default**: `None`
71+
72+
This variable can be set to `None`, a relative path and an absolute path.
73+
74+
If it is set to `None`, the library creates file with the name `source.css` in the directory returned by this call `platformdirs.user_cache_dir("django-tailwind-cli", "django-commons")`. Checkout [platformdirs](https://pypi.org/project/platformdirs/) for details.
75+
76+
If it is a relative path it is assumed to be relative to `settings.BASE_DIR`. If `settings.BASE_DIR` is not defined or the file doesn't exist a `ValueError` is raised.
7577

76-
For **Tailwind CSS 4.x** this required file is used to configure Tailwind CSS and also add
77-
additional CSS rules for your project. This file is stored relative to the first element of
78-
the `STATICFILES_DIRS` array.
78+
If it is an absolute path, this path is used as the input file for Tailwind CSS CLI. If the path doesn't exist, a `ValueError`is raised.
7979

8080
`TAILWIND_CLI_DIST_CSS`
8181
: **Default**: `"css/tailwind.css"`

src/django_tailwind_cli/config.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,26 @@ class Config:
2727
def watch_cmd(self) -> list[str]:
2828
result = [
2929
str(self.cli_path),
30+
"--input",
31+
str(self.src_css),
3032
"--output",
3133
str(self.dist_css),
3234
"--watch",
3335
]
3436

35-
if self.src_css:
36-
result.extend(["--input", str(self.src_css)])
37-
3837
return result
3938

4039
@property
4140
def build_cmd(self) -> list[str]:
4241
result = [
4342
str(self.cli_path),
43+
"--input",
44+
str(self.src_css),
4445
"--output",
4546
str(self.dist_css),
4647
"--minify",
4748
]
4849

49-
if self.src_css:
50-
result.extend(["--input", str(self.src_css)])
51-
5250
return result
5351

5452

@@ -123,8 +121,7 @@ def get_config() -> Config:
123121
cli_path = cli_path.expanduser() / f"{asset_name}-{system}-{machine}-{version_str}{extension}"
124122

125123
# Determine the download url for the cli
126-
if not (repo_url := getattr(settings, "TAILWIND_CLI_SRC_REPO", "tailwindlabs/tailwindcss")):
127-
raise ValueError("TAILWIND_CLI_SRC_REPO must not be None.")
124+
repo_url = getattr(settings, "TAILWIND_CLI_SRC_REPO", "tailwindlabs/tailwindcss")
128125
download_url = (
129126
f"https://github.com/{repo_url}/releases/download/v{version_str}/{asset_name}-{system}-{machine}{extension}"
130127
)
@@ -135,9 +132,14 @@ def get_config() -> Config:
135132
dist_css = Path(settings.STATICFILES_DIRS[0]) / dist_css_base
136133

137134
# Determine the full path to the source css file.
138-
if not (src_css := getattr(settings, "TAILWIND_CLI_SRC_CSS", "css/source.css")):
139-
raise ValueError("TAILWIND_CLI_SRC_CSS must not be None.")
140-
src_css = Path(settings.STATICFILES_DIRS[0]) / src_css
135+
src_css = getattr(settings, "TAILWIND_CLI_SRC_CSS", None)
136+
if not src_css:
137+
user_cache_dir = platformdirs.user_cache_dir("django-tailwind-cli", "django-commons")
138+
src_css = Path(user_cache_dir) / "source.css"
139+
else:
140+
src_css = Path(src_css)
141+
if not src_css.is_absolute():
142+
src_css = Path(settings.BASE_DIR) / src_css
141143

142144
# Determine if the CLI should be downloaded automatically
143145
automatic_download = getattr(settings, "TAILWIND_CLI_AUTOMATIC_DOWNLOAD", True)

src/django_tailwind_cli/management/commands/tailwind.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def build() -> None:
3232
typer.secho(f"Built production stylesheet '{config.dist_css}'.", fg=typer.colors.GREEN)
3333
except KeyboardInterrupt:
3434
typer.secho("Canceled building production stylesheet.", fg=typer.colors.RED)
35-
except subprocess.CalledProcessError as e:
35+
except subprocess.CalledProcessError as e: # pragma: no cover
3636
error_message = e.stderr if e.stderr else "An unknown error occurred."
3737
typer.secho(f"Failed to build production stylesheet: {error_message}", fg=typer.colors.RED)
3838
sys.exit(1)

tests/test_config.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_default_config():
8585
)
8686
assert str(c.dist_css) == "/home/user/project/assets/css/tailwind.css"
8787
assert c.src_css is not None
88-
assert str(c.src_css) == "/home/user/project/assets/css/source.css"
88+
assert str(c.src_css).endswith("django-tailwind-cli/source.css")
8989

9090

9191
def test_invalid_settings_for_staticfiles_dirs(settings: SettingsWrapper):
@@ -116,12 +116,6 @@ def test_invalid_settings_for_tailwind_cli_src_repo(settings: SettingsWrapper):
116116
get_config()
117117

118118

119-
def test_invalid_settings_for_tailwind_cli_src_css(settings: SettingsWrapper):
120-
settings.TAILWIND_CLI_SRC_CSS = None
121-
with pytest.raises(ValueError, match="TAILWIND_CLI_SRC_CSS must not be None."):
122-
get_config()
123-
124-
125119
@pytest.mark.parametrize(
126120
"platform,machine,result",
127121
[
@@ -215,21 +209,21 @@ def test_build_cmd():
215209
c = get_config()
216210
assert c.build_cmd == [
217211
str(c.cli_path),
212+
"--input",
213+
str(c.src_css),
218214
"--output",
219215
str(c.dist_css),
220216
"--minify",
221-
"--input",
222-
str(c.src_css),
223217
]
224218

225219

226220
def test_watch_cmd():
227221
c = get_config()
228222
assert c.watch_cmd == [
229223
str(c.cli_path),
224+
"--input",
225+
str(c.src_css),
230226
"--output",
231227
str(c.dist_css),
232228
"--watch",
233-
"--input",
234-
str(c.src_css),
235229
]

tests/test_management_commands.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def configure_settings(request: pytest.FixtureRequest, mocker: MockerFixture, se
1616
settings.BASE_DIR = tmp_path
1717
settings.TAILWIND_CLI_PATH = tmp_path
1818
settings.TAILWIND_CLI_VERSION = request.param
19+
settings.TAILWIND_CLI_SRC_CSS = tmp_path / "source.css"
1920
settings.STATICFILES_DIRS = (settings.BASE_DIR / "assets",)
2021

2122
mocker.resetall()
@@ -32,8 +33,6 @@ def test_calling_unknown_subcommand():
3233

3334

3435
def test_create_src_css_if_non_exists(settings: LazySettings, tmp_path: Path):
35-
if settings.TAILWIND_CLI_VERSION == "3.4.17":
36-
pytest.skip("With Tailwind CSS CLI 3.4.17 a src css file is optional. But with 4.0.0 it is required.")
3736
c = get_config()
3837
assert c.src_css is not None
3938
assert not c.src_css.exists()
@@ -43,8 +42,6 @@ def test_create_src_css_if_non_exists(settings: LazySettings, tmp_path: Path):
4342

4443

4544
def test_with_existing_src_css(settings: LazySettings, tmp_path: Path):
46-
if settings.TAILWIND_CLI_VERSION == "3.4.17":
47-
pytest.skip("With Tailwind CSS CLI 3.4.17 a src css file is optional. But with 4.0.0 it is required.")
4845
c = get_config()
4946
assert c.src_css is not None
5047
c.src_css.parent.mkdir(parents=True, exist_ok=True)

0 commit comments

Comments
 (0)