const fs = require('fs'); const p = require('path'); const root = 'dist/build/mp-weixin'; const appJson = JSON.parse(fs.readFileSync(p.join(root, 'app.json'), 'utf8')); // 收集所有被 app.json 引用/声明的页面路径 const declared = new Set(); (appJson.pages || []).forEach((x) => declared.add(x)); (appJson.subPackages || []).forEach((s) => { const base = s.root.replace(/\/$/, ''); (s.pages || []).forEach((x) => declared.add(`${base}/${x}`)); }); function walk(d, out) { for (const f of fs.readdirSync(d, { withFileTypes: true })) { const fp = p.join(d, f.name); if (f.isDirectory()) walk(fp, out); else out.push({ rel: p.relative(root, fp).replace(/\\/g, '/'), size: fs.statSync(fp).size }); } } const all = []; walk(root, all); // 找出 pages 与 components 下没有被声明的页面文件 const orphans = []; for (const x of all) { const rel = x.rel; if (!rel.startsWith('pages/')) continue; const noExt = rel.replace(/\.(js|json|wxml|wxss)$/, ''); if (!declared.has(noExt)) orphans.push(x); } const orphanTotal = orphans.reduce((s, x) => s + x.size, 0); console.log('孤儿页面文件数:', orphans.length, '总大小:', (orphanTotal / 1024).toFixed(1), 'KB'); console.log('\n孤儿文件 TOP 20:'); orphans.sort((a, b) => b.size - a.size).slice(0, 20).forEach((x) => console.log((x.size / 1024).toFixed(1).padStart(9), 'KB', x.rel)); // 检查 app.json 声明的页面是否都真实存在 const missing = []; for (const d of declared) { const base = p.join(root, d); if (!fs.existsSync(base + '.js') && !fs.existsSync(base + '.wxml')) missing.push(d); } console.log('\n声明但缺失的页面:', missing.length); missing.slice(0, 10).forEach((m) => console.log(' MISSING:', m)); console.log('\n主包 & 分包文件总数:', all.length);