nomadro/apps/miniprogram/weapp/scripts/copy-weapp-static.mjs
Eric 4789785f11 Ship remaining business changes across social API, miniprogram audit/book, VIP track, and deploy.
Includes social realtime entrypoints, weapp audit book surfaces, VIP analytics track routes, MiroTalk/Centrifugo deploy helpers, and related admin/config wiring. Excludes local DB dumps and .tmp probe scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-01 17:10:10 +08:00

154 lines
4.8 KiB
JavaScript
Raw 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 { fileURLToPath } from '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
for (const name of fs.readdirSync(pagesRoot)) {
const dir = path.join(pagesRoot, name)
if (!fs.statSync(dir).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, '/* 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)
// 提审小册:纯原生页 + 纯原生 App,彻底绕开 Taro/React(否则 App 壳仍会拖垮首屏)
const nativeBook = path.join(root, 'native-audit-book')
const bookDist = path.join(pagesRoot, 'book')
let nativeBookCopied = 0
if (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,
),
)
}
console.log(
`[copy-weapp-static] ${out}: icons=cdn assets=${assets} strippedShareJson=${stripped} stubWxss=${stubWxss} nativeBook=${nativeBookCopied} nativeApp=1`,
)