Refresh audience/admin UX, ntfy/track, social paths, and replace ebook chapters with the expanded catalog; keep local SQLite and one-off probe scripts out of tree. Co-authored-by: Cursor <cursoragent@cursor.com>
154 lines
4.9 KiB
JavaScript
154 lines
4.9 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 || {}
|
||
const outPath = () =>
|
||
this.distRoot || compiler.outputPath || path.resolve(__dirname, '..', 'dist')
|
||
|
||
const sanitizeFile = (abs) => {
|
||
try {
|
||
if (!abs || !abs.endsWith('.wxss') || !fs.existsSync(abs)) return false
|
||
const rel = path.relative(outPath(), abs).replace(/\\/g, '/')
|
||
if (!shouldSanitizeAsset(rel)) return false
|
||
const raw = fs.readFileSync(abs, 'utf8')
|
||
const next = sanitizeWxssSource(raw)
|
||
if (next === raw) return false
|
||
fs.writeFileSync(abs, next)
|
||
return true
|
||
} catch (err) {
|
||
console.error('[sanitize-wxss] file failed', abs, err?.message || err)
|
||
return false
|
||
}
|
||
}
|
||
|
||
// 落盘前清洗:避免微信热重载抢在 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 },
|
||
() => {
|
||
let n = 0
|
||
for (const name of Object.keys(compilation.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`)
|
||
}
|
||
},
|
||
)
|
||
})
|
||
}
|
||
|
||
// 每个 wxss 写出立刻洗 —— 真机调试上传会抢在 done 定时器前读盘
|
||
if (compiler.hooks.assetEmitted) {
|
||
compiler.hooks.assetEmitted.tap('EnsurePageWxssPlugin', (file, info) => {
|
||
const target =
|
||
(info && info.targetPath) ||
|
||
path.join(outPath(), String(file).replace(/\\/g, '/'))
|
||
if (sanitizeFile(target)) {
|
||
console.log(`[sanitize-wxss] assetEmitted ${String(file).replace(/\\/g, '/')}`)
|
||
}
|
||
})
|
||
}
|
||
|
||
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}`,
|
||
)
|
||
}
|
||
const root = outPath()
|
||
try {
|
||
const { changed, files } = sanitizeDir(root)
|
||
console.log(
|
||
`[sanitize-wxss] ${why || 'run'}: cleaned ${changed}/${files} under ${root}`,
|
||
)
|
||
} 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)
|
||
})
|
||
}
|
||
}
|
||
|
||
module.exports = { EnsurePageWxssPlugin, ensure }
|