diff --git a/build.sh b/build.sh index 8151112..7190366 100755 --- a/build.sh +++ b/build.sh @@ -26,6 +26,9 @@ build_blog() { /bin/rm -v "${DST_DIR}/genindex.html" /bin/rm -v "${DST_DIR}/objects.inv" /bin/rm -rv "${DST_DIR}/_sources" + + # Minify files + python minify.py "${DST_DIR}" } [ "${1}" == 'live' ] && dev_live || build_blog diff --git a/checks.sh b/checks.sh index 7ab054b..d713a55 100755 --- a/checks.sh +++ b/checks.sh @@ -4,9 +4,9 @@ set -eu FOLDER='sources' check_python_files() { - python -m ruff format "${FOLDER}" - python -m ruff --fix "${FOLDER}" - python -m mypy "${FOLDER}" + python -m ruff format "${FOLDER}" ./*.py + python -m ruff --fix "${FOLDER}" ./*.py + python -m mypy "${FOLDER}" ./*.py } check_shell_file() { diff --git a/minify.py b/minify.py new file mode 100644 index 0000000..9de635c --- /dev/null +++ b/minify.py @@ -0,0 +1,40 @@ +from functools import partial +from pathlib import Path + +import minify_html +import rcssmin +import rjsmin + +MINIFIER = { + ".css": rcssmin.cssmin, + ".js": rjsmin.jsmin, + ".html": partial(minify_html.minify, minify_css=True, minify_js=True), +} + + +def main(folder: str) -> None: + total_saved = 0 + + for file in Path(folder).glob("**/*"): + if not file.is_file() or not (minifier := MINIFIER.get(file.suffix)): + continue + + content = file.read_text() + minified = minifier(content) + if content == minified: + continue + + file.write_text(minified) + size_old = len(content) + size_new = len(minified) + total_saved += size_old - size_new + diff = 100 - (size_new * 100 / size_old) + print(f"Minified {file.name} (-{diff:.2f}%)", flush=True) + + print(f"Saved {total_saved:,} bytes", flush=True) + + +if __name__ == "__main__": + import sys + + main(sys.argv[1]) diff --git a/requirements.txt b/requirements.txt index 6e61f3f..3dc4c77 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,7 @@ +minify-html==0.15.0 myst-parser==2.0.0 +rcssmin==1.1.2 +rjsmin==1.2.2 shibuya==2024.1.17 sphinx==7.2.6 sphinx-copybutton==0.5.2