43 lines
1.2 KiB
Python
Executable file
43 lines
1.2 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Minify CSS, JS, and HTML files in the deploy directory in-place."""
|
|
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import rcssmin
|
|
import rjsmin
|
|
|
|
DEPLOY_DIR = Path("./deploy")
|
|
|
|
|
|
def minify_html_content(content):
|
|
content = re.sub(r"<!--.*?-->", "", content, flags=re.DOTALL) # remove comments
|
|
content = re.sub(r">\s+<", "><", content) # collapse whitespace between tags
|
|
content = re.sub(r"\s{2,}", " ", content) # collapse remaining whitespace
|
|
return content.strip()
|
|
|
|
|
|
def minify_files():
|
|
handlers = {
|
|
".css": lambda c: rcssmin.cssmin(c),
|
|
".js": lambda c: rjsmin.jsmin(c, keep_bang_comments=False),
|
|
".html": minify_html_content,
|
|
}
|
|
for file in DEPLOY_DIR.rglob("*"):
|
|
handler = handlers.get(file.suffix)
|
|
if not handler:
|
|
continue
|
|
content = file.read_text(encoding="utf-8")
|
|
file.write_text(handler(content), encoding="utf-8")
|
|
print(f"{file.suffix.upper().rjust(5)}: {file}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if not DEPLOY_DIR.exists():
|
|
print(f"Error: deploy directory not found at {DEPLOY_DIR}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
print("Minifying assets...")
|
|
minify_files()
|
|
print("Done.")
|