Keep 20261011 hub visuals on home; index only reLaunches to avoid missing page wxss.
Co-authored-by: Cursor <cursoragent@cursor.com>
108 lines
3.3 KiB
JavaScript
108 lines
3.3 KiB
JavaScript
/**
|
||
* 构建后静态资源整理(微信):
|
||
* - 导航图标不进代码包(走 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)
|
||
console.log(
|
||
`[copy-weapp-static] ${out}: icons=cdn assets=${assets} strippedShareJson=${stripped} stubWxss=${stubWxss}`,
|
||
)
|