import * as esbuild from 'esbuild'; import { networkInterfaces } from 'node:os'; import { mkdir, copyFile } from 'node:fs/promises'; const args = process.argv.slice(2); const serve = args.includes('--serve'); const index = args.indexOf('--port'); const port = index >= 0 ? Number(args[index + 1]) : Number(process.env.PORT || 8002); const ctx = await esbuild.context({ entryPoints: ['web/main.ts'], bundle: true, format: 'iife', target: 'es2020', outfile: serve ? 'web/dist/app.js' : 'release/dist/app.js', minify: !serve, sourcemap: serve, logLevel: 'info', define: { __DEV__: JSON.stringify(serve) }, }); if (serve) { await ctx.watch(); const result = await ctx.serve({ host: '0.0.0.0', port, servedir: 'web' }); console.log(`\n连连气球 http://localhost:${result.port}`); for (const item of Object.values(networkInterfaces()).flat()) { if (item?.family === 'IPv4' && !item.internal) console.log(`手机同一 Wi-Fi: http://${item.address}:${result.port}`); } } else { await mkdir('release', { recursive: true }); for (const file of ['index.html', 'style.css']) await copyFile(`web/${file}`, `release/${file}`); await ctx.rebuild(); await ctx.dispose(); console.log('离线 / 静态发布目录:release/'); }