Skip to content

Commit

Permalink
feat: minify CSS/HTML/JS files
Browse files Browse the repository at this point in the history
  • Loading branch information
BoboTiG committed Feb 4, 2024
1 parent 6396d47 commit 42c18dc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
3 changes: 3 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
40 changes: 40 additions & 0 deletions minify.py
Original file line number Diff line number Diff line change
@@ -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])
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 42c18dc

Please sign in to comment.