/** * 检查微信小程序生成的 app.wxss 中是否包含 WXSS 不兼容的 CSS 选择器转义。 * Tailwind 的小数点类(h-1.5)、分数类(w-1/2)、伪类变体(last:)等 * 会生成带反斜杠转义的选择器,微信 WXSS 解析会报错。 */ const fs = require('fs') const path = require('path') const wxssPath = path.resolve(__dirname, '../dist/build/mp-weixin/app.wxss') if (!fs.existsSync(wxssPath)) { console.error(`[check-wxss] 未找到 ${wxssPath},请先执行构建`) process.exit(1) } const content = fs.readFileSync(wxssPath, 'utf-8') // 匹配以 . 开头、类名中包含反斜杠转义的选择器 // 例如 .h-1\.5、.w-1\/2、.last\:border-b-0、.bg-white\/20 const escapedClassSelector = /\.[a-zA-Z0-9_-]*\\[.:/]/g const matches = content.match(escapedClassSelector) || [] // 排除常见的 base64/inline 数据中的反斜杠(它们通常不在选择器位置) const suspicious = matches.filter((m) => !m.startsWith('.data:') && !m.includes('\\/\\/')) if (suspicious.length > 0) { console.error('[check-wxss] 发现 WXSS 不兼容的转义选择器:') suspicious.slice(0, 20).forEach((m) => console.error(' -', m)) if (suspicious.length > 20) { console.error(` ... 还有 ${suspicious.length - 20} 处`) } console.error('[check-wxss] 请检查源码中是否使用了小数点类、分数类或 last:/hover: 等变体。') process.exit(1) } console.log('[check-wxss] app.wxss 未发现 WXSS 不兼容的转义选择器')