@uniweb/build 1.10.14 → 1.11.1

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.
@@ -149,13 +149,23 @@ function walkDataBlockAssets(doc, visitor) {
149
149
  }
150
150
  }
151
151
 
152
+ /** The code `resolveAssetPath` refuses a `/x` with when only the retired `assets/` folder holds it. */
153
+ export const ASSETS_FOLDER_RETIRED = 'ASSETS_FOLDER_RETIRED'
154
+
152
155
  /**
153
156
  * Resolve an asset path to absolute file system path
154
157
  *
158
+ * ⛔ `/x` NAMES `public/x`, AND ONLY THAT. Until 2026-10-28 `assets/` was a second home for it,
159
+ * searched after `public/`; it is retired [Diego: "We don't need too many ways of doing the same
160
+ * thing"]. A `/x` only `assets/` holds is refused by name, never read as missing, and the move it
161
+ * names is into `public/`, where the address the content names stays the same. A file beside the
162
+ * page that shows it serves as well, on every host (`uwx/located-media.js`).
163
+ *
155
164
  * @param {string} src - Original source path from content
156
165
  * @param {string} contextPath - Path of the file containing the reference
157
166
  * @param {string} siteRoot - Site root directory
158
167
  * @returns {Object} Resolution result
168
+ * @throws {Error} `code: ASSETS_FOLDER_RETIRED` — a `/x` that only `assets/` holds
159
169
  */
