Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /** * Static Runtime - For static site generation * Pre-renders components to static HTML files */ import { renderToString } from '@coherentjs/core'; export class StaticRuntime { constructor(options = {}) { this.options = { outputDir: 'dist', baseUrl: '', generateSitemap: true, generateManifest: true, minifyHtml: true, inlineCSS: false, ...options }; this.componentRegistry = new Map(); this.pageRegistry = new Map(); this.staticAssets = new Map(); this.generatedPages = new Map(); // Track build statistics this.buildStats = { startTime: Date.now(), pagesGenerated: 0, assetsProcessed: 0, totalSize: 0, errors: [] }; } // Component management registerComponent(name, component, options = {}) { this.componentRegistry.set(name, { component, options }); return component; } getComponent(name) { const registration = this.componentRegistry.get(name); return registration ? registration.component : null; } // Page registration addPage(route, component, options = {}) { this.pageRegistry.set(route, { component: typeof component === 'string' ? this.getComponent(component) : component, options: { title: options.title || 'Page', description: options.description || '', keywords: options.keywords || '', generatePath: options.generatePath || this.routeToPath(route), props: options.props || {}, ...options } }); } routeToPath(route) { // Convert route pattern to static path // /users/:id -> /users/[id] (or similar static pattern) if (route === '/') return '/index.html'; // Handle dynamic routes if (route.includes(':')) { // For static generation, dynamic routes need explicit paths console.warn(`Dynamic route ${route} requires explicit generatePath`); return null; } return route.endsWith('/') ? `${route}index.html` : `${route}.html`; } // Static asset management addAsset(path, content, options = {}) { this.staticAssets.set(path, { content, type: options.type || this.inferContentType(path), minify: options.minify !== false, ...options }); } inferContentType(path) { const ext = path.split('.').pop().toLowerCase(); const contentTypes = { 'html': 'text/html', 'css': 'text/css', 'js': 'application/javascript', 'json': 'application/json', 'png': 'image/png', 'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'gif': 'image/gif', 'svg': 'image/svg+xml', 'ico': 'image/x-icon' }; return contentTypes[ext] || 'text/plain'; } // HTML template generation generateHtmlDocument(content, options = {}) { const { title = 'Coherent.js App', description = '', keywords = '', lang = 'en', charset = 'utf-8', viewport = 'width=device-width, initial-scale=1', favicon = '/favicon.ico', styles = [], scripts = [], meta = [], bodyClass = '', hydrate = false, componentName = null, componentProps = {} } = options; const metaTags = [ `<meta charset="${charset}">`, `<meta name="viewport" content="${viewport}">`, description && `<meta name="description" content="${description}">`, keywords && `<meta name="keywords" content="${keywords}">`, ...meta.map(m => typeof m === 'string' ? m : `<meta ${Object.entries(m).map(([k, v]) => `${k}="${v}"`).join(' ')}>`) ].filter(Boolean).join('\n '); const styleTags = styles.map(style => typeof style === 'string' ? style.startsWith('<') ? style : `<link rel="stylesheet" href="${style}">` : `<style>${style.content}</style>` ).join('\n '); const scriptTags = [ ...scripts.map(script => typeof script === 'string' ? script.startsWith('<') ? script : `<script src="${script}"></script>` : `<script>${script.content}</script>` ), // Add hydration script if enabled hydrate && this.generateHydrationScript(componentName, componentProps) ].filter(Boolean).join('\n '); return `<!DOCTYPE html> <html lang="${lang}"> <head> ${metaTags} <title>${this.escapeHtml(title)}</title> <link rel="icon" href="${favicon}"> ${styleTags} </head> <body${bodyClass ? ` class="${bodyClass}"` : ''}> ${content} ${scriptTags} </body> </html>`; } generateHydrationScript(componentName) { if (!componentName) return ''; return ` <script type="module"> import { autoHydrate } from '/coherent-client.js'; // Auto-hydrate on page load document.addEventListener('DOMContentLoaded', () => { autoHydrate({ '${componentName}': window.components?.['${componentName}'] }); }); </script>`; } escapeHtml(text) { if (typeof text !== 'string') return text; return text .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, '''); } // Build process async build() { this.buildStats.startTime = Date.now(); console.log('🏗️ Starting static site generation...'); try { // Generate all pages await this.generatePages(); // Process static assets await this.processAssets(); // Generate additional files if (this.options.generateSitemap) { await this.generateSitemap(); } if (this.options.generateManifest) { await this.generateManifest(); } // Generate build report const buildTime = Date.now() - this.buildStats.startTime; console.log(`✅ Build completed in ${buildTime}ms`); console.log(`📄 Generated ${this.buildStats.pagesGenerated} pages`); console.log(`📦 Processed ${this.buildStats.assetsProcessed} assets`); return this.getBuildResult(); } catch (error) { this.buildStats.errors.push(error); console.error('❌ Build failed:', error); throw error; } } async generatePages() { for (const [route, pageConfig] of this.pageRegistry) { try { await this.generatePage(route, pageConfig); this.buildStats.pagesGenerated++; } catch (error) { console.error(`Failed to generate page ${route}:`, error); this.buildStats.errors.push({ route, error }); } } } async generatePage(route, pageConfig) { const { component, options } = pageConfig; if (!component) { throw new Error(`No component found for route: ${route}`); } // Render component to HTML const vdom = component(options.props || {}); const content = renderToString(vdom); // Wrap in HTML document const html = this.generateHtmlDocument(content, { title: options.title, description: options.description, keywords: options.keywords, styles: options.styles || [], scripts: options.scripts || [], meta: options.meta || [], hydrate: options.hydrate, componentName: options.componentName, componentProps: options.props || {}, ...options.htmlOptions }); // Minify if enabled const finalHtml = this.options.minifyHtml ? this.minifyHtml(html) : html; // Store generated page const outputPath = options.generatePath || this.routeToPath(route); if (outputPath) { this.generatedPages.set(outputPath, { html: finalHtml, size: finalHtml.length, route, options }); this.buildStats.totalSize += finalHtml.length; } } minifyHtml(html) { // Basic HTML minification return html .replace(/\s+/g, ' ') .replace(/>\s+</g, '><') .replace(/\s+>/g, '>') .replace(/<\s+/g, '<') .trim(); } async processAssets() { for (const [path, asset] of this.staticAssets) { try { let content = asset.content; // Process asset based on type if (asset.minify) { content = this.minifyAsset(content, asset.type); } // Store processed asset this.generatedPages.set(path, { content, type: asset.type, size: content.length }); this.buildStats.assetsProcessed++; this.buildStats.totalSize += content.length; } catch (error) { console.error(`Failed to process asset ${path}:`, error); this.buildStats.errors.push({ path, error }); } } } minifyAsset(content, type) { switch (type) { case 'text/css': return content .replace(/\/\*[\s\S]*?\*\//g, '') .replace(/\s+/g, ' ') .replace(/;\s*}/g, '}') .replace(/\s*{\s*/g, '{') .replace(/;\s*/g, ';') .trim(); case 'application/javascript': // Basic JS minification (in production, use a proper minifier) return content .replace(/\/\*[\s\S]*?\*\//g, '') .replace(/\/\/.*$/gm, '') .replace(/\s+/g, ' ') .replace(/\s*([{}();,])\s*/g, '$1') .trim(); default: return content; } } async generateSitemap() { const pages = Array.from(this.generatedPages.keys()) .filter(path => path.endsWith('.html')); const sitemap = `<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> ${pages.map(path => ` <url> <loc>${this.options.baseUrl}${path.replace('/index.html', '/')}</loc> <lastmod>${new Date().toISOString().split('T')[0]}</lastmod> </url>`).join('\n')} </urlset>`; this.generatedPages.set('/sitemap.xml', { content: sitemap, type: 'application/xml', size: sitemap.length }); } async generateManifest() { const manifest = { name: this.options.name || 'Coherent.js App', short_name: this.options.shortName || 'App', description: this.options.description || '', start_url: '/', display: 'standalone', theme_color: this.options.themeColor || '#000000', background_color: this.options.backgroundColor || '#ffffff', icons: this.options.icons || [] }; const manifestJson = JSON.stringify(manifest, null, 2); this.generatedPages.set('/manifest.json', { content: manifestJson, type: 'application/json', size: manifestJson.length }); } getBuildResult() { return { pages: this.generatedPages, stats: { ...this.buildStats, buildTime: Date.now() - this.buildStats.startTime }, success: this.buildStats.errors.length === 0 }; } // Create static app factory createApp() { return { // Component registration component: (name, component, opts) => this.registerComponent(name, component, opts), // Page registration page: (route, component, opts) => this.addPage(route, component, opts), // Asset management asset: (path, content, opts) => this.addAsset(path, content, opts), // Build process build: () => this.build(), // Utilities getRuntime: () => this, getStats: () => this.buildStats, getPages: () => Array.from(this.pageRegistry.keys()), getAssets: () => Array.from(this.staticAssets.keys()) }; } // Static factory methods static createApp(options = {}) { const runtime = new StaticRuntime(options); return runtime.createApp(options); } static async buildSite(pages = {}, components = {}, options = {}) { const runtime = new StaticRuntime(options); // Register components Object.entries(components).forEach(([name, component]) => { runtime.registerComponent(name, component); }); // Register pages Object.entries(pages).forEach(([route, config]) => { runtime.addPage(route, config.component, config.options); }); return await runtime.build(); } } |