# PowerShell 静态构建脚本 Write-Host "开始静态构建..." -ForegroundColor Green # 1. 清理之前的构建文件 Write-Host "1. 清理之前的构建文件..." -ForegroundColor Yellow if (Test-Path ".next") { Remove-Item ".next" -Recurse -Force -ErrorAction SilentlyContinue Write-Host " 已清理 .next 目录" -ForegroundColor Gray } if (Test-Path "out") { Remove-Item "out" -Recurse -Force -ErrorAction SilentlyContinue Write-Host " 已清理 out 目录" -ForegroundColor Gray } # 2. 临时重命名 API 目录 Write-Host "2. 临时重命名 API 目录..." -ForegroundColor Yellow $apiRenamed = $false if (Test-Path "app\api") { try { Rename-Item "app\api" "app\api-temp-disabled" -ErrorAction Stop $apiRenamed = $true Write-Host " API 目录已临时重命名" -ForegroundColor Gray } catch { Write-Host " 警告: 无法重命名 API 目录,继续构建..." -ForegroundColor Red } } # 3. 构建静态文件 Write-Host "3. 构建静态文件..." -ForegroundColor Yellow $buildResult = 1 & npm run build:static if ($LASTEXITCODE -eq 0) { $buildResult = 0 Write-Host " 构建成功完成" -ForegroundColor Green } else { Write-Host " 构建失败,错误代码: $LASTEXITCODE" -ForegroundColor Red } # 4. 恢复 API 目录 Write-Host "4. 恢复 API 目录..." -ForegroundColor Yellow if ($apiRenamed -and (Test-Path "app\api-temp-disabled")) { try { Rename-Item "app\api-temp-disabled" "app\api" -ErrorAction Stop Write-Host " API 目录已恢复" -ForegroundColor Gray } catch { Write-Host " 警告: 无法恢复 API 目录名称,请手动将 app\api-temp-disabled 重命名为 app\api" -ForegroundColor Red } } # 5. 显示结果 Write-Host "" if ($buildResult -eq 0) { Write-Host "✅ 静态构建完成!" -ForegroundColor Green Write-Host "📁 静态文件位置: out/ 目录" -ForegroundColor Cyan Write-Host "🚀 可以将 out/ 目录上传到服务器" -ForegroundColor Cyan Write-Host "" Write-Host "预览命令: npm run preview" -ForegroundColor Yellow } else { Write-Host "❌ 构建失败!" -ForegroundColor Red Write-Host "错误代码: $buildResult" -ForegroundColor Red } Write-Host "" Write-Host "按任意键继续..." -ForegroundColor Gray Read-Host "按回车键继续"