Skip to content

Commit dbcaf5e

Browse files
committed
Minify HTML, CSS, and JS for deploy
1 parent 9ccdf89 commit dbcaf5e

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ endif
1313

1414
OUT = out
1515

16-
.PHONY: clean build copy_assets sitemap
16+
.PHONY: clean build copy_assets minify_static sitemap
1717

18-
all: build copy_assets sitemap
18+
all: build copy_assets minify_static sitemap
1919

2020
build:
2121
@$(RM) -rf $(OUT)
2222
@$(MKDIR) $(OUT)
2323
@$(PYTHON) -m stigaview_static -o $(OUT)/ products
2424

25-
2625
copy_assets:
2726
@$(CP) -r public_html/* $(OUT)/
2827

28+
minify_static:
29+
@$(PYTHON) utils/minify.py --output_path $(OUT)/
30+
2931
sitemap:
3032
@$(FIND) "$(OUT)/" -name "*.html" > "$(OUT)/sitemap.txt"
3133
@$(SED) -i "s#out#https://stigaview.com#" "$(OUT)/sitemap.txt"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ black==25.1.0
44
pre-commit==4.2.0
55
isort==6.0.1
66
mypy==1.15.0
7+
minify-html==0.16.4

stigaview_static/html_output.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os.path
33
import shutil
44

5+
import minify_html
56
from jinja2 import Environment, FileSystemLoader
67

78
from stigaview_static import models
@@ -13,8 +14,9 @@ def render_template(template: str, out_path: str, **kwargs):
1314
env = Environment(loader=file_loader)
1415
template = env.get_template(template)
1516
output = template.render(git_sha=get_git_revision_short_hash(), **kwargs)
17+
minified = minify_html.minify(output)
1618
with open(out_path, "w") as fp:
17-
fp.write(output)
19+
fp.write(minified)
1820

1921

2022
def render_product(product: models.Product, out_path: str):

utils/minify.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import glob
4+
import pathlib
5+
6+
import minify_html.minify_html
7+
8+
9+
def _parse_args() -> argparse.Namespace:
10+
parser = argparse.ArgumentParser()
11+
parser.add_argument(
12+
"--output_path",
13+
help="Root of the output path",
14+
default="/Users/mburket/Developer/stigaview-static/out",
15+
)
16+
return parser.parse_args()
17+
18+
19+
def main() -> int:
20+
args = _parse_args()
21+
output_path = pathlib.Path(args.output_path)
22+
css_path = output_path.joinpath("static", "css")
23+
css_files = glob.glob(root_dir=css_path, pathname="*.css")
24+
for file in css_files:
25+
css_file = css_path.joinpath(file)
26+
minifed = minify_html.minify(css_file.read_text(), minify_css=True)
27+
css_file.write_text(minifed)
28+
js_path = output_path.joinpath("static", "js")
29+
js_files = glob.glob(root_dir=js_path, pathname="*.js")
30+
for file in js_files:
31+
js_file = js_path.joinpath(file)
32+
minifed = minify_html.minify(
33+
js_file.read_text(), minify_js=True, keep_comments=False
34+
)
35+
js_file.write_text(minifed)
36+
return 0
37+
38+
39+
if __name__ == "__main__":
40+
raise SystemExit(main())

0 commit comments

Comments
 (0)