Skip to content

Commit

Permalink
feat: minify CSS in lektor server if NODE_ENV=production (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki authored Nov 18, 2023
1 parent 3aa7ede commit a586857
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
24 changes: 13 additions & 11 deletions lektor_tailwind.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ def init_tailwindcss(self):
def _run_watcher(self, output_path: str):
if not self.input_exists():
return
self.tailwind = subprocess.Popen(
[
self.tailwind_bin,
"-i",
self.input_css,
"-o",
os.path.join(output_path, self.css_path),
"-w",
],
cwd=self.env.root_path,
)

cmd = [
self.tailwind_bin,
"--input",
self.input_css,
"--output",
os.path.join(output_path, self.css_path),
"--watch",
]
if os.environ.get("NODE_ENV") == "production":
cmd.append("--minify")

self.tailwind = subprocess.Popen(cmd, cwd=self.env.root_path)

def input_exists(self) -> bool:
return os.path.exists(self.input_css)
Expand Down
16 changes: 15 additions & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ def _communicate1(self, timeout):


@pytest.fixture
def lektor_server(tmp_project_path):
def node_env():
return "development"


@pytest.fixture
def lektor_server(tmp_project_path, node_env, monkeypatch):
monkeypatch.setenv("NODE_ENV", node_env)
with LektorServerFixture() as fixture:
yield fixture

Expand Down Expand Up @@ -202,3 +208,11 @@ def test_server_rebuilds_css_when_input_css_updated(
lektor_server.wait_for_build()
assert ".SENTINEL" in output_css_path.read_text()
assert ".flex" in output_css_path.read_text()


@pytest.mark.parametrize("node_env", ["production"])
def test_server_build_minified(lektor_server, output_css_path, node_env):
lektor_server.wait_for_build()
assert ".flex" in output_css_path.read_text()
lines_of_css = len(output_css_path.read_text().splitlines())
assert lines_of_css == 1

0 comments on commit a586857

Please sign in to comment.