| ... | @@ -1,11 +1,16 @@ | ... | @@ -1,11 +1,16 @@ |
| | 1 | // This file contains the main site generator build process. |
| | 2 | // By using `Incremental`'s ability to automatically purge stale |
| | 3 | // assets, the `sitegen` function performs partial rebuilds. |
| | 4 | |
| 1 | export function main(incremental?: Incremental) { | 5 | export function main(incremental?: Incremental) { |
| 2 | return withSpinner<Record<string, unknown>, any>({ | 6 | return withSpinner<Record<string, unknown>, any>({ |
| 3 | text: "Recovering State", | 7 | text: "Recovering State", |
| 4 | successText, | 8 | successText, |
| 5 | failureText: () => "sitegen FAIL", | 9 | failureText: () => "sitegen FAIL", |
| 6 | }, async (spinner) => { | 10 | }, async (spinner) => { |
| 7 | const incr = Incremental.fromDisk(); | 11 | // const incr = Incremental.fromDisk(); |
| 8 | await incr.statAllFiles(); | 12 | // await incr.statAllFiles(); |
| | 13 | const incr = new Incremental(); |
| 9 | const result = await sitegen(spinner, incr); | 14 | const result = await sitegen(spinner, incr); |
| 10 | incr.toDisk(); // Allows picking up this state again | 15 | incr.toDisk(); // Allows picking up this state again |
| 11 | return result; | 16 | return result; |
| ... | @@ -94,26 +99,42 @@ export async function sitegen( | ... | @@ -94,26 +99,42 @@ export async function sitegen( |
| 94 | scripts = scripts.filter(({ file }) => !file.match(/\.client\.[tj]sx?/)); | 99 | scripts = scripts.filter(({ file }) => !file.match(/\.client\.[tj]sx?/)); |
| 95 | const globalCssPath = join("global.css"); | 100 | const globalCssPath = join("global.css"); |
| 96 | | 101 | |
| 97 | // TODO: invalidate incremental resources | 102 | // TODO: make sure that `static` and `pages` does not overlap |
| 98 | | 103 | |
| 99 | // -- server side render -- | 104 | // -- inline style sheets, used and shared by pages and views -- |
| 100 | status.text = "Building"; | 105 | status.text = "Building"; |
| 101 | const cssOnce = new OnceMap<css.Output>(); | 106 | const cssOnce = new OnceMap(); |
| 102 | const cssQueue = new Queue<[string, string[], css.Theme], css.Output>({ | 107 | const cssQueue = new Queue({ |
| 103 | name: "Bundle", | 108 | name: "Bundle", |
| 104 | fn: ([, files, theme]) => css.bundleCssFiles(files, theme), | 109 | async fn([, key, files, theme]: [string, string, string[], css.Theme]) { |
| | 110 | const { text, sources } = await css.bundleCssFiles(files, theme); |
| | 111 | incr.put({ |
| | 112 | kind: "style", |
| | 113 | key, |
| | 114 | sources, |
| | 115 | value: text, |
| | 116 | }); |
| | 117 | }, |
| 105 | passive: true, | 118 | passive: true, |
| 106 | getItemText: ([id]) => id, | 119 | getItemText: ([id]) => id, |
| 107 | maxJobs: 2, | 120 | maxJobs: 2, |
| 108 | }); | 121 | }); |
| 109 | interface RenderResult { | 122 | function ensureCssGetsBuilt( |
| 110 | body: string; | 123 | cssImports: string[], |
| 111 | head: string; | 124 | theme: css.Theme, |
| 112 | css: css.Output; | 125 | referrer: string, |
| 113 | clientRefs: string[]; | 126 | ) { |
| 114 | item: FileItem; | 127 | const key = css.styleKey(cssImports, theme); |
| | 128 | cssOnce.get( |
| | 129 | key, |
| | 130 | async () => { |
| | 131 | incr.getArtifact("style", key) ?? |
| | 132 | await cssQueue.add([referrer, key, cssImports, theme]); |
| | 133 | }, |
| | 134 | ); |
| 115 | } | 135 | } |
| 116 | const renderResults: RenderResult[] = []; | 136 | |
| | 137 | // -- server side render pages -- |
| 117 | async function loadPageModule({ file }: FileItem) { | 138 | async function loadPageModule({ file }: FileItem) { |
| 118 | require(file); | 139 | require(file); |
| 119 | } | 140 | } |
| ... | @@ -125,28 +146,27 @@ export async function sitegen( | ... | @@ -125,28 +146,27 @@ export async function sitegen( |
| 125 | theme: pageTheme, | 146 | theme: pageTheme, |
| 126 | layout, | 147 | layout, |
| 127 | } = require(item.file); | 148 | } = require(item.file); |
| 128 | if (!Page) throw new Error("Page is missing a 'default' export."); | 149 | if (!Page) { |
| | 150 | throw new Error("Page is missing a 'default' export."); |
| | 151 | } |
| 129 | if (!metadata) { | 152 | if (!metadata) { |
| 130 | throw new Error("Page is missing 'meta' export with a title."); | 153 | throw new Error("Page is missing 'meta' export with a title."); |
| 131 | } | 154 | } |
| | 155 | |
| | 156 | // -- css -- |
| 132 | if (layout?.theme) pageTheme = layout.theme; | 157 | if (layout?.theme) pageTheme = layout.theme; |
| 133 | const theme = { | 158 | const theme: css.Theme = { |
| 134 | bg: "#fff", | 159 | ...css.defaultTheme, |
| 135 | fg: "#050505", | | |
| 136 | primary: "#2e7dab", | | |
| 137 | ...pageTheme, | 160 | ...pageTheme, |
| 138 | }; | 161 | }; |
| | 162 | const cssImports = [globalCssPath, ...hot.getCssImports(item.file)]; |
| | 163 | ensureCssGetsBuilt(cssImports, theme, item.id); |
| 139 | | 164 | |
| 140 | // -- metadata -- | 165 | // -- metadata -- |
| 141 | const renderedMetaPromise = Promise.resolve( | 166 | const renderedMetaPromise = Promise.resolve( |
| 142 | typeof metadata === "function" ? metadata({ ssr: true }) : metadata, | 167 | typeof metadata === "function" ? metadata({ ssr: true }) : metadata, |
| 143 | ).then((m) => meta.renderMeta(m)); | 168 | ).then((m) => meta.renderMeta(m)); |
| 144 | // -- css -- | 169 | |
| 145 | const cssImports = [globalCssPath, ...hot.getCssImports(item.file)]; | | |
| 146 | const cssPromise = cssOnce.get( | | |
| 147 | cssImports.join(":") + JSON.stringify(theme), | | |
| 148 | () => cssQueue.add([item.id, cssImports, theme]), | | |
| 149 | ); | | |
| 150 | // -- html -- | 170 | // -- html -- |
| 151 | let page = [engine.kElement, Page, {}]; | 171 | let page = [engine.kElement, Page, {}]; |
| 152 | if (layout?.default) { | 172 | if (layout?.default) { |
| ... | @@ -156,9 +176,8 @@ export async function sitegen( | ... | @@ -156,9 +176,8 @@ export async function sitegen( |
| 156 | sitegen: sg.initRender(), | 176 | sitegen: sg.initRender(), |
| 157 | }); | 177 | }); |
| 158 | | 178 | |
| 159 | const [{ text, addon }, cssBundle, renderedMeta] = await Promise.all([ | 179 | const [{ text, addon }, renderedMeta] = await Promise.all([ |
| 160 | bodyPromise, | 180 | bodyPromise, |
| 161 | cssPromise, | | |
| 162 | renderedMetaPromise, | 181 | renderedMetaPromise, |
| 163 | ]); | 182 | ]); |
| 164 | if (!renderedMeta.includes("<title>")) { | 183 | if (!renderedMeta.includes("<title>")) { |
| ... | @@ -167,20 +186,72 @@ export async function sitegen( | ... | @@ -167,20 +186,72 @@ export async function sitegen( |
| 167 | "All pages need a title tag.", | 186 | "All pages need a title tag.", |
| 168 | ); | 187 | ); |
| 169 | } | 188 | } |
| 170 | // The script content is not ready, allow another page to Render. The page | 189 | incr.put({ |
| 171 | // contents will be rebuilt at the end. This is more convenient anyways | 190 | kind: "pageMetadata", |
| 172 | // because it means client scripts don't re-render the page. | 191 | key: item.id, |
| 173 | renderResults.push({ | 192 | // Incremental integrates with `hot.ts` + `require` |
| 174 | body: text, | 193 | // to trace all the needed source files here. |
| 175 | head: renderedMeta, | 194 | sources: [item.file], |
| 176 | css: cssBundle, | 195 | value: { |
| 177 | clientRefs: Array.from(addon.sitegen.scripts), | 196 | html: text, |
| 178 | item: item, | 197 | meta: renderedMeta, |
| | 198 | cssImports, |
| | 199 | theme: theme ?? null, |
| | 200 | clientRefs: Array.from(addon.sitegen.scripts), |
| | 201 | }, |
| | 202 | }); |
| | 203 | } |
| | 204 | async function prepareView(view: FileItem) { |
| | 205 | const module = require(view.file); |
| | 206 | if (!module.meta) { |
| | 207 | throw new Error(`${view.file} is missing 'export const meta'`); |
| | 208 | } |
| | 209 | if (!module.default) { |
| | 210 | throw new Error(`${view.file} is missing a default export.`); |
| | 211 | } |
| | 212 | const pageTheme = module.layout?.theme ?? module.theme; |
| | 213 | const theme: css.Theme = { |
| | 214 | ...css.defaultTheme, |
| | 215 | ...pageTheme, |
| | 216 | }; |
| | 217 | const cssImports = hot.getCssImports(view.file) |
| | 218 | .concat("src/global.css") |
| | 219 | .map((file) => path.relative(hot.projectRoot, path.resolve(file))); |
| | 220 | incr.put({ |
| | 221 | kind: "viewMetadata", |
| | 222 | key: view.id, |
| | 223 | sources: [view.file], |
| | 224 | value: { |
| | 225 | file: path.relative(hot.projectRoot, view.file), |
| | 226 | cssImports, |
| | 227 | theme, |
| | 228 | clientRefs: hot.getClientScriptRefs(view.file), |
| | 229 | hasLayout: !!module.layout?.default, |
| | 230 | }, |
| 179 | }); | 231 | }); |
| 180 | } | 232 | } |
| | 233 | |
| | 234 | // Of the pages that are already built, a call to 'ensureCssGetsBuilt' is |
| | 235 | // required so that it's (1) re-built if needed, (2) not pruned from build. |
| | 236 | const neededPages = pages.filter((page) => { |
| | 237 | const existing = incr.getArtifact("pageMetadata", page.id); |
| | 238 | if (existing) { |
| | 239 | const { cssImports, theme } = existing; |
| | 240 | ensureCssGetsBuilt(cssImports, theme, page.id); |
| | 241 | } |
| | 242 | return !existing; |
| | 243 | }); |
| | 244 | const neededViews = views.filter((view) => { |
| | 245 | const existing = incr.getArtifact("viewMetadata", view.id); |
| | 246 | if (existing) { |
| | 247 | const { cssImports, theme } = existing; |
| | 248 | ensureCssGetsBuilt(cssImports, theme, view.id); |
| | 249 | } |
| | 250 | return !existing; |
| | 251 | }); |
| | 252 | |
| 181 | // This is done in two passes so that a page that throws during evaluation | 253 | // This is done in two passes so that a page that throws during evaluation |
| 182 | // will report "Load Render Module" instead of "Render Static Page". | 254 | // will report "Load Render Module" instead of "Render Static Page". |
| 183 | const neededPages = pages.filter((page) => incr.needsBuild("asset", page.id)); | | |
| 184 | const spinnerFormat = status.format; | 255 | const spinnerFormat = status.format; |
| 185 | status.format = () => ""; | 256 | status.format = () => ""; |
| 186 | const moduleLoadQueue = new Queue({ | 257 | const moduleLoadQueue = new Queue({ |
| ... | @@ -190,6 +261,7 @@ export async function sitegen( | ... | @@ -190,6 +261,7 @@ export async function sitegen( |
| 190 | maxJobs: 1, | 261 | maxJobs: 1, |
| 191 | }); | 262 | }); |
| 192 | moduleLoadQueue.addMany(neededPages); | 263 | moduleLoadQueue.addMany(neededPages); |
| | 264 | moduleLoadQueue.addMany(neededViews); |
| 193 | await moduleLoadQueue.done({ method: "stop" }); | 265 | await moduleLoadQueue.done({ method: "stop" }); |
| 194 | const pageQueue = new Queue({ | 266 | const pageQueue = new Queue({ |
| 195 | name: "Render Static Page", | 267 | name: "Render Static Page", |
| ... | @@ -198,32 +270,52 @@ export async function sitegen( | ... | @@ -198,32 +270,52 @@ export async function sitegen( |
| 198 | maxJobs: 2, | 270 | maxJobs: 2, |
| 199 | }); | 271 | }); |
| 200 | pageQueue.addMany(neededPages); | 272 | pageQueue.addMany(neededPages); |
| | 273 | const viewQueue = new Queue({ |
| | 274 | name: "Build Dynamic View", |
| | 275 | fn: prepareView, |
| | 276 | getItemText, |
| | 277 | maxJobs: 2, |
| | 278 | }); |
| | 279 | viewQueue.addMany(neededViews); |
| 201 | await pageQueue.done({ method: "stop" }); | 280 | await pageQueue.done({ method: "stop" }); |
| | 281 | await viewQueue.done({ method: "stop" }); |
| 202 | status.format = spinnerFormat; | 282 | status.format = spinnerFormat; |
| 203 | | 283 | |
| 204 | // -- bundle backend and views -- | 284 | // -- bundle backend and views -- |
| 205 | status.text = "Bundle backend code"; | 285 | // status.text = "Bundle backend code"; |
| 206 | const backend = await bundle.bundleServerJavaScript( | 286 | // const backend = await bundle.bundleServerJavaScript( |
| 207 | join("backend.ts"), | 287 | // join("backend.ts"), |
| 208 | views, | 288 | // views, |
| 209 | ); | 289 | // ); |
| 210 | const viewCssPromise = await Promise.all( | 290 | // const viewCssPromise = await Promise.all( |
| 211 | backend.views.map((view) => | 291 | // backend.views.map((view) => |
| 212 | cssOnce.get( | 292 | // cssOnce.get( |
| 213 | view.cssImports.join(":") + JSON.stringify(view.theme), | 293 | // view.cssImports.join(":") + JSON.stringify(view.theme), |
| 214 | () => cssQueue.add([view.id, view.cssImports, view.theme ?? {}]), | 294 | // () => cssQueue.add([view.id, view.cssImports, view.theme ?? {}]), |
| 215 | ) | 295 | // ) |
| 216 | ), | 296 | // ), |
| 217 | ); | 297 | // ); |
| 218 | | 298 | |
| 219 | // -- bundle scripts -- | 299 | // -- bundle scripts -- |
| 220 | const referencedScripts = Array.from( | 300 | const referencedScripts = Array.from( |
| 221 | new Set([ | 301 | new Set( |
| 222 | ...renderResults.flatMap((r) => r.clientRefs), | 302 | [ |
| 223 | ...backend.views.flatMap((r) => r.clientRefs), | 303 | ...pages.map((item) => |
| 224 | ]), | 304 | UNWRAP( |
| | 305 | incr.getArtifact("pageMetadata", item.id), |
| | 306 | `Missing pageMetadata ${item.id}`, |
| | 307 | ) |
| | 308 | ), |
| | 309 | ...views.map((item) => |
| | 310 | UNWRAP( |
| | 311 | incr.getArtifact("viewMetadata", item.id), |
| | 312 | `Missing viewMetadata ${item.id}`, |
| | 313 | ) |
| | 314 | ), |
| | 315 | ].flatMap((item) => item.clientRefs), |
| | 316 | ), |
| 225 | (script) => path.resolve(hot.projectSrc, script), | 317 | (script) => path.resolve(hot.projectSrc, script), |
| 226 | ); | 318 | ).filter((file) => !incr.hasArtifact("script", hot.getScriptId(file))); |
| 227 | const extraPublicScripts = scripts.map((entry) => entry.file); | 319 | const extraPublicScripts = scripts.map((entry) => entry.file); |
| 228 | const uniqueCount = new Set([ | 320 | const uniqueCount = new Set([ |
| 229 | ...referencedScripts, | 321 | ...referencedScripts, |
| ... | @@ -237,7 +329,7 @@ export async function sitegen( | ... | @@ -237,7 +329,7 @@ export async function sitegen( |
| 237 | ); | 329 | ); |
| 238 | | 330 | |
| 239 | // -- finalize backend bundle -- | 331 | // -- finalize backend bundle -- |
| 240 | await bundle.finalizeServerJavaScript(backend, await viewCssPromise, incr); | 332 | // await bundle.finalizeServerJavaScript(backend, await viewCssPromise, incr); |
| 241 | | 333 | |
| 242 | // -- copy/compress static files -- | 334 | // -- copy/compress static files -- |
| 243 | async function doStaticFile(item: FileItem) { | 335 | async function doStaticFile(item: FileItem) { |
| ... | @@ -256,37 +348,51 @@ export async function sitegen( | ... | @@ -256,37 +348,51 @@ export async function sitegen( |
| 256 | }); | 348 | }); |
| 257 | status.format = () => ""; | 349 | status.format = () => ""; |
| 258 | staticQueue.addMany( | 350 | staticQueue.addMany( |
| 259 | staticFiles.filter((file) => incr.needsBuild("asset", file.id)), | 351 | staticFiles.filter((file) => !incr.hasArtifact("asset", file.id)), |
| 260 | ); | 352 | ); |
| 261 | await staticQueue.done({ method: "stop" }); | 353 | await staticQueue.done({ method: "stop" }); |
| 262 | status.format = spinnerFormat; | 354 | status.format = spinnerFormat; |
| 263 | | 355 | |
| | 356 | await cssQueue.done({ method: "stop" }); |
| | 357 | |
| 264 | // -- concatenate static rendered pages -- | 358 | // -- concatenate static rendered pages -- |
| 265 | status.text = `Concat ${renderResults.length} Pages`; | 359 | status.text = `Concat Pages`; |
| 266 | await Promise.all( | 360 | await Promise.all(pages.map(async (page) => { |
| 267 | renderResults.map( | 361 | if (incr.hasArtifact("asset", page.id)) return; |
| 268 | async ( | 362 | const { |
| 269 | { item: page, body, head, css, clientRefs: scriptFiles }, | 363 | html, |
| 270 | ) => { | 364 | meta, |
| 271 | const doc = wrapDocument({ | 365 | cssImports, |
| 272 | body, | 366 | theme, |
| 273 | head, | 367 | clientRefs, |
| 274 | inlineCss: css.text, | 368 | } = UNWRAP(incr.out.pageMetadata.get(page.id)); |
| 275 | scripts: scriptFiles.map( | 369 | const scriptIds = clientRefs.map(hot.getScriptId); |
| 276 | (file) => UNWRAP(incr.out.script.get(hot.getScriptId(file))), | 370 | const styleKey = css.styleKey(cssImports, theme); |
| 277 | ).map((x) => `{${x}}`).join("\n"), | 371 | const style = UNWRAP( |
| 278 | }); | 372 | incr.out.style.get(styleKey), |
| 279 | await incr.putAsset({ | 373 | `Missing style ${styleKey}`, |
| 280 | sources: [page.file, ...css.sources], | 374 | ); |
| 281 | key: page.id, | 375 | const doc = wrapDocument({ |
| 282 | body: doc, | 376 | body: html, |
| 283 | headers: { | 377 | head: meta, |
| 284 | "Content-Type": "text/html", | 378 | inlineCss: style, |
| 285 | }, | 379 | scripts: scriptIds.map( |
| 286 | }); | 380 | (ref) => UNWRAP(incr.out.script.get(ref), `Missing script ${ref}`), |
| | 381 | ).map((x) => `{${x}}`).join("\n"), |
| | 382 | }); |
| | 383 | await incr.putAsset({ |
| | 384 | sources: [ |
| | 385 | page.file, |
| | 386 | ...incr.sourcesFor("style", styleKey), |
| | 387 | ...scriptIds.flatMap((ref) => incr.sourcesFor("script", ref)), |
| | 388 | ], |
| | 389 | key: page.id, |
| | 390 | body: doc, |
| | 391 | headers: { |
| | 392 | "Content-Type": "text/html", |
| 287 | }, | 393 | }, |
| 288 | ), | 394 | }); |
| 289 | ); | 395 | })); |
| 290 | status.format = () => ""; | 396 | status.format = () => ""; |
| 291 | status.text = ``; | 397 | status.text = ``; |
| 292 | // This will wait for all compression jobs to finish, which up | 398 | // This will wait for all compression jobs to finish, which up |