-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathminify.py
More file actions
266 lines (217 loc) · 8.14 KB
/
minify.py
File metadata and controls
266 lines (217 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
"""BTDT CSS/JS minification and preset bundling utility."""
import argparse
import re
import sys
from pathlib import Path
EXCLUDE_DIRS = {".git", "node_modules", "__pycache__", ".venv"}
IMPORT_RE = re.compile(
r"""
@import
\s+
(?:
url\(
\s*
(?:
(?P<url_double>"[^"]+")
|
(?P<url_single>'[^']+')
|
(?P<url_bare>[^)\s]+)
)
\s*
\)
|
(?P<plain_double>"[^"]+")
|
(?P<plain_single>'[^']+')
)
(?P<tail>\s+[^;]+)?
\s*;
""",
re.VERBOSE,
)
def should_skip_dir(path: Path) -> bool:
"""Return True when the path belongs to an excluded build or cache directory."""
return any(part in EXCLUDE_DIRS for part in path.parts)
def is_minified(path: Path) -> bool:
"""Return True for already-minified CSS or JS files."""
return path.name.endswith(".min.js") or path.name.endswith(".min.css")
def iter_targets(target: Path, mode: str):
"""Yield source files to process from a single file or a directory tree."""
if target.is_file():
if target.name.startswith("_") or is_minified(target):
return
yield target
return
if not target.is_dir():
raise FileNotFoundError(f"'{target}' is not a valid file or directory")
suffixes = {".css", ".js"} if mode == "normal" else {".css"}
for path in sorted(target.rglob("*")):
if not path.is_file():
continue
if should_skip_dir(path.parent):
continue
if path.name.startswith("_"):
continue
if path.suffix not in suffixes or is_minified(path):
continue
yield path
def write_minified(output_path: Path, content: str) -> None:
"""Write minified content next to its source file using UTF-8."""
if output_path.suffix == ".css" and content == "":
content = "/* empty */\n"
output_path.write_text(content, encoding="utf-8")
def get_cssmin():
"""Import and return the CSS minifier on demand."""
try:
from rcssmin import cssmin # pylint: disable=import-outside-toplevel
except ModuleNotFoundError as exc:
raise ModuleNotFoundError(
"Missing 'rcssmin'. Install script dependencies with: "
"pip install -r btdt/scripts/requirements.txt"
) from exc
return cssmin
def get_jsmin():
"""Import and return the JS minifier on demand."""
try:
from rjsmin import jsmin # pylint: disable=import-outside-toplevel
except ModuleNotFoundError as exc:
raise ModuleNotFoundError(
"Missing 'rjsmin'. Install script dependencies with: "
"pip install -r btdt/scripts/requirements.txt"
) from exc
return jsmin
def minify_standard_file(path: Path) -> None:
"""Minify a regular source CSS or JS file into its .min counterpart."""
try:
source = path.read_text(encoding="utf-8")
if path.suffix == ".js":
jsmin = get_jsmin()
output = path.with_suffix(".min.js")
write_minified(output, jsmin(source))
print(f"[JS] {path}")
elif path.suffix == ".css":
cssmin = get_cssmin()
output = path.with_suffix(".min.css")
write_minified(output, cssmin(source))
print(f"[CSS] {path}")
except Exception as exc: # pylint: disable=broad-except
print(f"[ERROR] {path}: {exc}")
def resolve_css_imports(path: Path, stack=None, root_path: Path | None = None) -> str:
"""Resolve local preset @import statements recursively into a single CSS string."""
stack = stack or []
resolved_path = path.resolve()
root_path = root_path or resolved_path
if resolved_path in stack:
chain = " -> ".join(str(item) for item in [*stack, resolved_path])
raise ValueError(f"Circular @import detected: {chain}")
source = path.read_text(encoding="utf-8")
current_stack = [*stack, resolved_path]
def replace_import(match):
"""Inline a supported local import, or leave unsupported imports untouched."""
relative = next(
(
value
for value in (
match.group("url_double"),
match.group("url_single"),
match.group("url_bare"),
match.group("plain_double"),
match.group("plain_single"),
)
if value is not None
),
None,
)
if relative is None:
raise ValueError(f"Could not parse @import in {path}")
relative = relative.strip().strip('"').strip("'")
tail = (match.group("tail") or "").strip()
if re.match(r"^(url\()?https?://", relative, re.IGNORECASE):
return match.group(0)
if tail:
return match.group(0)
imported_path = (path.parent / relative).resolve()
if not imported_path.exists():
raise FileNotFoundError(f"Import '{relative}' does not exist in {path}")
inlined = resolve_css_imports(imported_path, current_stack, root_path)
if "@font-face" in inlined and "src:" in inlined:
inlined = rewrite_font_urls(inlined, imported_path, root_path)
return inlined
return IMPORT_RE.sub(replace_import, source)
def rewrite_font_urls(css: str, css_path: Path, root_path: Path) -> str:
"""Rewrite relative font URLs in @font-face rules to be relative to root_path."""
try:
rel_path = css_path.parent.relative_to(root_path.parent)
prefix = str(rel_path).replace("\\", "/") + "/" if rel_path.parts else ""
except ValueError:
import os
prefix = os.path.relpath(css_path.parent, root_path.parent).replace("\\", "/") + "/"
if prefix and prefix != "./":
css = re.sub(r"url\((['\"]?)([^)'\"]+)\.ttf\1\)", rf"url(\1{prefix}\2.ttf\1)", css)
css = re.sub(r"url\((['\"]?)([^)'\"]+)\.woff2\1\)", rf"url(\1{prefix}\2.woff2\1)", css)
return css
def hoist_imports_to_top(css: str) -> str:
"""Move any remaining @import rules to the beginning of the stylesheet."""
imports: list[str] = []
def collect_import(match):
imports.append(match.group(0).strip())
return ""
body = IMPORT_RE.sub(collect_import, css).strip()
if not imports:
return body
if not body:
return "\n".join(imports) + "\n"
return "\n".join(imports) + "\n\n" + body
def minify_preset_file(path: Path) -> None:
"""Bundle a BTDT preset CSS file by inlining imports, then write its .min.css output."""
if path.suffix != ".css":
print(f"[SKIP] {path}: preset mode only supports CSS files")
return
try:
cssmin = get_cssmin()
bundled = resolve_css_imports(path)
bundled = hoist_imports_to_top(bundled)
output = path.with_suffix(".min.css")
write_minified(output, cssmin(bundled))
print(f"[PRESET] {path}")
except Exception as exc: # pylint: disable=broad-except
print(f"[ERROR] {path}: {exc}")
def parse_args():
"""Parse CLI arguments for normal or preset minification."""
parser = argparse.ArgumentParser(
description=(
"Minify CSS/JS files or bundle preset CSS files "
"by inlining their @import dependencies."
)
)
parser.add_argument(
"mode",
choices=("normal", "preset"),
help="Processing mode: standard minification or preset bundling/minification",
)
parser.add_argument(
"target",
help="File or directory to process",
)
return parser.parse_args()
def main():
"""CLI entry point for BTDT asset minification."""
args = parse_args()
target = Path(args.target)
try:
paths = list(iter_targets(target, args.mode))
except FileNotFoundError as exc:
print(f"Error: {exc}")
sys.exit(1)
if not paths:
print("No files to process")
sys.exit(0)
for path in paths:
if args.mode == "normal":
minify_standard_file(path)
else:
minify_preset_file(path)
if __name__ == "__main__":
main()