Skip to content

Commit

Permalink
wip: replace npm with native tailwind module
Browse files Browse the repository at this point in the history
  • Loading branch information
seyeong committed Jul 9, 2022
1 parent f07fc9f commit e17a1d4
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 120 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ packages = [
python = "^3.8"
django = ">=2.2.28"
django-browser-reload = "^1.6.0"
tailwind = "^3.1.5-beta.0"

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
Expand Down
1 change: 0 additions & 1 deletion src/django_tailwind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = {
plugins: {
"postcss-import": {},
"postcss-simple-vars": {},
"postcss-nested": {}
"tailwindcss": {},
"autoprefixer": {}
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,5 @@ module.exports = {
extend: {},
},
plugins: [
/**
* '@tailwindcss/forms' is the forms plugin that provides a minimal styling
* for forms. If you don't like it or have own styling for forms,
* comment the line below to disable '@tailwindcss/forms'.
*/
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
require('@tailwindcss/line-clamp'),
require('@tailwindcss/aspect-ratio'),
],
}
2 changes: 1 addition & 1 deletion src/django_tailwind/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.apps import AppConfig


class TailwindConfig(AppConfig):
class DjangoTailwindConfig(AppConfig):
name = "django_tailwind"
58 changes: 33 additions & 25 deletions src/django_tailwind/management/commands/tailwind.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import os
from contextlib import contextmanager

import tailwind
from django.core.management.base import CommandError, LabelCommand

from django_tailwind import get_config

from ...npm import NPM, NPMException
from ...utils import get_tailwind_src_path, install_pip_package
from ...validate import ValidationError, Validations


@contextmanager
def node_env(env: str):
current = os.environ.get("NODE_ENV", "")
os.environ["NODE_ENV"] = env
yield
os.environ["NODE_ENV"] = current


@contextmanager
def cd(path):
current = os.getcwd()
os.chdir(path)
yield
os.chdir(current)


class Command(LabelCommand):
help = "Runs tailwind commands"
missing_args_message = """
Command argument is missing, please add one of the following:
init - to initialize django-tailwind app
install - to install npm packages necessary to build tailwind css
build - to compile tailwind css into production css
start - to start watching css changes for dev
check-updates - to list possible updates for tailwind css and its dependencies
update - to update tailwind css and its dependencies
Usage example:
python manage.py tailwind start
"""
npm = None
cwd = None
validate = None

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -57,7 +71,7 @@ def handle_labels(self, *labels, **options):
self.validate.acceptable_label(labels[0])
if labels[0] != "init":
self.validate_app()
self.npm = NPM(cwd=get_tailwind_src_path(get_config("TAILWIND_APP_NAME")))
self.cwd = get_tailwind_src_path(get_config("TAILWIND_APP_NAME"))

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

Expand Down Expand Up @@ -93,25 +107,19 @@ def handle_init_command(self, **options):
except Exception as err:
raise CommandError(err)

def handle_install_command(self, **options):
self.npm_command("install")

def handle_build_command(self, **options):
self.npm_command("run", "build")
with node_env("production"), cd(self.cwd):
tailwind.build(
input="./src/styles.css",
output="../static/css/dist/styles.css",
minify=True,
postcss=True,
)

def handle_start_command(self, **options):
self.npm_command("run", "start")

def handle_check_updates_command(self, **options):
self.npm_command("outdated")

def handle_update_command(self, **options):
self.npm_command("update")

def npm_command(self, *args):
try:
self.npm.command(*args)
except NPMException as err:
raise CommandError(err)
except KeyboardInterrupt:
pass
with node_env("development"), cd(self.cwd):
tailwind.watch(
input="./src/styles.css",
output="../static/css/dist/styles.css",
postcss=True,
)
37 changes: 0 additions & 37 deletions src/django_tailwind/npm.py

This file was deleted.

9 changes: 0 additions & 9 deletions src/django_tailwind/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ def get_tailwind_src_path(app_name):
return os.path.join(get_app_path(app_name), "static_src")


def get_package_json_path(app_name):
return os.path.join(get_app_path(app_name), "static_src", "package.json")


def get_package_json_contents(app_name):
with open(get_package_json_path(app_name), "r") as f:
return json.load(f)


def is_path_absolute(path):
return path.startswith("/") or path.startswith("http")

Expand Down
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

ALLOWED_HOSTS = ["*"]

INSTALLED_APPS = ["django.contrib.staticfiles", "tailwind"]
INSTALLED_APPS = ["django.contrib.staticfiles", "django_tailwind"]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
Expand Down
5 changes: 0 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ def test_tailwind_install_and_build(settings):
tailwind_config_path = os.path.join(get_app_path(app_name), "static_src", "tailwind.config.js")
assert os.path.isfile(tailwind_config_path), "tailwind.config.js is present"

call_command("tailwind", "install")
assert os.path.isdir(
os.path.join(get_app_path(app_name), "static_src", "node_modules")
), "Tailwind has been installed from npm"

call_command("tailwind", "build")
assert os.path.isfile(
os.path.join(get_app_path(app_name), "static", "css", "dist", "styles.css")
Expand Down

0 comments on commit e17a1d4

Please sign in to comment.