const fs = require('fs') const path = require('path') const STUB = '/* page styles live in common.wxss */\n' function ensure(pagesRoot) { if (!pagesRoot || !fs.existsSync(pagesRoot)) return 0 let n = 0 for (const name of fs.readdirSync(pagesRoot)) { const dir = path.join(pagesRoot, name) let st try { st = fs.statSync(dir) } catch { continue } if (!st.isDirectory()) continue const js = path.join(dir, 'index.js') const wxss = path.join(dir, 'index.wxss') if (!fs.existsSync(js) || fs.existsSync(wxss)) continue fs.writeFileSync(wxss, STUB) n += 1 } return n } /** 样式进 common 后缺页面 wxss → 微信 ENOENT;编译完成后补空文件 */ class EnsurePageWxssPlugin { constructor(opts = {}) { this.pagesRoot = opts.pagesRoot } apply(compiler) { const run = () => { const n = ensure(this.pagesRoot) if (n > 0) { console.log(`[ensure-page-wxss] stubbed ${n} page wxss under ${this.pagesRoot}`) } } compiler.hooks.done.tap('EnsurePageWxssPlugin', () => { run() // Taro 部分资源可能略晚于 done 落盘 setTimeout(run, 300) setTimeout(run, 1200) }) } } module.exports = { EnsurePageWxssPlugin, ensure }