nomadro/apps/miniprogram/weapp/scripts/copy-weapp-static.mjs
Eric d0262eb58d Ship WeChat audit mode, package-size fixes, and live VIP switches.
Harden weapp for review: silent audit book, no flashy module defaults, safe-area PX, subpackages/lazyCodeLoading, and CDN nav icons. Document pre3 and move next work to pre4.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-11 15:12:06 +08:00

90 lines
2.6 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 { 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
}
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 stripped = stripInvalidShareKeys(path.join(root, out, 'pages'))
console.log(
`[copy-weapp-static] ${out}: icons=cdn assets=${assets} strippedShareJson=${stripped}`,
)