Trying agin without using packages that require Rust.
This commit is contained in:
parent
5f79d32fbb
commit
9695449e79
2 changed files with 20 additions and 9 deletions
26
minify.py
26
minify.py
|
|
@ -1,25 +1,35 @@
|
|||
#!/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 minify_html
|
||||
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("*"):
|
||||
if file.suffix not in {".html", ".css", ".js"}:
|
||||
handler = handlers.get(file.suffix)
|
||||
if not handler:
|
||||
continue
|
||||
content = file.read_text(encoding="utf-8")
|
||||
minified = minify_html.minify(
|
||||
content,
|
||||
minify_js=True,
|
||||
minify_css=True,
|
||||
)
|
||||
file.write_text(minified, encoding="utf-8")
|
||||
file.write_text(handler(content), encoding="utf-8")
|
||||
print(f"{file.suffix.upper().rjust(5)}: {file}")
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue