import { execSync } from 'child_process'; import fs from 'fs'; import path from 'path'; function forceCleanDir(dirPath) { if (!fs.existsSync(dirPath)) return; for (const entry of fs.readdirSync(dirPath, { withFileTypes: true })) { const fullPath = path.join(dirPath, entry.name); if (entry.isDirectory()) { forceCleanDir(fullPath); try { fs.rmdirSync(fullPath); } catch { try { fs.rmSync(fullPath, { recursive: true, force: true }); } catch {} } } else { try { fs.unlinkSync(fullPath); } catch {} } } } console.log('🧹 [Clean] 이전 λΉŒλ“œ 파일 μ™„μ „ 정리 쀑...'); const distPath = path.resolve('dist'); forceCleanDir(distPath); try { fs.rmSync(distPath, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 }); } catch {} console.log('πŸš€ [Build] Cloudflare Pages 2,000개 μ œν•œ μ™„λ²½ λŒ€μ‘ λΉŒλ“œ μ‹œμž‘...'); try { // 1. μ‚¬μ΄νŠΈλ§΅ 생성 execSync('npm run sitemap', { stdio: 'inherit' }); // 2. ν΄λΌμ΄μ–ΈνŠΈ λΉŒλ“œ execSync('npm run build:client', { stdio: 'inherit' }); // 3. μ„œλ²„ λΉŒλ“œ execSync('npm run build:server', { stdio: 'inherit' }); // 4. 핡심 μ§€μ—­ SSG 사전 λ Œλ”λ§ execSync('node scripts/prerender.js', { stdio: 'inherit', env: { ...process.env, PRERENDER_MODE: 'major' } }); // 5. ν›„μ²˜λ¦¬ (_worker.js 생성) 및 dist.zip 생성 execSync('node scripts/post-build.js', { stdio: 'inherit' }); let files = 0; let dirs = 0; const countWalk = (p) => { for (const e of fs.readdirSync(p, { withFileTypes: true })) { const fp = path.join(p, e.name); if (e.isDirectory()) { dirs++; countWalk(fp); } else { files++; } } }; countWalk(distPath); console.log(`\nπŸŽ‰ [Success] Cloudflare 2,000개 μ œν•œ μ΄ν•˜ μ•ˆμ „ λΉŒλ“œ μ™„λ£Œ!`); console.log(`πŸ‘‰ dist λ‚΄ 파일 수: ${files}개 | 폴더 수: ${dirs}개 | 전체 ν•­λͺ© 합계: ${files + dirs}개 (2,000개 미만 μΆ©μ‘±)`); console.log(`πŸ‘‰ 정적 ν”„λ¦¬λ Œλ”λ§ + dist/_worker.js μ‹€μ‹œκ°„ SSR둜 μ „κ΅­ 50,380개 λͺ¨λ“  μ§€μ—­ νŽ˜μ΄μ§€ 200 OK μ™„λ²½ 응닡`); } catch (error) { console.error('❌ λΉŒλ“œ 쀑 였λ₯˜ λ°œμƒ:', error); process.exit(1); }