diff --git a/apps/miniprogram/tt/scripts/copy-static.mjs b/apps/miniprogram/tt/scripts/copy-static.mjs index 3f6f177..96237ab 100644 --- a/apps/miniprogram/tt/scripts/copy-static.mjs +++ b/apps/miniprogram/tt/scripts/copy-static.mjs @@ -4,6 +4,7 @@ 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' @@ -49,11 +50,44 @@ function stripMaps(dir) { return n } +/** 真机兼容:webp → png(保留原 webp) */ +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-static] Pillow missing — skip webp→png') + return 0 + } + if (r.status !== 0) { + console.warn('[copy-static] webp→png failed:', r.stderr || r.stdout) + return 0 + } + return Number((r.stdout || '').trim()) || 0 +} + const icons = copyDir( path.join(root, 'src/public/icons'), path.join(root, out, 'icons'), { wipe: true }, ) +const pngs = convertIconsWebpToPng(path.join(root, out, 'icons')) const assets = copyDir( path.join(root, 'src/assets'), path.join(root, out, 'assets'), @@ -66,5 +100,5 @@ const bookImgs = copyDir( ) const maps = stripMaps(path.join(root, out)) console.log( - `[copy-static] ${out}: icons=${icons} assets=${assets} bookImgs=${bookImgs} mapsRemoved=${maps}`, + `[copy-static] ${out}: icons=${icons} png=${pngs} assets=${assets} bookImgs=${bookImgs} mapsRemoved=${maps}`, ) diff --git a/apps/miniprogram/tt/src/pages/nav/index.tsx b/apps/miniprogram/tt/src/pages/nav/index.tsx index b857940..f4b0214 100644 --- a/apps/miniprogram/tt/src/pages/nav/index.tsx +++ b/apps/miniprogram/tt/src/pages/nav/index.tsx @@ -48,9 +48,12 @@ function countLinks(cat: NavCategory) { function iconSrc(icon: string) { if (!icon) return '' - // 必须从小程序包根读:/icons/xxx.webp(去掉 / 会变成相对 pages/nav/,全裂) - const cleaned = icon.replace(/^\//, '') - return `/icons/${cleaned.replace(/^icons\//, '')}` + // 真机:绝对路径 /icons 偶发失败;相对 pages/nav → ../../icons + // 真机对本地 webp 常空白 → 统一 png(copy-static 已从 webp 导出) + const cleaned = icon.replace(/^\//, '').replace(/^icons\//, '') + const base = cleaned.replace(/\.(webp|png|jpe?g)$/i, '') + if (process.env.TARO_ENV === 'h5') return `/icons/${base}.png` + return `../../icons/${base}.png` } const MENU_SVG = (open: boolean) => @@ -276,7 +279,7 @@ function NavPageInner() { {isH5 ? ( ) : ( - + )} nomadro · @@ -300,7 +303,7 @@ function NavPageInner() { - + {nav.brand} {nav.tagline}