/** * Web 构建脚本 */ import * as fs from 'fs/promises'; import * as path from 'path'; import { execSync } from 'child_process'; import * as esbuild from 'esbuild'; const ROOT = path.resolve(import.meta.dirname, '..'); const DIST_WEB = path.join(ROOT, 'dist', 'web'); async function main() { console.log('[build-web] 开始构建...'); // 重要: 不能 rm -rf 整个 dist/web/, 否则会删掉 build:main 编译出来的 // dist/web/server.js. 只清理 web 静态资源, 保留 server.js. for (const f of ['index.html', 'api-config.html', 'explorer.html', 'style.css', 'client.js', 'chain-explorer.js', 'mobile.html', 'mobile.css', 'mobile.js', 'mobile-core.js', 'components']) { await fs.rm(path.join(DIST_WEB, f), { recursive: true, force: true }); } await fs.mkdir(DIST_WEB, { recursive: true }); await fs.mkdir(path.join(DIST_WEB, 'components', 'p2p'), { recursive: true }); // 编译 TypeScript 模块 console.log('[build-web] 编译 TypeScript 模块...'); const moduleFiles = [ 'src/web/components/p2p/types.ts', 'src/web/components/p2p/p2p-store-memory.ts', 'src/web/components/p2p/p2p-identity.ts', 'src/web/components/p2p/p2p-connection.ts', 'src/web/components/p2p/p2p-messages.ts', 'src/web/components/p2p/p2p-manager.ts', ]; try { execSync(`npx tsc --ignoreConfig --outDir dist/web/components/p2p --declaration false --skipLibCheck --target ES2022 --module ESNext --moduleResolution bundler ${moduleFiles.join(' ')}`, { cwd: ROOT, stdio: 'inherit' }); } catch { console.log('[build-web] TypeScript 编译有警告,继续...'); } // 编译 P2P Modal UI(esbuild) console.log('[build-web] 编译 P2P Modal UI...'); await esbuild.build({ entryPoints: [path.join(ROOT, 'src/web/components/p2p/index.ts')], outfile: path.join(DIST_WEB, 'components/p2p/index.js'), format: 'esm', target: 'es2022', platform: 'browser', minify: false, }); // 2026-06-15: 编译 message-renderer.ts (对话显示 UI 模块) // esbuild 编译 .ts → dist/web/ui/message-renderer.js (ESM, 浏览器侧