83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Build script for dgray.io
|
|
Minifies JS and CSS files into dist/ directory.
|
|
|
|
Usage: python3 build.py
|
|
Requirements: pip3 install rjsmin rcssmin
|
|
"""
|
|
|
|
import os
|
|
import shutil
|
|
import sys
|
|
|
|
try:
|
|
import rjsmin
|
|
import rcssmin
|
|
except ImportError:
|
|
print("Missing dependencies. Install with:")
|
|
print(" pip3 install rjsmin rcssmin")
|
|
sys.exit(1)
|
|
|
|
SRC_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
DIST_DIR = os.path.join(SRC_DIR, 'dist')
|
|
|
|
EXCLUDE_DIRS = {'.git', 'dist', 'node_modules', 'docs', 'tests', '.vscode', '.idea', '__pycache__'}
|
|
EXCLUDE_FILES = {'AGENTS.md', 'deploy.sh', 'build.py', 'README.md', '.gitignore'}
|
|
|
|
def should_exclude(path):
|
|
parts = os.path.relpath(path, SRC_DIR).split(os.sep)
|
|
return any(p in EXCLUDE_DIRS for p in parts) or os.path.basename(path) in EXCLUDE_FILES
|
|
|
|
def build():
|
|
if os.path.exists(DIST_DIR):
|
|
shutil.rmtree(DIST_DIR)
|
|
|
|
js_saved = 0
|
|
css_saved = 0
|
|
files_copied = 0
|
|
|
|
for root, dirs, files in os.walk(SRC_DIR):
|
|
dirs[:] = [d for d in dirs if d not in EXCLUDE_DIRS]
|
|
|
|
for filename in files:
|
|
src_path = os.path.join(root, filename)
|
|
if should_exclude(src_path):
|
|
continue
|
|
|
|
rel_path = os.path.relpath(src_path, SRC_DIR)
|
|
dest_path = os.path.join(DIST_DIR, rel_path)
|
|
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
|
|
|
if filename.endswith('.js') and not filename.endswith('.min.js'):
|
|
with open(src_path, 'r', encoding='utf-8') as f:
|
|
original = f.read()
|
|
minified = rjsmin.jsmin(original)
|
|
with open(dest_path, 'w', encoding='utf-8') as f:
|
|
f.write(minified)
|
|
saved = len(original.encode('utf-8')) - len(minified.encode('utf-8'))
|
|
js_saved += saved
|
|
files_copied += 1
|
|
|
|
elif filename.endswith('.css') and not filename.endswith('.min.css'):
|
|
with open(src_path, 'r', encoding='utf-8') as f:
|
|
original = f.read()
|
|
minified = rcssmin.cssmin(original)
|
|
with open(dest_path, 'w', encoding='utf-8') as f:
|
|
f.write(minified)
|
|
saved = len(original.encode('utf-8')) - len(minified.encode('utf-8'))
|
|
css_saved += saved
|
|
files_copied += 1
|
|
|
|
else:
|
|
shutil.copy2(src_path, dest_path)
|
|
files_copied += 1
|
|
|
|
print(f"Build complete: {files_copied} files -> dist/")
|
|
print(f" JS savings: {js_saved / 1024:.1f} KiB")
|
|
print(f" CSS savings: {css_saved / 1024:.1f} KiB")
|
|
print(f" Total saved: {(js_saved + css_saved) / 1024:.1f} KiB")
|
|
|
|
if __name__ == '__main__':
|
|
build()
|