import esbuild from 'esbuild' import fs from 'fs' import path from 'path' const isProduction = process.env.NODE_ENV === 'production' // 读取 package.json 中的所有依赖 const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8')) const allDeps = { ...packageJson.dependencies, ...packageJson.devDependencies, } const externalModules = Object.keys(allDeps) const config = { entryPoints: ['src/index.ts'], bundle: true, platform: 'node', target: 'esnext', format: 'esm', outfile: 'dist/index.js', external: externalModules, sourcemap: !isProduction, minify: isProduction, define: { 'process.env.NODE_ENV': `"${process.env.NODE_ENV || 'development'}"`, }, } async function build() { try { console.log('Building with esbuild...') await esbuild.build(config) // 生成 package.json const packageJson = { type: 'module' } fs.mkdirSync(path.dirname('dist/package.json'), { recursive: true }) fs.writeFileSync('dist/package.json', JSON.stringify(packageJson, null, 2)) console.log('✓ Build complete!') console.log(` Entry: ${config.entryPoints[0]}`) console.log(` Output: ${config.outfile}`) console.log(` Mode: ${isProduction ? 'production' : 'development'}`) console.log( ` Size: ${(fs.statSync(config.outfile).size / 1024).toFixed(1)}KB`, ) } catch (error) { console.error('✗ Build failed:', error.message) process.exit(1) } } build()