const fs = require('fs'); const path = require('path'); // dist 目录路径,根据你项目实际情况修改 const DIST_DIR = path.join(__dirname, 'dist'); // 递归统计文件大小 function getFiles(dir) { let results = []; const list = fs.readdirSync(dir); list.forEach(file => { const filePath = path.join(dir, file); const stat = fs.statSync(filePath); if (stat && stat.isDirectory()) { results = results.concat(getFiles(filePath)); } else { results.push({ path: filePath, size: stat.size }); } }); return results; } function formatSize(bytes) { if (bytes < 1024) return bytes + ' B'; if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB'; return (bytes / (1024 * 1024)).toFixed(2) + ' MB'; } function analyze() { if (!fs.existsSync(DIST_DIR)) { console.warn('dist 目录不存在,请先执行 taro build --type weapp'); return; } const files = getFiles(DIST_DIR); const total = files.reduce((sum, f) => sum + f.size, 0); console.log('文件大小分析(按从大到小排序):'); files .sort((a, b) => b.size - a.size) .forEach(f => { console.log( `${formatSize(f.size)} | ${(f.size / total * 100).toFixed(2)}% | ${path.relative(DIST_DIR, f.path)}` ); }); console.log(`\n总大小: ${formatSize(total)}`); } analyze();