160
170
  export function resolveAssetPath(src, contextPath, siteRoot) {
161
171
  // External URLs - don't process
@@ -176,19 +186,11 @@ export function resolveAssetPath(src, contextPath, siteRoot) {
176
186
  const contextDir = dirname(contextPath)
177
187
  resolved = normalize(join(contextDir, src))
178
188
  }
179
- // Absolute site paths: /images/hero.png
189
+ // Absolute site paths: /images/hero.png — a file in public/, whether or not it exists yet
180
190
  else if (src.startsWith('/')) {
181
- // Check public folder first, then assets folder
182
- const publicPath = join(siteRoot, 'public', src)
183
- const assetsPath = join(siteRoot, 'assets', src)
184
-
185
- if (existsSync(publicPath)) {
186
- resolved = publicPath
187
- } else if (existsSync(assetsPath)) {
188
- resolved = assetsPath
189
- } else {
190
- // Default to public folder path even if it doesn't exist yet
191
- resolved = publicPath
191
+ resolved = join(siteRoot, 'public', src)
192
+ if (!existsSync(resolved) && existsSync(join(siteRoot, 'assets', src))) {
193
+ throw assetsFolderRetired(src, contextPath, siteRoot)
192
194
  }
193
195
  }
194
196
 
@@ -202,6 +204,22 @@ export function resolveAssetPath(src, contextPath, siteRoot) {
202
204
  }
203
205
  }
204
206
 
207
+ /**
208
+ * The refusal of a `/x` that only `assets/` holds, naming the file and where it goes — and the
209
+ * file that wrote it, when there is one (a site's config is read through a stand-in path).
210
+ */
211
+ function assetsFolderRetired(src, contextPath, siteRoot) {
212
+ const where = contextPath ? relative(siteRoot, contextPath).split(sep).join('/') : ''
213
+ const named = where && !where.startsWith('..') && existsSync(contextPath) ? ` (in ${where})` : ''
214
+ const file = src.replace(/^\/+/, '')
215
+ const err = new Error(
216
+ `${src}${named} names assets/${file}, and a site no longer keeps media in assets/. ` +
217
+ `Move the file to public/${file}: the address stays ${src}.`
218
+ )
219
+ err.code = ASSETS_FOLDER_RETIRED
220
+ return err
221
+ }
222
+
205
223
  /**
206
224
  * Walk a ProseMirror document and collect all asset references
207
225
  *
@@ -253,7 +271,6 @@ export function walkContentAssets(doc, visitor, path = '') {
253
271
  * @returns {Object} Asset collection result
254
272
  * - assets: Asset manifest mapping original paths to resolved info
255
273
  * - hasExplicitPoster: Set of video src paths that have explicit poster attributes
256
- * - hasExplicitPreview: Set of PDF src paths that have explicit preview attributes
257
274
  * - uses: every co-located reference, with the file it resolved to and a setter
258
275
  * for the place it is written — what `disambiguateAssetRefs` needs to rename
259
276
  * one. In memory only; never part of the site content.
@@ -261,11 +278,10 @@ export function walkContentAssets(doc, visitor, path = '') {
261
278
  export function collectSectionAssets(section, markdownPath, siteRoot) {
262
279
  const assets = {}
263
280
  const hasExplicitPoster = new Set()
264
- const hasExplicitPreview = new Set()
265
281
  const uses = []
266
282
 
267
283
  // One entry per reference. `set` writes a new reference where this one stands;
268
- // `flags` carries the explicit-poster/preview marks a renamed video/PDF must keep.
284
+ // `flags` carries the explicit-poster mark a renamed video must keep.
269
285
  const add = (ref, set, flags = null) => {
270
286
  const result = resolveAssetPath(ref, markdownPath, siteRoot)
271
287
  if (result.external || !result.resolved) return
@@ -287,10 +303,9 @@ export function collectSectionAssets(section, markdownPath, siteRoot) {
287
303
  const ref = node.attrs.src
288
304
  let flags = null
289
305
  if (attrName === 'src') {
290
- // Check if this image has explicit poster/preview
306
+ // A video with its own poster gets none made for it
291
307
  if (node.attrs.poster) hasExplicitPoster.add(ref)
292
- if (node.attrs.preview) hasExplicitPreview.add(ref)
293
- flags = { poster: !!node.attrs.poster, preview: !!node.attrs.preview }
308
+ flags = { poster: !!node.attrs.poster }
294
309
  }
295
310
  add(ref, holder ? (v) => { holder[attrName] = v } : null, flags)
296
311
  })
@@ -326,7 +341,7 @@ export function collectSectionAssets(section, markdownPath, siteRoot) {
326
341
  walkDataBlockAssets(section.content, (assetPath, set) => add(assetPath, set))
327
342
  }
328
343
 
329
- return { assets, hasExplicitPoster, hasExplicitPreview, uses }
344
+ return { assets, hasExplicitPoster, uses }
330
345
  }
331
346
 
332
347
  /**
@@ -364,7 +379,6 @@ export function disambiguateAssetRefs(collection, siteRoot) {
364
379
  const entry = collection.assets[use.ref]
365
380
  collection.assets[key] = { ...entry, original: key, resolved: use.resolved }
366
381
  if (use.flags?.poster) collection.hasExplicitPoster?.add(key)
367
- if (use.flags?.preview) collection.hasExplicitPreview?.add(key)
368
382
  use.set(key)
369
383
  renamed++
370
384
  }
@@ -373,7 +387,6 @@ export function disambiguateAssetRefs(collection, siteRoot) {
373
387
  if (files.size < 2) continue
374
388
  delete collection.assets[ref]
375
389
  collection.hasExplicitPoster?.delete(ref)
376
- collection.hasExplicitPreview?.delete(ref)
377
390
  }
378
391
  return renamed
379
392
  }
@@ -450,7 +463,6 @@ export function mergeAssetCollections(...collections) {
450
463
  const merged = {
451
464
  assets: {},
452
465
  hasExplicitPoster: new Set(),
453
- hasExplicitPreview: new Set(),
454
466
  uses: []
455
467
  }
456
468
 
@@ -460,7 +472,6 @@ export function mergeAssetCollections(...collections) {
460
472
  Object.assign(merged.assets, collection.assets)
461
473
  if (collection.uses) merged.uses.push(...collection.uses)
462
474
  collection.hasExplicitPoster?.forEach(p => merged.hasExplicitPoster.add(p))
463
- collection.hasExplicitPreview?.forEach(p => merged.hasExplicitPreview.add(p))
464
475
  } else {
465
476
  // Legacy: plain asset manifest
466
477
  Object.assign(merged.assets, collection)
@@ -469,3 +480,73 @@ export function mergeAssetCollections(...collections) {
469
480
 
470
481
  return merged
471
482
  }
483
+
484
+ /**
485
+ * The files each language's pages name that the site's own content does not: what its
486
+ * translations brought, such as an image's file in that language or a free-form section's media.
487
+ *
488
+ * ⭐ A TRANSLATION'S FILE GOES THROUGH THE IMAGE STEP, AS THE CONTENT'S DOES (2026-10-28). A
489
+ * translation names an image's file in its language as `![alt](/x)` (`i18n/merge.js::
490
+ * translatedImage`), and a language's pages are made after the build has processed the site's
491
+ * media, from what it wrote. So a translated page showed that file as `public/` holds it, a PNG
492
+ * where the default language's page had the build's WebP. The bundle collects them here, from each
493
+ * language's merged content, and processes them with the site's own (`site/plugin.js::
494
+ * generateBundle`); a language's pages take the address the build gave each one
495
+ * (`i18n/index.js::buildLocalizedContent`).
496
+ *
497
+ * Only a `/x` names a file here. A translation is written in no folder, so a `./x` it holds has
498
+ * nowhere to resolve from: it comes back in `unplaced`, to be said, never guessed.
499
+ *
500
+ * @param {Array<{ locale: string, content: Object }>} languages - each language's content: its
501
+ * translations merged into the site's content as collected (`mergeTranslations`)
502
+ * @param {Object} known - the site's own manifest, by reference as written (`siteContent.assets`)
503
+ * @param {string} siteRoot - Site root directory
504
+ * @param {string} localesDir - the translations' folder; a refusal names `<locale>.json` in it
505
+ * @returns {{ assets: Object, unplaced: Array<{ locale: string, ref: string }> }} a manifest of the
506
+ * files, in `collectSectionAssets`'s shape, and each reference no folder places
507
+ * @throws {Error} `code: ASSETS_FOLDER_RETIRED` — a `/x` that only `assets/` holds
508
+ */
509
+ export function collectTranslatedAssets(languages, known, siteRoot, localesDir) {
510
+ const assets = {}
511
+ const unplaced = []
512
+ for (const { locale, content } of languages) {
513
+ const table = join(localesDir, `${locale}.json`)
514
+ const seen = new Set()
515
+ const visit = (ref) => {
516
+ if (typeof ref !== 'string' || !ref || isExternalUrl(ref)) return
517
+ if (known?.[ref] || assets[ref] || seen.has(ref)) return
518
+ seen.add(ref)
519
+ if (!ref.startsWith('/')) {
520
+ unplaced.push({ locale, ref })
521
+ return
522
+ }
523
+ const result = resolveAssetPath(ref, table, siteRoot)
524
+ if (result.external || !result.resolved) return
525
+ assets[ref] = {
526
+ original: ref,
527
+ resolved: result.resolved,
528
+ isImage: result.isImage,
529
+ isVideo: result.isVideo,
530
+ isPdf: result.isPdf
531
+ }
532
+ }
533
+ eachSectionDoc(content, (doc) => {
534
+ walkContentAssets(doc, (node) => visit(node.attrs.src))
535
+ walkDataBlockAssets(doc, (path) => visit(path))
536
+ })
537
+ }
538
+ return { assets, unplaced }
539
+ }
540
+
541
+ /** Each section's content in a site's content, as `rewriteSiteContentPaths` walks it: pages, layout areas, subsections. */
542
+ function eachSectionDoc(siteContent, visit) {
543
+ const section = (s) => {
544
+ if (s?.content) visit(s.content)
545
+ s?.subsections?.forEach(section)
546
+ }
547
+ const page = (p) => p?.sections?.forEach(section)
548
+ siteContent?.pages?.forEach(page)
549
+ for (const areas of Object.values(siteContent?.layouts || {})) {
550
+ for (const area of Object.values(areas || {})) page(area)
551
+ }
552
+ }
@@ -19,8 +19,8 @@
19
19
  * actually consumed). Both paths use
