Skip to content

Commit 42c18dc

Browse files
committed
feat: minify CSS/HTML/JS files
1 parent 6396d47 commit 42c18dc

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

build.sh

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ build_blog() {
2626
/bin/rm -v "${DST_DIR}/genindex.html"
2727
/bin/rm -v "${DST_DIR}/objects.inv"
2828
/bin/rm -rv "${DST_DIR}/_sources"
29+
30+
# Minify files
31+
python minify.py "${DST_DIR}"
2932
}
3033

3134
[ "${1}" == 'live' ] && dev_live || build_blog

checks.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ set -eu
44
FOLDER='sources'
55

66
check_python_files() {
7-
python -m ruff format "${FOLDER}"
8-
python -m ruff --fix "${FOLDER}"
9-
python -m mypy "${FOLDER}"
7+
python -m ruff format "${FOLDER}" ./*.py
8+
python -m ruff --fix "${FOLDER}" ./*.py
9+
python -m mypy "${FOLDER}" ./*.py
1010
}
1111

1212
check_shell_file() {

minify.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from functools import partial
2+
from pathlib import Path
3+
4+
import minify_html
5+
import rcssmin
6+
import rjsmin
7+
8+
MINIFIER = {
9+
".css": rcssmin.cssmin,
10+
".js": rjsmin.jsmin,
11+
".html": partial(minify_html.minify, minify_css=True, minify_js=True),
12+
}
13+
14+
15+
def main(folder: str) -> None:
16+
total_saved = 0
17+
18+
for file in Path(folder).glob("**/*"):
19+
if not file.is_file() or not (minifier := MINIFIER.get(file.suffix)):
20+
continue
21+
22+
content = file.read_text()
23+
minified = minifier(content)
24+
if content == minified:
25+
continue
26+
27+
file.write_text(minified)
28+
size_old = len(content)
29+
size_new = len(minified)
30+
total_saved += size_old - size_new
31+
diff = 100 - (size_new * 100 / size_old)
32+
print(f"Minified {file.name} (-{diff:.2f}%)", flush=True)
33+
34+
print(f"Saved {total_saved:,} bytes", flush=True)
35+
36+
37+
if __name__ == "__main__":
38+
import sys
39+
40+
main(sys.argv[1])

requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
minify-html==0.15.0
12
myst-parser==2.0.0
3+
rcssmin==1.1.2
4+
rjsmin==1.2.2
25
shibuya==2024.1.17
36
sphinx==7.2.6
47
sphinx-copybutton==0.5.2

0 commit comments

Comments
 (0)