|
| 1 | +/** |
| 2 | + * Production Builder |
| 3 | + * |
| 4 | + * Orchestrates the production build process: |
| 5 | + * 1. Discover routes from pages/ |
| 6 | + * 2. Generate shared assets (runtime, router) |
| 7 | + * 3. Compile all templates |
| 8 | + * 4. Generate CSS from all pages |
| 9 | + * 5. Write .output/ directory |
| 10 | + * 6. Generate manifest |
| 11 | + * |
| 12 | + * @module production-builder |
| 13 | + */ |
| 14 | + |
| 15 | +import fs from 'node:fs' |
| 16 | +import path from 'node:path' |
| 17 | +import { createRouter, type Route } from './router' |
| 18 | +import { buildRuntimeAsset, buildRouterAsset, type BuiltAsset } from './build-assets' |
| 19 | +import { compileTemplate, type CompiledTemplate } from './template-compiler' |
| 20 | +import { generateManifest, writeManifest, type ManifestRoute, type ManifestAssets } from './manifest' |
| 21 | + |
| 22 | +/** |
| 23 | + * Production build configuration. |
| 24 | + */ |
| 25 | +export interface ProductionBuildOptions { |
| 26 | + /** Project root directory (default: process.cwd()) */ |
| 27 | + root?: string |
| 28 | + /** Output directory (default: '.output') */ |
| 29 | + outputDir?: string |
| 30 | + /** Enable debug/dev runtime (default: false) */ |
| 31 | + debug?: boolean |
| 32 | + /** Components directory */ |
| 33 | + componentsDir?: string |
| 34 | + /** Partials directory */ |
| 35 | + partialsDir?: string |
| 36 | + /** Layouts directory */ |
| 37 | + layoutsDir?: string |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Result of a production build. |
| 42 | + */ |
| 43 | +export interface ProductionBuildResult { |
| 44 | + /** Number of pages compiled */ |
| 45 | + pageCount: number |
| 46 | + /** Generated asset filenames */ |
| 47 | + assets: ManifestAssets |
| 48 | + /** Output directory path */ |
| 49 | + outputDir: string |
| 50 | + /** Build duration in ms */ |
| 51 | + duration: number |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Build the stx application for production. |
| 56 | + * |
| 57 | + * Produces a `.output/` directory with: |
| 58 | + * - `public/__stx/` — fingerprinted runtime and router JS |
| 59 | + * - `public/assets/` — bundled CSS |
| 60 | + * - `server/pages/` — compiled template JSON files |
| 61 | + * - `server/fragments/` — SPA navigation fragments |
| 62 | + * - `manifest.json` — route map and asset hashes |
| 63 | + */ |
| 64 | +export async function buildForProduction(options: ProductionBuildOptions = {}): Promise<ProductionBuildResult> { |
| 65 | + const startTime = Date.now() |
| 66 | + const root = path.resolve(options.root || process.cwd()) |
| 67 | + const outputDir = path.resolve(root, options.outputDir || '.output') |
| 68 | + |
| 69 | + console.log('[stx build] Starting production build...') |
| 70 | + |
| 71 | + // ── 1. Clean output directory ── |
| 72 | + if (fs.existsSync(outputDir)) { |
| 73 | + fs.rmSync(outputDir, { recursive: true }) |
| 74 | + } |
| 75 | + |
| 76 | + // Create directory structure |
| 77 | + const dirs = [ |
| 78 | + path.join(outputDir, 'public', '__stx'), |
| 79 | + path.join(outputDir, 'public', 'assets'), |
| 80 | + path.join(outputDir, 'server', 'pages'), |
| 81 | + path.join(outputDir, 'server', 'fragments'), |
| 82 | + ] |
| 83 | + for (const dir of dirs) { |
| 84 | + fs.mkdirSync(dir, { recursive: true }) |
| 85 | + } |
| 86 | + |
| 87 | + // ── 2. Discover routes ── |
| 88 | + console.log('[stx build] Discovering routes...') |
| 89 | + const routes = createRouter(root) |
| 90 | + console.log(`[stx build] Found ${routes.length} routes`) |
| 91 | + |
| 92 | + // ── 3. Generate shared assets ── |
| 93 | + console.log('[stx build] Generating shared assets...') |
| 94 | + const runtimeAsset = buildRuntimeAsset(options.debug) |
| 95 | + const routerAsset = buildRouterAsset() |
| 96 | + |
| 97 | + // Write runtime JS |
| 98 | + fs.writeFileSync( |
| 99 | + path.join(outputDir, 'public', '__stx', runtimeAsset.filename), |
| 100 | + runtimeAsset.content, |
| 101 | + ) |
| 102 | + // Write router JS |
| 103 | + fs.writeFileSync( |
| 104 | + path.join(outputDir, 'public', '__stx', routerAsset.filename), |
| 105 | + routerAsset.content, |
| 106 | + ) |
| 107 | + |
| 108 | + console.log(`[stx build] Runtime: ${runtimeAsset.filename} (${(runtimeAsset.content.length / 1024).toFixed(1)}KB)`) |
| 109 | + console.log(`[stx build] Router: ${routerAsset.filename} (${(routerAsset.content.length / 1024).toFixed(1)}KB)`) |
| 110 | + |
| 111 | + // ── 4. Compile all templates ── |
| 112 | + console.log('[stx build] Compiling templates...') |
| 113 | + const compiledPages: CompiledTemplate[] = [] |
| 114 | + const manifestRoutes: ManifestRoute[] = [] |
| 115 | + |
| 116 | + for (const route of routes) { |
| 117 | + try { |
| 118 | + const compiled = await compileTemplate(route.filePath, route.pattern, { |
| 119 | + componentsDir: options.componentsDir, |
| 120 | + partialsDir: options.partialsDir, |
| 121 | + layoutsDir: options.layoutsDir, |
| 122 | + debug: options.debug, |
| 123 | + }) |
| 124 | + |
| 125 | + compiledPages.push(compiled) |
| 126 | + |
| 127 | + // Replace asset hash placeholders with actual fingerprinted filenames |
| 128 | + compiled.html = compiled.html |
| 129 | + .replace(/runtime\.__STX_HASH__\.js/g, runtimeAsset.filename) |
| 130 | + .replace(/router\.__STX_HASH__\.js/g, routerAsset.filename) |
| 131 | + compiled.fragment = compiled.fragment |
| 132 | + .replace(/runtime\.__STX_HASH__\.js/g, runtimeAsset.filename) |
| 133 | + .replace(/router\.__STX_HASH__\.js/g, routerAsset.filename) |
| 134 | + |
| 135 | + // Write compiled template |
| 136 | + const safeRouteName = route.pattern === '/' ? 'index' : route.pattern.slice(1).replace(/\//g, '-').replace(/[[\]]/g, '_') |
| 137 | + const compiledPath = path.join('server', 'pages', `${safeRouteName}.compiled.json`) |
| 138 | + const fragmentPath = path.join('server', 'fragments', `${safeRouteName}.html`) |
| 139 | + |
| 140 | + fs.writeFileSync( |
| 141 | + path.join(outputDir, compiledPath), |
| 142 | + JSON.stringify(compiled, null, 2), |
| 143 | + ) |
| 144 | + fs.writeFileSync( |
| 145 | + path.join(outputDir, fragmentPath), |
| 146 | + compiled.fragment, |
| 147 | + ) |
| 148 | + |
| 149 | + manifestRoutes.push({ |
| 150 | + pattern: route.pattern, |
| 151 | + compiledPath, |
| 152 | + fragmentPath, |
| 153 | + isDynamic: compiled.hasServerScripts, |
| 154 | + hasParams: route.pattern.includes(':') || route.pattern.includes('['), |
| 155 | + }) |
| 156 | + |
| 157 | + console.log(`[stx build] ✓ ${route.pattern}`) |
| 158 | + } |
| 159 | + catch (error) { |
| 160 | + console.error(`[stx build] ✗ ${route.pattern}:`, error instanceof Error ? error.message : error) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // ── 5. Generate manifest ── |
| 165 | + const assets: ManifestAssets = { |
| 166 | + runtime: runtimeAsset.filename, |
| 167 | + router: routerAsset.filename, |
| 168 | + } |
| 169 | + |
| 170 | + const manifest = generateManifest(manifestRoutes, assets, outputDir) |
| 171 | + writeManifest(manifest, outputDir) |
| 172 | + |
| 173 | + const duration = Date.now() - startTime |
| 174 | + console.log(`\n[stx build] Done in ${duration}ms — ${compiledPages.length} pages compiled`) |
| 175 | + |
| 176 | + return { |
| 177 | + pageCount: compiledPages.length, |
| 178 | + assets, |
| 179 | + outputDir, |
| 180 | + duration, |
| 181 | + } |
| 182 | +} |
0 commit comments