Skip to content

Commit e17a1d4

Browse files
committed
wip: replace npm with native tailwind module
1 parent f07fc9f commit e17a1d4

File tree

11 files changed

+38
-120
lines changed

11 files changed

+38
-120
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ packages = [
2828
python = "^3.8"
2929
django = ">=2.2.28"
3030
django-browser-reload = "^1.6.0"
31+
tailwind = "^3.1.5-beta.0"
3132

3233
[tool.poetry.dev-dependencies]
3334
pytest = "^7.1.2"

src/django_tailwind/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
def get_config(setting_name):
55
return {
6-
"NPM_BIN_PATH": getattr(settings, "NPM_BIN_PATH", "npm"),
76
# 'TAILWIND_DEV_MODE' is deprecated. Leaving it here
87
# to support legacy browser-sync based configs.
98
"TAILWIND_DEV_MODE": getattr(settings, "TAILWIND_DEV_MODE", False),

src/django_tailwind/app_template/{{cookiecutter.app_name}}/static_src/package.json

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = {
22
plugins: {
3-
"postcss-import": {},
4-
"postcss-simple-vars": {},
5-
"postcss-nested": {}
3+
"tailwindcss": {},
4+
"autoprefixer": {}
65
},
76
}

src/django_tailwind/app_template/{{cookiecutter.app_name}}/static_src/tailwind.config.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,5 @@ module.exports = {
4545
extend: {},
4646
},
4747
plugins: [
48-
/**
49-
* '@tailwindcss/forms' is the forms plugin that provides a minimal styling
50-
* for forms. If you don't like it or have own styling for forms,
51-
* comment the line below to disable '@tailwindcss/forms'.
52-
*/
53-
require('@tailwindcss/forms'),
54-
require('@tailwindcss/typography'),
55-
require('@tailwindcss/line-clamp'),
56-
require('@tailwindcss/aspect-ratio'),
5748
],
5849
}

src/django_tailwind/apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.apps import AppConfig
22

33

4-
class TailwindConfig(AppConfig):
4+
class DjangoTailwindConfig(AppConfig):
55
name = "django_tailwind"

src/django_tailwind/management/commands/tailwind.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
import os
2+
from contextlib import contextmanager
23

4+
import tailwind
35
from django.core.management.base import CommandError, LabelCommand
46

57
from django_tailwind import get_config
68

7-
from ...npm import NPM, NPMException
89
from ...utils import get_tailwind_src_path, install_pip_package
910
from ...validate import ValidationError, Validations
1011

1112

13+
@contextmanager
14+
def node_env(env: str):
15+
current = os.environ.get("NODE_ENV", "")
16+
os.environ["NODE_ENV"] = env
17+
yield
18+
os.environ["NODE_ENV"] = current
19+
20+
21+
@contextmanager
22+
def cd(path):
23+
current = os.getcwd()
24+
os.chdir(path)
25+
yield
26+
os.chdir(current)
27+
28+
1229
class Command(LabelCommand):
1330
help = "Runs tailwind commands"
1431
missing_args_message = """
1532
Command argument is missing, please add one of the following:
1633
init - to initialize django-tailwind app
17-
install - to install npm packages necessary to build tailwind css
1834
build - to compile tailwind css into production css
1935
start - to start watching css changes for dev
20-
check-updates - to list possible updates for tailwind css and its dependencies
21-
update - to update tailwind css and its dependencies
2236
Usage example:
2337
python manage.py tailwind start
2438
"""
25-
npm = None
39+
cwd = None
2640
validate = None
2741

2842
def __init__(self, *args, **kwargs):
@@ -57,7 +71,7 @@ def handle_labels(self, *labels, **options):
5771
self.validate.acceptable_label(labels[0])
5872
if labels[0] != "init":
5973
self.validate_app()
60-
self.npm = NPM(cwd=get_tailwind_src_path(get_config("TAILWIND_APP_NAME")))
74+
self.cwd = get_tailwind_src_path(get_config("TAILWIND_APP_NAME"))
6175

6276
getattr(self, "handle_" + labels[0].replace("-", "_") + "_command")(*labels[1:], **options)
6377

@@ -93,25 +107,19 @@ def handle_init_command(self, **options):
93107
except Exception as err:
94108
raise CommandError(err)
95109

96-
def handle_install_command(self, **options):
97-
self.npm_command("install")
98-
99110
def handle_build_command(self, **options):
100-
self.npm_command("run", "build")
111+
with node_env("production"), cd(self.cwd):
112+
tailwind.build(
113+
input="./src/styles.css",
114+
output="../static/css/dist/styles.css",
115+
minify=True,
116+
postcss=True,
117+
)
101118

102119
def handle_start_command(self, **options):
103-
self.npm_command("run", "start")
104-
105-
def handle_check_updates_command(self, **options):
106-
self.npm_command("outdated")
107-
108-
def handle_update_command(self, **options):
109-
self.npm_command("update")
110-
111-
def npm_command(self, *args):
112-
try:
113-
self.npm.command(*args)
114-
except NPMException as err:
115-
raise CommandError(err)
116-
except KeyboardInterrupt:
117-
pass
120+
with node_env("development"), cd(self.cwd):
121+
tailwind.watch(
122+
input="./src/styles.css",
123+
output="../static/css/dist/styles.css",
124+
postcss=True,
125+
)

src/django_tailwind/npm.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/django_tailwind/utils.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ def get_tailwind_src_path(app_name):
1616
return os.path.join(get_app_path(app_name), "static_src")
1717

1818

19-
def get_package_json_path(app_name):
20-
return os.path.join(get_app_path(app_name), "static_src", "package.json")
21-
22-
23-
def get_package_json_contents(app_name):
24-
with open(get_package_json_path(app_name), "r") as f:
25-
return json.load(f)
26-
27-
2819
def is_path_absolute(path):
2920
return path.startswith("/") or path.startswith("http")
3021

tests/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
ALLOWED_HOSTS = ["*"]
1010

11-
INSTALLED_APPS = ["django.contrib.staticfiles", "tailwind"]
11+
INSTALLED_APPS = ["django.contrib.staticfiles", "django_tailwind"]
1212

1313
MIDDLEWARE = [
1414
"django.middleware.security.SecurityMiddleware",

0 commit comments

Comments
 (0)