@uniweb/build 1.20.3 → 1.20.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/build",
3
- "version": "1.20.3",
3
+ "version": "1.20.4",
4
4
  "description": "Build tooling for the Uniweb Component Web Platform",
5
5
  "type": "module",
6
6
  "exports": {
@@ -58,14 +58,14 @@
58
58
  "sharp": "^0.35.5",
59
59
  "yaml": "^2.5.0",
60
60
  "@uniweb/content-reader": "~1.5.9",
61
- "@uniweb/schemas": "~1.7.0",
62
61
  "@uniweb/theming": "~1.3.2",
62
+ "@uniweb/projections": "~1.2.14",
63
+ "@uniweb/schemas": "~1.7.0",
63
64
  "@uniweb/content-writer": "~1.3.1",
64
- "@uniweb/semantic-parser": "~1.13.2",
65
- "@uniweb/projections": "~1.2.13"
65
+ "@uniweb/semantic-parser": "~1.13.2"
66
66
  },
67
67
  "optionalDependencies": {
68
- "@uniweb/runtime": "~1.5.4"
68
+ "@uniweb/runtime": "~1.5.5"
69
69
  },
70
70
  "peerDependencies": {
71
71
  "vite": "^5.0.0 || ^6.0.0 || ^7.0.0",
@@ -74,8 +74,8 @@
74
74
  "@tailwindcss/vite": "^4.0.0",
75
75
  "@vitejs/plugin-react": "^4.0.0 || ^5.0.0",
76
76
  "vite-plugin-svgr": "^4.0.0",
77
- "@uniweb/core": "~1.6.9",
78
- "@uniweb/runtime": "~1.5.4"
77
+ "@uniweb/core": "~1.6.10",
78
+ "@uniweb/runtime": "~1.5.5"
79
79
  },
