[ai] memory optimized web serving

This commit is contained in:
2025-11-02 00:04:31 +01:00
parent e10ffcfd65
commit c32eabf464
2 changed files with 149 additions and 49 deletions

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env python3
"""
Simple build tool to minify web assets in the `web/` folder.
Simple build tool to minify web assets in the `web/` folder and generate gzipped versions.
Usage:
python3 scripts/minify_web.py
@@ -15,6 +15,11 @@ and write minified outputs to:
- web/cleaned/style.css
- web/cleaned/script.js
Additionally, gzipped variants are produced to enable efficient on-device serving:
- web/cleaned/index.html.gz
- web/cleaned/style.css.gz
- web/cleaned/script.js.gz
The minifiers are intentionally conservative (no external deps) and aim to be
safe for typical static files used in this project. They remove comments,
collapse unnecessary whitespace and do small syntax-preserving transformations.
@@ -29,6 +34,7 @@ from pathlib import Path
import re
import sys
import os
import gzip
BASE = Path(__file__).resolve().parent.parent
WEB = BASE / "web"
@@ -218,6 +224,15 @@ def write_file(path: Path, data: str):
except Exception as e:
print(f"ERROR writing {path}: {e}", file=sys.stderr)
def write_gzip(path: Path, data: str):
"""Write UTF-8 text as gzip with deterministic mtime for reproducible builds."""
try:
gz_bytes = gzip.compress(data.encode('utf-8'), mtime=0)
path.write_bytes(gz_bytes)
print(f"Wrote {path} ({len(gz_bytes)} bytes, gzip)")
except Exception as e:
print(f"ERROR writing {path}: {e}", file=sys.stderr)
def minify_all():
ensure_clean_dir()
@@ -228,6 +243,7 @@ def minify_all():
s = read_file(index)
out = minify_html(s)
write_file(CLEAN / "index.html", out)
write_gzip(CLEAN / "index.html.gz", out)
else:
print("No index.html found in web/")
@@ -238,6 +254,7 @@ def minify_all():
s = read_file(css)
out = minify_css(s)
write_file(CLEAN / "style.css", out)
write_gzip(CLEAN / "style.css.gz", out)
else:
print("No style.css found in web/")
@@ -248,6 +265,7 @@ def minify_all():
s = read_file(js)
out = minify_js(s)
write_file(CLEAN / "script.js", out)
write_gzip(CLEAN / "script.js.gz", out)
else:
print("No script.js found in web/")