111 lines
2.4 KiB
JavaScript
111 lines
2.4 KiB
JavaScript
/**
|
|
* 从 vant/lib/index.css 生成 WXSS 兼容样式:去掉含 * / *= 的选择器规则。
|
|
* 升级 vant 后执行:node scripts/gen-vant-mp-safe-css.js
|
|
*/
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
const root = path.join(__dirname, '..')
|
|
const src = path.join(root, 'node_modules', 'vant', 'lib', 'index.css')
|
|
const dest = path.join(root, 'styles', 'vant-mp-safe.css')
|
|
|
|
if (!fs.existsSync(src)) {
|
|
console.error('[gen-vant-mp-safe-css] missing', src)
|
|
process.exit(1)
|
|
}
|
|
|
|
const text = fs.readFileSync(src, 'utf8')
|
|
const out = []
|
|
let i = 0
|
|
const n = text.length
|
|
let removed = 0
|
|
let kept = 0
|
|
|
|
function isBadSelector(sel) {
|
|
// universal selector `*` (not `*=` attr or content "*")
|
|
if (/(^|[,)\s>+~])\*(?!=)/.test(sel)) return true
|
|
if (sel.includes('*=')) return true
|
|
return false
|
|
}
|
|
|
|
while (i < n) {
|
|
if (text.startsWith('/*', i)) {
|
|
const j = text.indexOf('*/', i + 2)
|
|
if (j < 0) break
|
|
out.push(text.slice(i, j + 2))
|
|
i = j + 2
|
|
continue
|
|
}
|
|
if (/\s/.test(text[i])) {
|
|
out.push(text[i])
|
|
i += 1
|
|
continue
|
|
}
|
|
if (text[i] === '@') {
|
|
const brace = text.indexOf('{', i)
|
|
const semi = text.indexOf(';', i)
|
|
if (semi !== -1 && (brace === -1 || semi < brace)) {
|
|
out.push(text.slice(i, semi + 1))
|
|
i = semi + 1
|
|
continue
|
|
}
|
|
if (brace < 0) {
|
|
out.push(text.slice(i))
|
|
break
|
|
}
|
|
let depth = 0
|
|
let j = brace
|
|
while (j < n) {
|
|
if (text[j] === '{') depth += 1
|
|
else if (text[j] === '}') {
|
|
depth -= 1
|
|
if (depth === 0) {
|
|
j += 1
|
|
break
|
|
}
|
|
}
|
|
j += 1
|
|
}
|
|
const block = text.slice(i, j)
|
|
if (isBadSelector(block)) removed += 1
|
|
else {
|
|
out.push(block)
|
|
kept += 1
|
|
}
|
|
i = j
|
|
continue
|
|
}
|
|
|
|
const brace = text.indexOf('{', i)
|
|
if (brace < 0) {
|
|
out.push(text.slice(i))
|
|
break
|
|
}
|
|
let depth = 0
|
|
let j = brace
|
|
while (j < n) {
|
|
if (text[j] === '{') depth += 1
|
|
else if (text[j] === '}') {
|
|
depth -= 1
|
|
if (depth === 0) {
|
|
j += 1
|
|
break
|
|
}
|
|
}
|
|
j += 1
|
|
}
|
|
const sel = text.slice(i, brace)
|
|
const rule = text.slice(i, j)
|
|
if (isBadSelector(sel)) removed += 1
|
|
else {
|
|
out.push(rule)
|
|
kept += 1
|
|
}
|
|
i = j
|
|
}
|
|
|
|
const header =
|
|
'/* auto-generated from vant/lib/index.css — WXSS-safe (no * / *= selectors). Do not edit. */\n'
|
|
fs.writeFileSync(dest, header + out.join(''))
|
|
console.log('[gen-vant-mp-safe-css] kept=%s removed=%s -> %s', kept, removed, dest)
|