80
80
  "peerDependenciesMeta": {
81
81
  "vite": {
package/src/prerender.js CHANGED
@@ -36,6 +36,8 @@ import { detectCiContext } from './hosts/detect-ci-context.js'
36
36
  import { stripBasePath } from './site/extension-urls.js'
37
37
  import { isSiteRelative, resolveExtensionDist } from './site/emit-extensions.js'
38
38
  import { crawlerFiles, sitemapEntriesOf } from './site/seo-files.js'
39
+ import { agentFiles } from './site/projections.js'
40
+ import { recordProjections, compiledData } from './site/record-projections.js'
39
41
  import { absoluteUrl } from '@uniweb/core/site-address'
40
42
 
41
43
  /**
@@ -1132,6 +1134,8 @@ export async function prerenderSite(siteDir, options = {}) {
1132
1134
 
1133
1135
  // The route of each page written, as its head states it (`Page#getNavRoute`).
1134
1136
  const written = new Set()
1137
+ // By a page's route, the records each of its sections showed: what its markdown carries.
1138
+ const shownRecords = new Map()
1135
1139
  for (const page of website.pages) {
1136
1140
  // Skip dynamic template pages — they exist in the content for runtime
1137
1141
  // route matching but can't be pre-rendered (no concrete route)
@@ -1247,7 +1251,10 @@ export async function prerenderSite(siteDir, options = {}) {
1247
1251
  // is how the two lanes drift. This lane passes the Page it already holds; the
1248
1252
  // isolate passes a route. See `runtime/src/page-renderer.js` for what it leaves
1249
1253
  // to the host.
1250
- const result = renderer.render(page, { fetchedData: carried })
1254
+ // ⭐ `sectionData`: and which records each section showed, which the page's markdown carries
1255
+ // after it (`writePageMarkdown`, below). An older runtime returns none, and the markdown stays
1256
+ // as the content alone makes it.
1257
+ const result = renderer.render(page, { fetchedData: carried, sectionData: true })
1251
1258
 
1252
1259
  if (result.outcome === 'failed') {
1253
1260
  const { type, message } = result.error
@@ -1298,6 +1305,25 @@ export async function prerenderSite(siteDir, options = {}) {
1298
1305
  onProgress(` → ${outputPath.replace(distDir, 'dist')}`)
1299
1306
  // The page whose head was written — a content-less folder renders its index child.
1300
1307
  written.add((result.page || page).getNavRoute())
1308
+ if (Array.isArray(result.sections) && result.sections.length > 0) {
1309
+ shownRecords.set((result.page || page).route, result.sections)
1310
+ }
1311
+ }
1312
+
1313
+ // ⭐ THE PAGES' MARKDOWN, WITH THE RECORDS THEIR SECTIONS SHOWED — only a page's render knows
1314
+ // those, so a page whose sections showed any has its markdown written again, now, by the one
1315
+ // writer every language's projections are made by (`./site/projections.js`).
1316
+ if (shownRecords.size > 0) {
1317
+ const count = await writePageMarkdown({
1318
+ content: { ...siteContent, pages: authoredPages },
1319
+ shownRecords,
1320
+ distDir,
1321
+ dataRoots: isDefault ? [distDir, join(siteDir, 'public')] : [join(distDir, locale)],
1322
+ locale,
1323
+ })
1324
+ if (count > 0) {
1325
+ onProgress(` Page markdown: ${count} page${count === 1 ? '' : 's'} now carry the records their sections show`)
1326
+ }
1301
1327
  }
1302
1328
 
1303
1329
  sitemapEntries.push(...sitemapEntriesOf({ ...siteContent, pages: authoredPages }, {
@@ -1415,6 +1441,41 @@ export async function prerenderSite(siteDir, options = {}) {
1415
1441
  }
1416
1442
  }
1417
1443
 
1444
+ /**
1445
+ * Write again the markdown of each page whose sections showed records, carrying them (`agentFiles`,
1446
+ * fed what each page's render read). A record page's markdown is its record's as before, then its
1447
+ * sections' records (`recordProjections`). Every other page's markdown is left as the bundle step
1448
+ * wrote it, which is what this would write too.
1449
+ *
1450
+ * @param {Object} where
1451
+ * @param {Object} where.content - one language's site content, its pages as authored
1452
+ * @param {Map<string, Array<{ id: string, data: Object }>>} where.shownRecords - by page route
1453
+ * @param {string} where.distDir
1454
+ * @param {string[]} where.dataRoots - where the language's compiled data files are, searched in order
1455
+ * @param {string} where.locale
1456
+ * @returns {Promise<number>} the pages written
1457
+ */
1458
+ async function writePageMarkdown({ content, shownRecords, distDir, dataRoots, locale }) {
1459
+ const readers = dataRoots.map((root) => compiledData(root))
1460
+ const read = (path) => {
1461
+ for (const reader of readers) {
1462
+ const found = reader(path)
1463
+ if (found !== null && found !== undefined) return found
1464
+ }
1465
+ return null
1466
+ }
1467
+ const records = await recordProjections(content, { read, locale, sections: shownRecords })
1468
+ let count = 0
1469
+ for (const file of agentFiles(content, records, { sections: shownRecords })) {
1470
+ if (file.kind !== 'markdown' || !shownRecords.has(file.route)) continue
1471
+ const target = join(distDir, file.fileName)
1472
+ await mkdir(dirname(target), { recursive: true })
1473
+ await writeFile(target, file.source)
1474
+ count++
1475
+ }
1476
+ return count
1477
+ }
1478
+
1418
1479
  const BROWSER_ONLY = /\b(window|document|navigator|localStorage|sessionStorage) is not defined/
1419
1480
 
1420
1481
  /**
@@ -92,11 +92,19 @@ export function searchFiles(content, records) {
92
92
  * then, and `uniweb doctor` warns. An agent that arrived through the index can follow a relative
93
93
  * link, and a silently absent index is the failure the whole feature exists to prevent.
94
94
  *
95
+ * ⭐ A page's markdown carries the records its sections showed where the caller hands them in
96
+ * (`sections`): what a page's render read, which a build has only once it prerenders
97
+ * (`../prerender.js`). Without them, each page reads as its content alone.
98
+ *
95
99
  * @param {Object} content - one language's site content
96
100
  * @param {Object} [records] - its record pages (`recordProjections`), listed and written as pages
97
- * @returns {Array<{ kind: 'index'|'branch'|'markdown', fileName: string, source: string }>}
101
+ * @param {Object} [options]
102
+ * @param {Map<string, Array<{ id: string, data: Object }>>} [options.sections] - by a page's route,
103
+ * the records each of its sections showed (`render(…, { sectionData: true })`)
104
+ * @returns {Array<{ kind: 'index'|'branch'|'markdown', fileName: string, source: string, route?: string }>}
105
+ * a markdown file says the route it is for
98
106
  */
99
- export function agentFiles(content, records) {
107
+ export function agentFiles(content, records, { sections = null } = {}) {
100
108
  const agents = resolveAgentsConfig(content?.config)
101
109
  if (!agents.index && !agents.markdown) return []
102
110
  if (!content?.pages?.length) return []
@@ -129,15 +137,15 @@ export function agentFiles(content, records) {
129
137
 
130
138
  if (agents.markdown) {
131
139
  for (const page of selectIndexablePages(content.pages, { exclude: agents.exclude })) {
132
- const markdown = renderPageMarkdown(page)
133
- if (markdown) files.push({ kind: 'markdown', fileName: `${prefix}${pageMarkdownFilename(at(page.route))}`, source: markdown })
140
+ const markdown = renderPageMarkdown(page, { sections: sections?.get(page.route) })
141
+ if (markdown) files.push({ kind: 'markdown', fileName: `${prefix}${pageMarkdownFilename(at(page.route))}`, source: markdown, route: page.route })
134
142
  }
135
143
  // Each record's page, held to the exclusions a page is held to — the site's pages among them,
136
144
  // so a record under a branch `llms.txt` leaves out gets no file, as it gets no line there.
137
145
  const listed = indexableRecordRoutes(content.pages, records?.pages, agents.exclude)
138
146
  for (const { route, markdown } of records?.pages || []) {
139
147
  if (!listed.has(route)) continue
140
- files.push({ kind: 'markdown', fileName: `${prefix}${pageMarkdownFilename(routeOnDisk(at(route)))}`, source: markdown })
148
+ files.push({ kind: 'markdown', fileName: `${prefix}${pageMarkdownFilename(routeOnDisk(at(route)))}`, source: markdown, route })
141
149
  }
142
150
  }
143
151
 
@@ -171,7 +179,9 @@ export function describeProjections(files) {
171
179
  lines.push(`Generated ${branches.length} branch index(es): ${branches.map((b) => `${b.route} (${b.count})`).join(', ')}`)
172
180
  }
173
181
  const markdown = files.filter((file) => file.kind === 'markdown').length
174
- if (markdown) lines.push(`Generated ${markdown} page markdown projections`)
182
+ // ⭐ Never silent that the records a page's sections show are not in it yet: only a page's render
183
+ // knows them, and a build has one only once it prerenders, which then rewrites those pages.
184
+ if (markdown) lines.push(`Generated ${markdown} page markdown projections (the records their sections show are added by prerendering)`)
175
185
  return lines
176
186
  }
177
187
 
@@ -66,11 +66,14 @@ export function compiledData(root) {
66
66
  * one too (`compiledData`)
67
67
  * @param {string} [options.locale] - the content's language, `config.activeLocale` by default, else
68
68
  * the site's default
69
+ * @param {Map<string, Array<{ id: string, data: Object }>>} [options.sections] - by a record page's
70
+ * route, the records its sections showed in its render, which its markdown carries
71
+ * (`renderPageMarkdown`'s `sections`)
69
72
  * @returns {Promise<{ indexes: Object[], entries: Object[], pages: Array<{ route: string, markdown: string }> }>}
70
73
  */
71
74
  export async function recordProjections(
72
75
  content,
73
- { read, locale = content?.config?.activeLocale || resolveDefaultLocale(content?.config || {}) } = {}
76
+ { read, locale = content?.config?.activeLocale || resolveDefaultLocale(content?.config || {}), sections = null } = {}
74
77
  ) {
75
78
  const queries = content?.config?.queries || {}
76
79
  const indexes = []
@@ -92,7 +95,7 @@ export async function recordProjections(
92
95
  if (!route) continue
93
96
  const handle = recordHandle(brief)
94
97
  const record = (handle && (await read(`${wholeDir}/${handle}.json`))) || brief
95
- const markdown = renderPageMarkdown(template, { record })
98
+ const markdown = renderPageMarkdown(template, { record, sections: sections?.get(route) })
96
99
  if (markdown) pages.push({ route, markdown })
97
100
  }
98
101
  }