Skip to content

Commit

Permalink
Don't use Tailwind watcher
Browse files Browse the repository at this point in the history
Running Tailwind watcher from within Lektor watcher is unnecessary,
since we already have a watcher and know when to rebuild things, and
it leads to bugs, such as the Tailwind watcher leaking memory or
kicking in before the build is fully done.
  • Loading branch information
mathrick committed Mar 1, 2024
1 parent 3bc397b commit df89f8a
Showing 1 changed file with 5 additions and 28 deletions.
33 changes: 5 additions & 28 deletions lektor_tailwind.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
from lektor.pluginsystem import Plugin
from pytailwindcss import get_bin_path, install

__version__ = "0.1.2"
GRACEFUL_TIMEOUT = 5

__version__ = "0.1.3"

class TailwindPlugin(Plugin):
name = "lektor-tailwind"
Expand All @@ -35,15 +33,6 @@ def init_tailwindcss(self):
[self.tailwind_bin, "init"], check=True, cwd=self.env.root_path
)

def _run_watcher(self, output_path: str):
if not self.input_exists():
return
self.tailwind = subprocess.Popen(
self._get_tailwind_args(output_path, "-w",
*(["--minify"] if os.environ.get("NODE_ENV") == "production" else [])),
cwd=output_path,
)

def _get_tailwind_args(self, output_path, *extra_args):
return [self.tailwind_bin,
"-c",
Expand All @@ -58,29 +47,17 @@ def input_exists(self) -> bool:
return os.path.exists(self.input_css)

def compile_css(self, output_path: str):
minify = [] if self.watch else ["--minify"]
subprocess.run(
self._get_tailwind_args(output_path, "--minify"),
self._get_tailwind_args(output_path, *minify),
check=True,
cwd=output_path,
)

def on_server_spawn(self, **extra):
self.watch = True

def on_server_stop(self, **extra):
self.watch = False
if self.tailwind is not None:
self.tailwind.terminate()
try:
self.tailwind.communicate(GRACEFUL_TIMEOUT)
except subprocess.TimeoutExpired:
self.tailwind.kill()
self.tailwind = None

def on_after_build_all(self, builder, **extra):
if not self.input_exists() or self.tailwind is not None:
if not self.input_exists():
return
if self.watch:
self._run_watcher(builder.destination_path)
else:
self.compile_css(builder.destination_path)
self.compile_css(builder.destination_path)

0 comments on commit df89f8a

Please sign in to comment.