#!/bin/bash # Minify HTML files from src/ to production versions # Usage: ./minify.sh # Processes: stratumiops-arch, stratumiops-pipeline, stratumiops-orchestrator set -e SCRIPT_DIR="$(dirname "$0")" FILES=("stratumiops-arch" "stratumiops-pipeline" "stratumiops-orchestrator") minify_file() { local basename=$1 local src_file="${SCRIPT_DIR}/src/${basename}.html" local out_file="${SCRIPT_DIR}/${basename}.html" local temp_file="${out_file}.tmp" if [ ! -f "$src_file" ]; then echo "⚠️ Skipping $basename: source file not found: $src_file" return 0 fi echo "🔨 Minifying $basename.html..." echo " Input: $src_file" echo " Output: $out_file" perl -e " use strict; use warnings; open(my \$fh, '<', '$src_file') or die \$!; my \$content = do { local \$/; <\$fh> }; close(\$fh); # Remove HTML comments \$content =~ s///gs; # Compress CSS (remove spaces and comments) \$content =~ s/(]*>)(.*?)(<\/style>)/ my \$before = \$1; my \$style = \$2; my \$after = \$3; \$style =~ s{\/\*.*?\*\/}{}gs; \$style =~ s{\s+}{ }gs; \$style =~ s{\s*([{}:;,>+~])\s*}{\$1}gs; \$before . \$style . \$after; /gies; # Compress JavaScript (remove comments and extra spaces) \$content =~ s/(]*>)(.*?)(<\/script>)/ my \$before = \$1; my \$script = \$2; my \$after = \$3; \$script =~ s{\/\/.*\$}{}gm; \$script =~ s{\s+}{ }gs; \$script =~ s{\s*([{}();,])\s*}{\$1}gs; \$before . \$script . \$after; /gies; # Remove whitespace between tags \$content =~ s/>\s+', '$temp_file') or die \$!; print \$out \$content; close(\$out); " || { echo "❌ Minification failed for $basename" rm -f "$temp_file" return 1 } mv "$temp_file" "$out_file" # Show statistics original=$(wc -c < "$src_file") minified=$(wc -c < "$out_file") saved=$((original - minified)) percent=$((saved * 100 / original)) echo " ✅ $basename.html minified" printf " Original: %6d bytes | Minified: %6d bytes | Saved: %d%% (%d bytes)\n" "$original" "$minified" "$percent" "$saved" echo "" } echo "🔨 Minifying HTML files..." echo "" for file in "${FILES[@]}"; do minify_file "$file" || exit 1 done echo "✅ All HTML files minified and ready for production"