|
| 1 | +# JavaScript Minifier for Provinent Scripture Study |
| 2 | +# Conservative approach: removes comments only, preserves code structure and UTF-8 encoding |
| 3 | + |
| 4 | +param( |
| 5 | + [switch]$NoMinify = $false # Optional: skip processing |
| 6 | +) |
| 7 | + |
| 8 | +# Define file mappings |
| 9 | +$fileMappings = @( |
| 10 | + @{ Source = "../src/main.js"; Destination = "../public/main.js" }, |
| 11 | + @{ Source = "../src/sw.js"; Destination = "../public/sw.js" }, |
| 12 | + @{ Source = "../src/modules/api.js"; Destination = "../public/modules/api.js" }, |
| 13 | + @{ Source = "../src/modules/navigation.js"; Destination = "../public/modules/navigation.js" }, |
| 14 | + @{ Source = "../src/modules/passage.js"; Destination = "../public/modules/passage.js" }, |
| 15 | + @{ Source = "../src/modules/pdf.js"; Destination = "../public/modules/pdf.js" }, |
| 16 | + @{ Source = "../src/modules/settings.js"; Destination = "../public/modules/settings.js" }, |
| 17 | + @{ Source = "../src/modules/state.js"; Destination = "../public/modules/state.js" }, |
| 18 | + @{ Source = "../src/modules/strongs.js"; Destination = "../public/modules/strongs.js" }, |
| 19 | + @{ Source = "../src/modules/ui.js"; Destination = "../public/modules/ui.js" } |
| 20 | +) |
| 21 | + |
| 22 | +# Check if JS files exist |
| 23 | +Write-Host "Checking file dependencies..." -ForegroundColor Yellow |
| 24 | + |
| 25 | +$missingFiles = @() |
| 26 | +foreach ($mapping in $fileMappings) { |
| 27 | + if (-not (Test-Path $mapping.Source)) { |
| 28 | + $missingFiles += (Split-Path $mapping.Source -Leaf) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +if ($missingFiles.Count -gt 0) { |
| 33 | + Write-Host "Missing files:" -ForegroundColor Red |
| 34 | + $missingFiles | ForEach-Object { Write-Host " - $_" -ForegroundColor Red } |
| 35 | + exit 1 |
| 36 | +} |
| 37 | + |
| 38 | +Write-Host "All files found" -ForegroundColor Green |
| 39 | + |
| 40 | +function Remove-JSComments { |
| 41 | + param([string]$Content) |
| 42 | + |
| 43 | + # Process line by line to avoid encoding issues |
| 44 | + $lines = $Content -split "`r?`n" |
| 45 | + $processedLines = @() |
| 46 | + |
| 47 | + $inBlockComment = $false |
| 48 | + $blockCommentBuffer = @() |
| 49 | + |
| 50 | + foreach ($line in $lines) { |
| 51 | + $currentLine = $line |
| 52 | + |
| 53 | + # Handle block comments spanning multiple lines |
| 54 | + if ($inBlockComment) { |
| 55 | + $endIndex = $currentLine.IndexOf('*/') |
| 56 | + if ($endIndex -ge 0) { |
| 57 | + # End of block comment found |
| 58 | + $currentLine = $currentLine.Substring($endIndex + 2) |
| 59 | + $inBlockComment = $false |
| 60 | + # Don't add the line if it only contained the comment end |
| 61 | + if (-not [string]::IsNullOrWhiteSpace($currentLine)) { |
| 62 | + $processedLines += $currentLine |
| 63 | + } |
| 64 | + continue |
| 65 | + } else { |
| 66 | + # Still inside block comment, skip this line |
| 67 | + continue |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + # Check for block comments |
| 72 | + $blockStartIndex = $currentLine.IndexOf('/*') |
| 73 | + if ($blockStartIndex -ge 0) { |
| 74 | + $blockEndIndex = $currentLine.IndexOf('*/', $blockStartIndex + 2) |
| 75 | + if ($blockEndIndex -ge 0) { |
| 76 | + # Block comment starts and ends on same line |
| 77 | + $beforeComment = $currentLine.Substring(0, $blockStartIndex) |
| 78 | + $afterComment = $currentLine.Substring($blockEndIndex + 2) |
| 79 | + $currentLine = $beforeComment + $afterComment |
| 80 | + # Check if line is now empty |
| 81 | + if ([string]::IsNullOrWhiteSpace($currentLine)) { |
| 82 | + continue |
| 83 | + } |
| 84 | + } else { |
| 85 | + # Block comment starts on this line but continues |
| 86 | + $beforeComment = $currentLine.Substring(0, $blockStartIndex) |
| 87 | + # Only keep content before comment if it's not just whitespace |
| 88 | + if (-not [string]::IsNullOrWhiteSpace($beforeComment)) { |
| 89 | + $currentLine = $beforeComment |
| 90 | + } else { |
| 91 | + $currentLine = '' |
| 92 | + } |
| 93 | + $inBlockComment = $true |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + # Handle single-line comments only if not in block comment |
| 98 | + if (-not $inBlockComment -and -not [string]::IsNullOrEmpty($currentLine)) { |
| 99 | + $commentIndex = $currentLine.IndexOf('//') |
| 100 | + |
| 101 | + if ($commentIndex -ge 0) { |
| 102 | + # Check if it's likely a URL or special pattern that should be preserved |
| 103 | + $beforeComment = $currentLine.Substring(0, $commentIndex) |
| 104 | + |
| 105 | + # Skip if it's a URL, regex, or quoted string containing // |
| 106 | + $isUrl = $beforeComment -match 'https?:$|://$|["'']$' -or |
| 107 | + $currentLine -match '"[^"]*//[^"]*"' -or |
| 108 | + $currentLine -match "'[^']*//[^']*'" -or |
| 109 | + $currentLine -match '\/[^\/]*\/[gmiyus]*\s*//' # regex pattern followed by comment |
| 110 | + |
| 111 | + if (-not $isUrl) { |
| 112 | + # Remove the single-line comment and everything after it |
| 113 | + $currentLine = $beforeComment |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + # Only add non-empty lines (or lines that only had trailing whitespace before comment) |
| 119 | + $trimmedLine = $currentLine.Trim() |
| 120 | + if (-not [string]::IsNullOrEmpty($trimmedLine)) { |
| 121 | + $processedLines += $currentLine |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + # Rejoin with original line endings |
| 126 | + return $processedLines -join "`r`n" |
| 127 | +} |
| 128 | + |
| 129 | +function Get-File-Size-Stats { |
| 130 | + param( |
| 131 | + [string]$originalContent, |
| 132 | + [string]$processedContent, |
| 133 | + [string]$fileName |
| 134 | + ) |
| 135 | + |
| 136 | + $originalSize = [System.Text.Encoding]::UTF8.GetByteCount($originalContent) |
| 137 | + $processedSize = [System.Text.Encoding]::UTF8.GetByteCount($processedContent) |
| 138 | + $savings = if ($originalSize -gt 0) { [math]::Round((1 - $processedSize / $originalSize) * 100, 1) } else { 0 } |
| 139 | + |
| 140 | + # Count lines |
| 141 | + $originalLines = ($originalContent -split "`r`n").Count |
| 142 | + $processedLines = ($processedContent -split "`r`n").Count |
| 143 | + $linesReduction = if ($originalLines -gt 0) { [math]::Round((1 - $processedLines / $originalLines) * 100, 1) } else { 0 } |
| 144 | + |
| 145 | + return @{ |
| 146 | + OriginalSize = $originalSize |
| 147 | + ProcessedSize = $processedSize |
| 148 | + SavingsPercent = $savings |
| 149 | + OriginalLines = $originalLines |
| 150 | + ProcessedLines = $processedLines |
| 151 | + LinesReduction = $linesReduction |
| 152 | + FileName = $fileName |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +# Track statistics |
| 157 | +$totalOriginalSize = 0 |
| 158 | +$totalProcessedSize = 0 |
| 159 | +$totalOriginalLines = 0 |
| 160 | +$totalProcessedLines = 0 |
| 161 | +$fileStats = @() |
| 162 | + |
| 163 | +# Ensure backup directory exists |
| 164 | +$backupDir = "../src/modules" |
| 165 | +if (!(Test-Path $backupDir)) { |
| 166 | + New-Item -ItemType Directory -Path $backupDir -Force |
| 167 | +} |
| 168 | + |
| 169 | +Write-Host "Processing JavaScript files..." -ForegroundColor Yellow |
| 170 | + |
| 171 | +# Process each file |
| 172 | +foreach ($mapping in $fileMappings) { |
| 173 | + $sourceFile = $mapping.Source |
| 174 | + $destFile = $mapping.Destination |
| 175 | + $fileName = Split-Path $sourceFile -Leaf |
| 176 | + |
| 177 | + Write-Host " Processing: $fileName" -ForegroundColor Cyan |
| 178 | + |
| 179 | + if (Test-Path $sourceFile) { |
| 180 | + # Backup destination file if it exists |
| 181 | + if (Test-Path $destFile) { |
| 182 | + $backupPath = Join-Path $backupDir "$fileName.backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')" |
| 183 | + Write-Host " Backing up existing file to: $backupPath" -ForegroundColor Yellow |
| 184 | + Copy-Item $destFile $backupPath -Force |
| 185 | + } |
| 186 | + |
| 187 | + # Read source file with proper UTF-8 encoding using .NET methods |
| 188 | + $originalContent = [System.IO.File]::ReadAllText($sourceFile, [System.Text.Encoding]::UTF8) |
| 189 | + $processedContent = $originalContent |
| 190 | + |
| 191 | + if (-not $NoMinify) { |
| 192 | + # Remove comments only (conservative approach) |
| 193 | + $processedContent = Remove-JSComments -Content $processedContent |
| 194 | + } |
| 195 | + |
| 196 | + # Get file statistics |
| 197 | + $stats = Get-File-Size-Stats -originalContent $originalContent -processedContent $processedContent -fileName $fileName |
| 198 | + $totalOriginalSize += $stats.OriginalSize |
| 199 | + $totalProcessedSize += $stats.ProcessedSize |
| 200 | + $totalOriginalLines += $stats.OriginalLines |
| 201 | + $totalProcessedLines += $stats.ProcessedLines |
| 202 | + $fileStats += $stats |
| 203 | + |
| 204 | + if (-not $NoMinify) { |
| 205 | + Write-Host " Processed: $([math]::Round($stats.OriginalSize/1KB,1))KB -> $([math]::Round($stats.ProcessedSize/1KB,1))KB ($($stats.SavingsPercent)%)" -ForegroundColor White |
| 206 | + Write-Host " Lines: $($stats.OriginalLines) -> $($stats.ProcessedLines) ($($stats.LinesReduction)%)" -ForegroundColor Gray |
| 207 | + } else { |
| 208 | + Write-Host " Copied: $([math]::Round($stats.OriginalSize/1KB,1))KB, $($stats.OriginalLines) lines" -ForegroundColor White |
| 209 | + } |
| 210 | + |
| 211 | + # Ensure destination directory exists |
| 212 | + $destDir = Split-Path $destFile -Parent |
| 213 | + if (!(Test-Path $destDir)) { |
| 214 | + New-Item -ItemType Directory -Path $destDir -Force |
| 215 | + } |
| 216 | + |
| 217 | + # Write processed content with proper UTF-8 encoding using .NET methods |
| 218 | + [System.IO.File]::WriteAllText($destFile, $processedContent, [System.Text.Encoding]::UTF8) |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +# Calculate total savings |
| 223 | +$totalSavings = if ($totalOriginalSize -gt 0) { [math]::Round((1 - $totalProcessedSize / $totalOriginalSize) * 100, 1) } else { 0 } |
| 224 | +$totalLinesReduction = if ($totalOriginalLines -gt 0) { [math]::Round((1 - $totalProcessedLines / $totalOriginalLines) * 100, 1) } else { 0 } |
| 225 | + |
| 226 | +Write-Host "`nProcessing complete!" -ForegroundColor Green |
| 227 | + |
| 228 | +# Display statistics |
| 229 | +$finalSizeKB = [math]::Round($totalProcessedSize / 1KB, 1) |
| 230 | +$originalSizeKB = [math]::Round($totalOriginalSize / 1KB, 1) |
| 231 | + |
| 232 | +Write-Host "File size statistics:" -ForegroundColor Yellow |
| 233 | +Write-Host " Original total: $originalSizeKB KB" -ForegroundColor Gray |
| 234 | +Write-Host " Final size: $finalSizeKB KB" -ForegroundColor Green |
| 235 | + |
| 236 | +if (-not $NoMinify) { |
| 237 | + $savedKB = [math]::Round(($totalOriginalSize - $totalProcessedSize) / 1KB, 1) |
| 238 | + Write-Host " Space saved: $totalSavings% ($savedKB KB)" -ForegroundColor Green |
| 239 | + Write-Host " Lines reduced: $totalOriginalLines -> $totalProcessedLines ($totalLinesReduction%)" -ForegroundColor Cyan |
| 240 | +} |
| 241 | + |
| 242 | +Write-Host "`nBackups stored in: $backupDir" -ForegroundColor Gray |
| 243 | +Write-Host "UTF-8 encoding preserved for all special characters" -ForegroundColor Green |
| 244 | + |
| 245 | +# Usage examples |
| 246 | +Write-Host "`nUsage examples:" -ForegroundColor Gray |
| 247 | +Write-Host " .\Build-JS.ps1 # Remove comments only (conservative)" |
| 248 | +Write-Host " .\Build-JS.ps1 -NoMinify # Copy files only (no modification)" |
| 249 | + |
| 250 | +if (-not $NoMinify) { |
| 251 | + Write-Host "`nNote: Conservative minification - only comments removed, UTF-8 and code structure preserved" -ForegroundColor Yellow |
| 252 | + Write-Host "Removes: // single-line comments, /* */ block comments, preserves URLs and special patterns" -ForegroundColor Gray |
| 253 | +} |
0 commit comments