Files

130 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

#!/usr/bin/env node
/**
* 小程序「真编译」预检 —— 直接调用微信开发者工具内置的 wcc / wcsc 编译器。
*
* 为什么需要它(2026-09-23 教训):
* v2 设计收敛在 app.wxss 写了 `.tap-gap > * + *`(通配符 + 兄弟选择器),
* WXSS 不支持通配符 → wcc 编译期直接 `error at token '*'`,整个小程序编译不过。
* 而当时 19 个 UI 契约守卫全绿 —— 因为静态守卫只扫 `miniapp/components/`,
* 且判据里没有通配符这一项。**静态分析全绿 ≠ 能编译**。
* 本脚本用真编译器兜住这一类,属静态守卫的补充而非替代。
*
* 用法:
* node scripts/miniapp-compile-check.mjs
* 环境变量 WX_DEVTOOLS 可指定微信开发者工具安装目录。
*
* CI 说明:GitHub Actions 的 runner 上没有微信开发者工具,本脚本会输出 SKIPPED
* 并以 0 退出。**它刻意不挂进 scripts/run-ui-contracts.mjs** —— 一个在 CI 里
* 什么都不做的守卫被计入"全绿",正是本项目反复强调的"假绿"。
* CI 的拦截依赖静态守卫 ui-tests/wxss-component-selector-contracts.mjs。
*/
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { execFileSync } from 'node:child_process'
const ROOT = path.resolve(import.meta.dirname, '..', 'miniapp')
/** wcc/wcsc 相对开发者工具安装目录的位置 */
const REL = 'resources/app.asar.unpacked/node_modules/wcc-exec'
const CANDIDATES = [
process.env.WX_DEVTOOLS,
'E:/Program Files/微信web开发者工具',
'C:/Program Files (x86)/Tencent/微信web开发者工具',
'C:/Program Files/Tencent/微信web开发者工具',
'D:/Program Files (x86)/Tencent/微信web开发者工具',
].filter(Boolean)
function locateCompiler() {
for (const base of CANDIDATES) {
const dir = path.join(base, REL)
const wcc = path.join(dir, 'wcc.exe')
const wcsc = path.join(dir, 'wcsc.exe')
if (fs.existsSync(wcc) && fs.existsSync(wcsc)) return { wcc, wcsc }
}
return null
}
function walk(dir, ext, out = []) {
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, e.name)
if (e.isDirectory()) {
if (!['node_modules', 'miniprogram_npm'].includes(e.name)) walk(p, ext, out)
} else if (e.name.endsWith(ext)) out.push(p)
}
return out
}
/** 把 @import "x"; 内联为被导入文件的原文(wcsc 单独调用时不做相对路径解析) */
function inlineImports(file, depth = 0) {
let src = fs.readFileSync(file, 'utf8')
if (depth > 3) return src
return src.replace(/@import\s+["']([^"']+)["']\s*;/g, (m, rel) => {
const target = path.resolve(path.dirname(file), rel)
if (!fs.existsSync(target)) return `/* MISSING IMPORT: ${rel} */`
return `\n/* ==== inlined: ${rel} ==== */\n${inlineImports(target, depth + 1)}\n`
})
}
const compileOne = (exe, file, cwd) => {
try {
execFileSync(exe, [file], { stdio: ['ignore', 'ignore', 'pipe'], cwd })
return null
} catch (e) {
const msg = (e.stderr || '').toString().trim() || `exit ${e.status}`
return msg.replace(/\r?\n/g, ' | ').slice(0, 300)
}
}
const compiler = locateCompiler()
if (!compiler) {
console.log('SKIPPED: 未找到微信开发者工具内置编译器(wcc/wcsc)')
console.log(' 可通过 WX_DEVTOOLS 指定安装目录;CI 环境无此工具属预期。')
console.log(' CI 侧的同类拦截由静态守卫 ui-tests/wxss-component-selector-contracts.mjs 承担。')
process.exit(0)
}
if (!fs.existsSync(ROOT)) {
console.error(`FAIL: 找不到小程序目录 ${ROOT} —— 拒绝报绿`)
process.exit(1)
}
const tmp = path.join(os.tmpdir(), 'miniapp-compile-check')
fs.mkdirSync(tmp, { recursive: true })
const failures = []
// ── WXSS ─────────────────────────────────────────────────────────────
const wxssFiles = walk(ROOT, '.wxss')
for (const f of wxssFiles) {
const rel = path.relative(ROOT, f).replace(/\\/g, '/')
const hasImport = /@import/.test(fs.readFileSync(f, 'utf8'))
const target = path.join(tmp, `${rel.replace(/\//g, '__')}`)
fs.writeFileSync(target, hasImport ? inlineImports(f) : fs.readFileSync(f, 'utf8'), 'utf8')
const err = compileOne(compiler.wcsc, target, tmp)
if (err) failures.push({ kind: 'WXSS', rel, err })
}
// ── WXML ─────────────────────────────────────────────────────────────
const wxmlFiles = walk(ROOT, '.wxml')
for (const f of wxmlFiles) {
const rel = path.relative(ROOT, f).replace(/\\/g, '/')
const err = compileOne(compiler.wcc, f, path.dirname(f))
if (err) failures.push({ kind: 'WXML', rel, err })
}
console.log(`小程序真编译预检(wcc / wcsc)`)
console.log(` WXSS : ${wxssFiles.length} 个`)
console.log(` WXML : ${wxmlFiles.length} 个`)
console.log(` 失败 : ${failures.length}`)
if (failures.length) {
console.log('--- 失败明细 ---')
for (const f of failures) console.log(`[${f.kind}] ${f.rel}\n ${f.err}`)
console.error(`\n❌ FAIL:${failures.length} 个文件编译不过`)
process.exit(1)
}
console.log('\n✅ 全部编译通过')