Wire Taro social pages to social_svc (Centrifugo + live RTMP), drop multi-channel chat chrome, and add weapp WXSS sanitize/deploy helpers. Co-authored-by: Cursor <cursoragent@cursor.com>
123 lines
3.6 KiB
JavaScript
123 lines
3.6 KiB
JavaScript
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) => {
|
|
const stage =
|
|
compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE ?? 7000
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
compiler.hooks.done.tap('EnsurePageWxssPlugin', () => {
|
|
run('done')
|
|
setTimeout(() => run('300ms'), 300)
|
|
setTimeout(() => run('1200ms'), 1200)
|
|
setTimeout(() => run('3000ms'), 3000)
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = { EnsurePageWxssPlugin, ensure }
|