54 lines
1.4 KiB
Python
Executable file
54 lines
1.4 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Minify CSS, JS, and HTML files in the deploy directory in-place."""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import htmlmin
|
|
import rcssmin
|
|
import rjsmin
|
|
|
|
DEPLOY_DIR = Path("./deploy")
|
|
|
|
|
|
def minify_css():
|
|
for css_file in DEPLOY_DIR.rglob("*.css"):
|
|
content = css_file.read_text(encoding="utf-8")
|
|
minified = rcssmin.cssmin(content)
|
|
css_file.write_text(minified, encoding="utf-8")
|
|
print(f" CSS: {css_file}")
|
|
|
|
|
|
def minify_js():
|
|
for js_file in DEPLOY_DIR.rglob("*.js"):
|
|
if js_file.name.endswith(".min.js"):
|
|
continue
|
|
content = js_file.read_text(encoding="utf-8")
|
|
minified = rjsmin.jsmin(content)
|
|
js_file.write_text(minified, encoding="utf-8")
|
|
print(f" JS: {js_file}")
|
|
|
|
|
|
def minify_html():
|
|
for html_file in DEPLOY_DIR.rglob("*.html"):
|
|
content = html_file.read_text(encoding="utf-8")
|
|
minified = htmlmin.minify(
|
|
content,
|
|
remove_comments=True,
|
|
remove_empty_space=True,
|
|
reduce_boolean_attributes=True,
|
|
)
|
|
html_file.write_text(minified, encoding="utf-8")
|
|
print(f" HTML: {html_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_css()
|
|
minify_js()
|
|
minify_html()
|
|
print("Done.")
|