nomadro/apps/miniprogram/weapp/scripts/copy-weapp-static.mjs
eric 3e5b7c2098 Monorepo super-optimize: src layout, workspace scripts, docs.
Collapse portal under src/, unify pnpm workspace/scripts, thin VIP topic re-exports, drop orphan vip scaffolds and one-shot scripts, ignore xhs/release, document root commands.

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

107 lines
3.3 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.

/**
* Taro copy.patterns 偶发不落盘;构建后强制把 icons / assets 拷进 dist/weapp。
* 真机调试:本地 webp 在部分机型 Image 不渲染 → 同步导出 png,页面统一读 .png。
* 分享开关由 markPageShare 挂到 Page 组件(勿写入 page.json)。
* 仅微信;抖音见 tt/scripts/copy-static.mjs,小红书见 xhs/。
*/
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { spawnSync } from 'child_process'
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
}
/** 清掉历史误写入的 enableShare*,避免开发者工具刷黄字警告 */
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
}
/**
* webp → png(真机兼容)。保留原 webp 作备用。
* 用 Pillow;失败时不阻断构建。
*/
function convertIconsWebpToPng(iconsDir) {
if (!fs.existsSync(iconsDir)) return 0
const py = `
from pathlib import Path
try:
from PIL import Image
except ImportError:
raise SystemExit(2)
root = Path(r'''${iconsDir.replace(/\\/g, '/')}''')
n = 0
for src in root.glob('*.webp'):
dst = src.with_suffix('.png')
im = Image.open(src)
if im.mode not in ('RGB', 'RGBA'):
im = im.convert('RGBA')
im.save(dst, format='PNG', optimize=True)
n += 1
print(n)
`
const r = spawnSync('python', ['-c', py], { encoding: 'utf8' })
if (r.status === 2) {
console.warn('[copy-weapp-static] Pillow missing — skip webp→png (真机图标可能空白)')
return 0
}
if (r.status !== 0) {
console.warn('[copy-weapp-static] webp→png failed:', r.stderr || r.stdout)
return 0
}
const n = Number((r.stdout || '').trim()) || 0
return n
}
const h5Src = path.join(root, '..', 'h5', 'src')
const icons = copyDir(
path.join(h5Src, 'public', 'icons'),
path.join(root, out, 'icons'),
)
const pngs = convertIconsWebpToPng(path.join(root, out, 'icons'))
const assets = copyDir(
path.join(h5Src, 'assets'),
path.join(root, out, 'assets'),
)
const stripped = stripInvalidShareKeys(path.join(root, out, 'pages'))
console.log(
`[copy-weapp-static] ${out}: icons=${icons} png=${pngs} assets=${assets} strippedShareJson=${stripped}`,
)