20
20
  * the same underlying building blocks (`collectSiteContent`,
21
21
  * `processQueries`, etc.), so behavior stays consistent without forcing
22
- * one path through the other's lifecycle. Media processing — WebP copies,
23
- * video posters, PDF thumbnails — is the bundle path's alone (below).
22
+ * one path through the other's lifecycle. Media processing — WebP copies and
23
+ * video posters — is the bundle path's alone (below).
24
24
  */
25
25
 
26
26
  import { writeFile, mkdir, cp } from 'node:fs/promises'
@@ -164,7 +164,6 @@ export async function buildSiteData({
164
164
  // Strip internal-only properties that don't serialize (Sets don't
165
165
  // JSON-roundtrip): the collector records them for media processing.
166
166
  delete finalContent.hasExplicitPoster
167
- delete finalContent.hasExplicitPreview
168
167
 
169
168
  // ⛔ `config.icons` is a HOST-owned slot on this lane — drop it.
170
169
  //
@@ -1203,7 +1203,7 @@ async function processMarkdownFile(filePath, id, siteRoot, defaultStableId = nul
1203
1203
  const assetCollection = collectSectionAssets(section, filePath, siteRoot)
1204
1204
 
1205
1205
  // Collect icons referenced in this section
1206
- const iconCollection = collectSectionIcons(section, filePath)
1206
+ const iconCollection = collectSectionIcons(section)
1207
1207
 
1208
1208
  return { section, assetCollection, iconCollection }
1209
1209
  }
@@ -1228,7 +1228,7 @@ async function processMarkdownFile(filePath, id, siteRoot, defaultStableId = nul
1228
1228
  */
1229
1229
  async function processNesting(sections, nestConfig, pagePath, siteRoot, cachedFiles) {
1230
1230
  const result = {
1231
- assetCollection: { assets: {}, hasExplicitPoster: new Set(), hasExplicitPreview: new Set() },
1231
+ assetCollection: { assets: {}, hasExplicitPoster: new Set() },
1232
1232
  iconCollection: { icons: new Set(), bySource: new Map() },
1233
1233
  lastModified: null,
1234
1234
  attachedChildren: new Set(),
@@ -1305,8 +1305,7 @@ async function processExplicitSections(sectionsConfig, pagePath, siteRoot, paren
1305
1305
  const sections = []
1306
1306
  let assetCollection = {
1307
1307
  assets: {},
1308
- hasExplicitPoster: new Set(),
1309
- hasExplicitPreview: new Set()
1308
+ hasExplicitPoster: new Set()
1310
1309
  }
1311
1310
  let iconCollection = {
1312
1311
  icons: new Set(),
@@ -1464,7 +1463,7 @@ async function processPage(pagePath, pageName, siteRoot, { isIndex = false, pare
1464
1463
  redirect: target,
1465
1464
  sections: []
1466
1465
  },
1467
- assetCollection: { assets: {}, hasExplicitPoster: new Set(), hasExplicitPreview: new Set() },
1466
+ assetCollection: { assets: {}, hasExplicitPoster: new Set() },
1468
1467
  iconCollection: { icons: new Set(), bySource: new Map() }
1469
1468
  }
1470
1469
  }
@@ -1490,7 +1489,7 @@ async function processPage(pagePath, pageName, siteRoot, { isIndex = false, pare
1490
1489
  rewrite: pageConfig.rewrite,
1491
1490
  sections: []
1492
1491
  },
1493
- assetCollection: { assets: {}, hasExplicitPoster: new Set(), hasExplicitPreview: new Set() },
1492
+ assetCollection: { assets: {}, hasExplicitPoster: new Set() },
1494
1493
  iconCollection: { icons: new Set(), bySource: new Map() }
1495
1494
  }
1496
1495
  }
@@ -1498,8 +1497,7 @@ async function processPage(pagePath, pageName, siteRoot, { isIndex = false, pare
1498
1497
  let hierarchicalSections = []
1499
1498
  let pageAssetCollection = {
1500
1499
  assets: {},
1501
- hasExplicitPoster: new Set(),
1502
- hasExplicitPreview: new Set()
1500
+ hasExplicitPoster: new Set()
1503
1501
  }
1504
1502
  let pageIconCollection = {
1505
1503
  icons: new Set(),
@@ -1939,8 +1937,7 @@ async function collectPagesRecursive(dirPath, parentRoute, siteRoot, orderConfig
1939
1937
  const pages = []
1940
1938
  let assetCollection = {
1941
1939
  assets: {},
1942
- hasExplicitPoster: new Set(),
1943
- hasExplicitPreview: new Set()
1940
+ hasExplicitPoster: new Set()
1944
1941
  }
1945
1942
  let iconCollection = {
1946
1943
  icons: new Set(),
@@ -2465,7 +2462,7 @@ export async function loadFoundationInfo(foundationPath) {
2465
2462
  * @returns {Promise<{ layouts: Object|null, assetCollection: Object, iconCollection: Object }>}
2466
2463
  */
2467
2464
  async function collectLayouts(layoutDir, siteRoot) {
2468
- let assetCollection = { assets: {}, hasExplicitPoster: new Set(), hasExplicitPreview: new Set() }
2465
+ let assetCollection = { assets: {}, hasExplicitPoster: new Set() }
2469
2466
  let iconCollection = { icons: new Set(), bySource: new Map() }
2470
2467
 
2471
2468
  const areas = await readLayoutFolder(layoutDir, { siteRoot })
@@ -2808,9 +2805,9 @@ export async function collectSiteContent(sitePath, options = {}) {
2808
2805
 
2809
2806
  // Log asset summary
2810
2807
  const assetCount = Object.keys(assetCollection.assets).length
2811
- const explicitCount = assetCollection.hasExplicitPoster.size + assetCollection.hasExplicitPreview.size
2808
+ const explicitCount = assetCollection.hasExplicitPoster.size
2812
2809
  if (assetCount > 0) {
2813
- console.log(`[content-collector] Found ${assetCount} asset references${explicitCount > 0 ? ` (${explicitCount} with explicit poster/preview)` : ''}`)
2810
+ console.log(`[content-collector] Found ${assetCount} asset references${explicitCount > 0 ? ` (${explicitCount} with an explicit poster)` : ''}`)
2814
2811
  }
2815
2812
 
2816
2813
  // Build icon manifest from collected icons
@@ -2969,7 +2966,6 @@ export async function collectSiteContent(sitePath, options = {}) {
2969
2966
  versionedScopes: versionedScopesObj,
2970
2967
  assets: assetCollection.assets,
2971
2968
  hasExplicitPoster: assetCollection.hasExplicitPoster,
2972
- hasExplicitPreview: assetCollection.hasExplicitPreview,
2973
2969
  // Icon manifest for preloading
2974
2970
  icons: iconManifest
2975
2971
  }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * WHERE `uniweb dev` SERVES EACH MEDIA FILE A SITE NAMES — so dev shows the file a build ships.
3
+ *
4
+ * A build resolves each local media reference (`resolveAssetPath`, `./assets.js`) and rewrites it
5
+ * to the copy it makes (`processAssets`, `rewriteSiteContentPaths`). Dev ran neither, so a
6
+ * reference stayed as written and only one into `public/` reached its file: a co-located
7
+ * `./photo.png` was read against the page's URL, and answered with the site's HTML and a 200
8
+ * (measured 2026-10-28).
9
+ *
10
+ * So dev rewrites each reference the collector resolved — the same manifest, the same rewrite — to
11
+ * where the dev server serves its file: one in `public/` at the site's base, one under the root by
12
+ * its path from the root, and any other (a mounted folder's) through Vite's `/@fs/`.
13
+ */
14
+
15
+ import { existsSync } from 'node:fs'
16
+ import { isAbsolute, relative, sep } from 'node:path'
17
+ import { pathToFileURL } from 'node:url'
18
+
19
+ /**
20
+ * @param {Object} assets - the collector's manifest: each reference as written → `{ resolved }`
21
+ * @param {{ root: string, publicDir?: string|null, base?: string }} server - the dev server's
22
+ * root and public directory, and the site's base
23
+ * @returns {Object} each reference whose file exists → the address that serves it
24
+ */
25
+ export function devMediaPaths(assets, { root, publicDir = null, base = '/' }) {
26
+ const prefix = base.endsWith('/') ? base : `${base}/`
27
+ const mapping = {}
28
+ for (const [ref, entry] of Object.entries(assets || {})) {
29
+ const file = entry?.resolved
30
+ if (typeof file !== 'string' || !existsSync(file)) continue
31
+ const address = addressOf(file, { root, publicDir, prefix })
32
+ if (address !== ref) mapping[ref] = address
33
+ }
34
+ return mapping
35
+ }
36
+
37
+ function addressOf(file, { root, publicDir, prefix }) {
38
+ for (const dir of [publicDir, root]) {
39
+ const below = dir ? pathBelow(dir, file) : null
40
+ if (below) return prefix + below.split(sep).map(encodeURIComponent).join('/')
41
+ }
42
+ return `${prefix}@fs${pathToFileURL(file).pathname}`
43
+ }
44
+
45
+ /** `file`'s path below `dir`, or null when it is not inside it. */
46
+ function pathBelow(dir, file) {
47
+ const rel = relative(dir, file)
48
+ return rel && !isAbsolute(rel) && rel.split(sep)[0] !== '..' ? rel : null
49
+ }
package/src/site/icons.js CHANGED
@@ -86,33 +86,27 @@ export function walkContentIcons(doc, visitor) {
86
86
  /**
87
87
  * Collect all icon references from a section's content
88
88
  *
89
+ * ⛔ Until 2026-10-28 this also recorded the file each icon came from, and the
90
+ * manifest carried that map as `bySource`: the build machine's absolute paths, in
91
+ * every page's payload, read by nothing.
92
+ *
89
93
  * @param {Object} section - Section object with content
90
- * @param {string} sourcePath - Path to source file (for bySource tracking)
91
94
  * @returns {Object} Icon collection result
92
95
  * - icons: Set of normalized icon references (e.g., "lu:house")
93
- * - bySource: Map of icon references to source files
94
96
  */
95
- export function collectSectionIcons(section, sourcePath) {
97
+ export function collectSectionIcons(section) {
96
98
  const icons = new Set()
97
- const bySource = new Map()
98
99
 
99
100
  if (section.content) {
100
101
  walkContentIcons(section.content, (library, name) => {
101
102
  const normalizedLibrary = normalizeLibrary(library)
102
103
  if (!normalizedLibrary) return
103
104
 
104
- const iconRef = `${normalizedLibrary}:${name}`
105
- icons.add(iconRef)
106
-
107
- // Track which files use this icon
108
- if (!bySource.has(iconRef)) {
109
- bySource.set(iconRef, [])
110
- }
111
- bySource.get(iconRef).push(sourcePath)
105
+ icons.add(`${normalizedLibrary}:${name}`)
112
106
  })
113
107
  }
114
108
 
115
- return { icons, bySource }
109
+ return { icons }
116
110
  }
117
111
 
118
112
  /**
@@ -123,27 +117,13 @@ export function collectSectionIcons(section, sourcePath) {
123
117
  */
124
118
  export function mergeIconCollections(...collections) {
125
119
  const merged = {
126
- icons: new Set(),
127
- bySource: new Map()
120
+ icons: new Set()
128
121
  }
129
122
 
130
123
  for (const collection of collections) {
131
- if (!collection) continue
132
-
133
- // Merge icons set
134
- if (collection.icons) {
124
+ if (collection?.icons) {
135
125
  collection.icons.forEach(icon => merged.icons.add(icon))
136
126
  }
137
-
138
- // Merge bySource map
139
- if (collection.bySource) {
140
- for (const [iconRef, sources] of collection.bySource) {
141
- if (!merged.bySource.has(iconRef)) {
142
- merged.bySource.set(iconRef, [])
143
- }
144
- merged.bySource.get(iconRef).push(...sources)
145
- }
146
- }
147
127
  }
148
128
 
149
129
  return merged
@@ -156,7 +136,7 @@ export function mergeIconCollections(...collections) {
156
136
  * @returns {Object} Icon manifest for site-content.json
157
137
  */
158
138
  export function buildIconManifest(iconCollection) {
159
- const { icons, bySource } = iconCollection
139
+ const { icons } = iconCollection
160
140
 
161
141
  // Get unique families used
162
142
  const families = new Set()
@@ -165,16 +145,9 @@ export function buildIconManifest(iconCollection) {
165
145
  families.add(family)
166
146
  }
167
147
 
168
- // Convert bySource Map to plain object for JSON serialization
169
- const bySourceObj = {}
170
- for (const [iconRef, sources] of bySource) {
171
- bySourceObj[iconRef] = [...new Set(sources)] // dedupe sources
172
- }
173
-
174
148
  return {
175
149
  used: [...icons].sort(),
176
150
  families: [...families].sort(),
177
- bySource: bySourceObj,
178
151
  count: icons.size
179
152
  }
180
153
  }