nomadro/apps/miniprogram/weapp/scripts/copy-weapp-static.mjs
Eric 92bbee3c6c Ship native social weapp: immersive hall chat, SRS live, join pay.
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>
2026-11-02 12:33:35 +08:00

217 lines
7.0 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.

/**
* 构建后静态资源整理(微信):
* - 导航图标不进代码包(走 https://nomadro.cn/icons,满足图片合计≤200K)
* - 仅保留必要 assets(如公众号二维码)
* - 清掉历史误写入的 enableShare*
*/
import fs from 'fs'
import path from 'path'
import { createRequire } from 'module'
import { fileURLToPath } from 'url'
const require = createRequire(import.meta.url)
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
const out = 'dist'
function copyDir(from, to) {
if (!fs.existsSync(from)) {
console.warn(`[copy-weapp-static] skip missing: ${from}`)
return 0
}
fs.mkdirSync(to, { recursive: true })
let n = 0
for (const name of fs.readdirSync(from)) {
const src = path.join(from, name)
const dest = path.join(to, name)
const st = fs.statSync(src)
if (st.isDirectory()) n += copyDir(src, dest)
else {
fs.copyFileSync(src, dest)
n += 1
}
}
return n
}
function rmDir(dir) {
if (!fs.existsSync(dir)) return
fs.rmSync(dir, { recursive: true, force: true })
}
function stripInvalidShareKeys(pagesRoot) {
if (!fs.existsSync(pagesRoot)) return 0
let n = 0
for (const name of fs.readdirSync(pagesRoot)) {
const jsonPath = path.join(pagesRoot, name, 'index.json')
if (!fs.existsSync(jsonPath)) continue
try {
const raw = fs.readFileSync(jsonPath, 'utf8').replace(/^\uFEFF/, '')
const j = JSON.parse(raw)
if (!('enableShareAppMessage' in j) && !('enableShareTimeline' in j)) continue
delete j.enableShareAppMessage
delete j.enableShareTimeline
fs.writeFileSync(jsonPath, JSON.stringify(j))
n += 1
} catch (e) {
console.warn(`[copy-weapp-static] strip share fail ${jsonPath}:`, e.message)
}
}
return n
}
/** 样式进 common.wxss 时 Taro 可能不落页面 wxss,微信仍会 open 该文件 → ENOENT */
function ensurePageWxss(pagesRoot) {
if (!fs.existsSync(pagesRoot)) return 0
let n = 0
const stack = [pagesRoot]
while (stack.length) {
const dir = stack.pop()
for (const name of fs.readdirSync(dir)) {
const full = path.join(dir, name)
const st = fs.statSync(full)
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, '/* page styles live in common.wxss */\n')
n += 1
}
}
return n
}
const h5Src = path.join(root, '..', 'h5', 'src')
// 主包/分包历史图标一律删掉(改 CDN)
rmDir(path.join(root, out, 'icons'))
rmDir(path.join(root, out, 'assets', 'icons'))
rmDir(path.join(root, out, 'pages', 'nav', 'icons'))
// assets:只拷非 icons
const assetsSrc = path.join(h5Src, 'assets')
const assetsDst = path.join(root, out, 'assets')
let assets = 0
if (fs.existsSync(assetsSrc)) {
fs.mkdirSync(assetsDst, { recursive: true })
for (const name of fs.readdirSync(assetsSrc)) {
if (name === 'icons') continue
const src = path.join(assetsSrc, name)
const dest = path.join(assetsDst, name)
const st = fs.statSync(src)
if (st.isDirectory()) assets += copyDir(src, dest)
else {
fs.copyFileSync(src, dest)
assets += 1
}
}
}
const pagesRoot = path.join(root, out, 'pages')
const stripped = stripInvalidShareKeys(pagesRoot)
const stubWxss = ensurePageWxss(pagesRoot)
// 提审小册:仅 TARO_APP_AUDIT=1 时覆盖;平时 dev 禁止动 book/app(否则缺 wxss / 启动页被改坏)
const nativeBook = path.join(root, 'native-audit-book')
const bookDist = path.join(pagesRoot, 'book')
let nativeBookCopied = 0
const auditMode = process.env.TARO_APP_AUDIT === '1'
if (auditMode && fs.existsSync(nativeBook)) {
fs.mkdirSync(bookDist, { recursive: true })
for (const name of ['index.js', 'index.json', 'index.wxml', 'index.wxss']) {
const src = path.join(nativeBook, name)
if (!fs.existsSync(src)) continue
fs.copyFileSync(src, path.join(bookDist, name))
nativeBookCopied += 1
}
for (const name of fs.readdirSync(bookDist)) {
if (name.startsWith('sub-common') || name.endsWith('.js.map')) {
const p = path.join(bookDist, name)
const st = fs.statSync(p)
if (st.isDirectory()) rmDir(p)
else fs.unlinkSync(p)
}
}
fs.writeFileSync(path.join(root, out, 'app.js'), 'App({})\n')
fs.writeFileSync(
path.join(root, out, 'app.wxss'),
'page{background:#0c0b12;color:#f8fafc;box-sizing:border-box}\n',
)
fs.writeFileSync(
path.join(root, out, 'app.json'),
JSON.stringify(
{
pages: ['pages/book/index'],
window: {
navigationBarTitleText: '数字游民入门指南',
navigationBarBackgroundColor: '#0c0b12',
navigationBarTextStyle: 'white',
backgroundColor: '#0c0b12',
backgroundTextStyle: 'light',
},
},
null,
0,
),
)
}
// social Next CSS → WXSS:清 @layer / :has / :is(若 dist 已生成)
try {
const { sanitizeDir } = require('./sanitize-wxss-plugin.cjs')
const distAbs = path.join(root, out)
if (fs.existsSync(distAbs)) {
const r = sanitizeDir(distAbs)
if (r.changed > 0) {
console.log(`[copy-weapp-static] sanitize-wxss cleaned ${r.changed}/${r.files}`)
}
}
} catch (err) {
console.warn('[copy-weapp-static] sanitize-wxss skip:', err?.message || err)
}
// 小程序 AI 开发模式 SKILL:仅 TARO_APP_AI_MODE=1 且非提审时拷入并注入 app.json;勿合入正式提审包
let aiPkg = 0
const aiMode = process.env.TARO_APP_AI_MODE === '1' && !auditMode
if (aiMode) {
const aiSrc = path.join(root, 'ai-pkg')
const aiDst = path.join(root, out, 'ai-pkg')
if (fs.existsSync(aiSrc)) {
aiPkg = copyDir(aiSrc, aiDst)
}
const appJsonPath = path.join(root, out, 'app.json')
if (fs.existsSync(appJsonPath) && aiPkg > 0) {
try {
const appJson = JSON.parse(fs.readFileSync(appJsonPath, 'utf8').replace(/^\uFEFF/, ''))
const subs = Array.isArray(appJson.subPackages) ? appJson.subPackages : []
if (!subs.some((s) => s && s.root === 'ai-pkg')) {
subs.push({ root: 'ai-pkg', name: 'ai-pkg', independent: true, pages: [] })
}
appJson.subPackages = subs
appJson.lazyCodeLoading = 'requiredComponents'
appJson.agent = {
instruction: 'ai-pkg/AGENTS.md',
skills: [
{
name: 'socialSkill',
description: '游民社区:加群收费、开视频、找搭子申请',
path: 'ai-pkg/socialSkill',
},
],
}
fs.writeFileSync(appJsonPath, JSON.stringify(appJson))
console.log('[copy-weapp-static] injected agent.skills + ai-pkg independent subPackage')
} catch (e) {
console.warn('[copy-weapp-static] agent inject fail:', e?.message || e)
}
}
}
console.log(
`[copy-weapp-static] ${out}: icons=cdn assets=${assets} strippedShareJson=${stripped} stubWxss=${stubWxss} nativeBook=${nativeBookCopied} audit=${auditMode ? 1 : 0} aiPkg=${aiPkg}`,
)