nomadro/apps/miniprogram/weapp/scripts/ensure-page-wxss-plugin.cjs
Eric 4389ebe6cb Ship WeChat audit release 2026-11-02: realtime config, audit vip-ebook shell, admin ops.
Document test9 tag 2026-11-02 for upload; POST config pull + WS, fixed dark home, no debug bar.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-02 16:12:57 +08:00

128 lines
3.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require('fs')
const path = require('path')
const { sanitizeDir, sanitizeWxssSource } = require('./sanitize-wxss-plugin.cjs')
const STUB = '/* page styles live in common.wxss */\n'
function shouldSanitizeAsset(name) {
const rel = String(name || '').replace(/\\/g, '/')
return /pages\/social\//.test(rel) || /(?:^|\/)common\.wxss$/.test(rel)
}
/** 递归:凡有 index.js 的页面目录都补 index.wxss */
function ensure(pagesRoot) {
if (!pagesRoot || !fs.existsSync(pagesRoot)) return 0
let n = 0
const stack = [pagesRoot]
while (stack.length) {
const dir = stack.pop()
let entries
try {
entries = fs.readdirSync(dir)
} catch {
continue
}
for (const name of entries) {
const full = path.join(dir, name)
let st
try {
st = fs.statSync(full)
} catch {
continue
}
if (st.isDirectory()) {
stack.push(full)
continue
}
}
const js = path.join(dir, 'index.js')
const wxss = path.join(dir, 'index.wxss')
if (fs.existsSync(js) && !fs.existsSync(wxss)) {
fs.writeFileSync(wxss, STUB)
n += 1
}
}
return n
}
/**
* 1) 样式进 common 后缺页面 wxss → 补空文件,避免微信 ENOENT
* 2) 清洗 @layer / :has / :is / * 等 WXSS 不支持语法
*/
class EnsurePageWxssPlugin {
constructor(opts = {}) {
this.pagesRoot = opts.pagesRoot
this.distRoot =
opts.distRoot ||
(opts.pagesRoot ? path.resolve(opts.pagesRoot, '..') : undefined)
}
apply(compiler) {
const webpack = compiler.webpack || require('webpack')
const { RawSource } = webpack.sources || {}
// 落盘前清洗:避免微信热重载抢在 setTimeout sanitize 前编译 :is()
if (RawSource && compiler.hooks.compilation) {
compiler.hooks.compilation.tap('EnsurePageWxssPlugin', (compilation) => {
// 尽量靠后:等 mini-css/extract 写完再洗,避免微信抢编译脏 * / :has
const stage =
compilation.constructor.PROCESS_ASSETS_STAGE_REPORT ?? 5000
compilation.hooks.processAssets.tap(
{ name: 'EnsurePageWxssPlugin', stage },
(assets) => {
let n = 0
for (const name of Object.keys(assets)) {
if (!name.endsWith('.wxss') || !shouldSanitizeAsset(name)) continue
const asset = compilation.getAsset(name)
if (!asset) continue
const raw = asset.source.source()
const src = Buffer.isBuffer(raw) ? raw.toString('utf8') : String(raw)
const next = sanitizeWxssSource(src)
if (next !== src) {
compilation.updateAsset(name, new RawSource(next))
n += 1
}
}
if (n > 0) {
console.log(`[sanitize-wxss] processAssets cleaned ${n} wxss`)
}
},
)
})
}
const run = (why = '') => {
const n = ensure(this.pagesRoot)
if (n > 0) {
console.log(
`[ensure-page-wxss] stubbed ${n} page wxss${why ? ` (${why})` : ''} under ${this.pagesRoot}`,
)
}
if (this.distRoot) {
try {
const { changed, files } = sanitizeDir(this.distRoot)
if (changed > 0) {
console.log(
`[sanitize-wxss] cleaned ${changed}/${files} wxss under ${this.distRoot}`,
)
}
} catch (err) {
console.error('[sanitize-wxss] failed', err)
}
}
}
// afterEmit:文件已落盘立刻同步洗,抢在微信热编译前
compiler.hooks.afterEmit.tap('EnsurePageWxssPlugin', () => {
run('afterEmit')
})
compiler.hooks.done.tap('EnsurePageWxssPlugin', () => {
run('done')
setTimeout(() => run('300ms'), 300)
setTimeout(() => run('1200ms'), 1200)
setTimeout(() => run('3000ms'), 3000)
})
}
}
module.exports = { EnsurePageWxssPlugin, ensure }