()
| 136 | |
| 137 | // 主压缩函数 |
| 138 | async function compressBuild() { |
| 139 | const distDir = path.join(__dirname, '../dist'); |
| 140 | |
| 141 | if (!fs.existsSync(distDir)) { |
| 142 | console.error('dist目录不存在,请先运行构建命令'); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | BuildLogger.log('开始压缩构建文件...'); |
| 147 | |
| 148 | try { |
| 149 | const results = await compressDirectory(distDir); |
| 150 | |
| 151 | if (results.length === 0) { |
| 152 | BuildLogger.log('没有找到需要压缩的文件'); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | // 显示压缩结果 |
| 157 | BuildLogger.log('\n压缩结果:'); |
| 158 | BuildLogger.log('文件名'.padEnd(30) + '原始大小'.padEnd(12) + 'Gzip大小'.padEnd(12) + 'Brotli大小'.padEnd(12) + '压缩率'); |
| 159 | BuildLogger.log('-'.repeat(80)); |
| 160 | |
| 161 | let totalOriginal = 0; |
| 162 | let totalGzip = 0; |
| 163 | let totalBrotli = 0; |
| 164 | |
| 165 | results.forEach(result => { |
| 166 | totalOriginal += result.original; |
| 167 | totalGzip += result.gzip; |
| 168 | if (result.brotli) totalBrotli += result.brotli; |
| 169 | |
| 170 | const originalKB = (result.original / 1024).toFixed(1); |
| 171 | const gzipKB = (result.gzip / 1024).toFixed(1); |
| 172 | const brotliKB = result.brotli ? (result.brotli / 1024).toFixed(1) : '-'; |
| 173 | |
| 174 | BuildLogger.log( |
| 175 | result.file.padEnd(30) + |
| 176 | `${originalKB}KB`.padEnd(12) + |
| 177 | `${gzipKB}KB`.padEnd(12) + |
| 178 | `${brotliKB}KB`.padEnd(12) + |
| 179 | `${result.ratio}%` |
| 180 | ); |
| 181 | }); |
| 182 | |
| 183 | const totalRatio = ((totalOriginal - totalGzip) / totalOriginal * 100).toFixed(1); |
| 184 | BuildLogger.log('-'.repeat(80)); |
| 185 | BuildLogger.log( |
| 186 | '总计'.padEnd(30) + |
| 187 | `${(totalOriginal / 1024).toFixed(1)}KB`.padEnd(12) + |
| 188 | `${(totalGzip / 1024).toFixed(1)}KB`.padEnd(12) + |
| 189 | `${totalBrotli > 0 ? (totalBrotli / 1024).toFixed(1) + 'KB' : '-'}`.padEnd(12) + |
| 190 | `${totalRatio}%` |
| 191 | ); |
| 192 | |
| 193 | BuildLogger.log(`\n压缩完成!共处理 ${results.length} 个文件`); |
| 194 | BuildLogger.log(`节省空间: ${((totalOriginal - totalGzip) / 1024).toFixed(1)}KB`); |
| 195 | } catch (error) { |
no test coverage detected