'use strict'; /** * 前端元素引用自检:核对 app.js 里 `$('id')` 引用的元素是否都存在于 index.html, * 以及 index.html 里是否还有未被引用的 id(可能是改版后遗留的死元素)。 * * 之所以放进 scripts/ 而不是测试里,是因为它检查的是静态资源一致性, * 与运行时行为无关;出问题时直接跑 `node scripts/check-web-ids.js` 即可。 */ const fs = require('fs'); const path = require('path'); const publicDir = path.join(__dirname, '..', 'src', 'web', 'public'); const app = fs.readFileSync(path.join(publicDir, 'app.js'), 'utf8'); const html = fs.readFileSync(path.join(publicDir, 'index.html'), 'utf8'); const referenced = new Set(); for (const match of app.matchAll(/\$\('([^']+)'\)/g)) referenced.add(match[1]); const declared = new Set(); for (const match of html.matchAll(/id="([^"]+)"/g)) declared.add(match[1]); const missing = [...referenced].filter((id) => !declared.has(id)); const unused = [...declared].filter((id) => !referenced.has(id)); console.log(`app.js 引用 ${referenced.size} 个元素 id,index.html 声明 ${declared.size} 个`); if (missing.length > 0) { console.error('引用了不存在的元素 id:' + missing.join(', ')); process.exit(1); } if (unused.length > 0) { console.log('提示:index.html 中未被 app.js 直接引用的 id(可能由 CSS / 标签页逻辑使用):' + unused.join(', ')); } console.log('前端元素引用自检通过');