@uniweb/build 1.22.0 → 1.23.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/build",
3
- "version": "1.22.0",
3
+ "version": "1.23.1",
4
4
  "description": "Build tooling for the Uniweb Component Web Platform",
5
5
  "type": "module",
6
6
  "exports": {
@@ -57,15 +57,15 @@
57
57
  "js-yaml": "^4.3.2",
58
58
  "sharp": "^0.35.5",
59
59
  "yaml": "^2.5.0",
60
- "@uniweb/content-reader": "~1.5.10",
61
- "@uniweb/content-writer": "~1.3.2",
60
+ "@uniweb/schemas": "~1.7.2",
62
61
  "@uniweb/projections": "~1.2.17",
63
- "@uniweb/schemas": "~1.7.1",
62
+ "@uniweb/content-writer": "~1.3.2",
63
+ "@uniweb/content-reader": "~1.5.10",
64
64
  "@uniweb/semantic-parser": "~1.13.4",
65
65
  "@uniweb/theming": "~1.3.2"
66
66
  },
67
67
  "optionalDependencies": {
68
- "@uniweb/runtime": "~1.6.1"
68
+ "@uniweb/runtime": "~1.6.3"
69
69
  },
70
70
  "peerDependencies": {
71
71
  "vite": "^5.0.0 || ^6.0.0 || ^7.0.0",
@@ -74,7 +74,7 @@
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/runtime": "~1.6.1",
77
+ "@uniweb/runtime": "~1.6.3",
78
78
  "@uniweb/core": "~1.6.12"
79
79
  },
80
80
  "peerDependenciesMeta": {
@@ -0,0 +1,109 @@
1
+ /**
2
+ * ⭐ A LIBRARY'S MAPPINGS, CARRIED BY A FOUNDATION THAT NAMES THEM. A library (one back office, many
3
+ * front desks) may hold `mappings/` beside its `schemas/`, under its scope. A foundation carries one
4
+ * by naming it in full in its `main.js`:
5
+ *
6
+ * ```js
7
+ * export default {
8
+ * name: '@shop/storefront',
9
+ * mappings: ['@acme/vehicle-to-meta'],
10
+ * }
11
+ * ```
12
+ *
13
+ * The build reads each from the library that defines it (`workspaceMappingFiles`, the rule a
14
+ * library's types are found by), checks it as a foundation's own mapping is checked, against the
15
+ * library's types and mappings, and keeps it in `_mappings` under its full name, with every name in
16
+ * it in full. `register` names it in the foundation's `mappings` refs, and registers it from the
17
+ * library first.
18
+ *
19
+ * A name no package here defines only warns: it may be registered already, and the registry judges.
20
+ *
21
+ * @module
22
+ */
23
+
24
+ import { basename, dirname } from 'node:path'
25
+ import { parseSchemaRef } from '@uniweb/schemas/format'
26
+ import { qualifyRef } from '@uniweb/schemas/refs'
27
+ import { ownSchemaRefs } from '../resolve-data-schema.js'
28
+ import { workspaceMappingFiles } from './shared-types.js'
29
+ import { MAPPINGS_DIR, loadMappingFile, mappingInFull, mappingProblems, ownMappingFiles } from './mappings.js'
30
+
31
+ /**
32
+ * The library mappings a foundation's `main.js` names.
33
+ *
34
+ * @param {*} names - `main.js`'s `mappings:`, as written
35
+ * @param {object} p
36
+ * @param {string} p.srcDir - the foundation's source root
37
+ * @param {string|null} p.ownScope - the foundation's scope, when its name gives one
38
+ * @returns {Promise<{ map: Object<string, object|null>, warnings: string[] }>} `map`: full name →
39
+ * the mapping with its names in full, or null when no package here defines it
40
+ * @throws {Error} naming each one that cannot be carried, and why
41
+ */
42
+ export async function resolveLibraryMappings(names, { srcDir, ownScope = null }) {
43
+ if (names === undefined) return { map: {}, warnings: [] }
44
+ if (!Array.isArray(names) || names.some((n) => typeof n !== 'string')) {
45
+ throw new Error(
46
+ "[uniweb] main.js: `mappings` lists the library mappings the foundation carries, each named in full — `mappings: ['@acme/vehicle-to-meta']`."
47
+ )
48
+ }
49
+ const map = {}
50
+ const warnings = []
51
+ const found = []
52
+ for (const name of names) {
53
+ const at = `main.js: \`mappings\` names \`${name}\``
54
+ let scope
55
+ try {
56
+ ;({ scope } = parseSchemaRef(name))
57
+ } catch {
58
+ found.push(`${at}, which is not a mapping's full name, \`'@scope/<name>'\`.`)
59
+ continue
60
+ }
61
+ if (!scope || name.startsWith('@/')) {
62
+ found.push(`${at}: a mapping of the foundation's own is a file in src/${MAPPINGS_DIR}/, and is not listed. List a library's, in full.`)
63
+ continue
64
+ }
65
+ if (ownScope && `@${String(ownScope).replace(/^@/, '')}` === `@${scope}`) {
66
+ found.push(`${at}, in the foundation's own scope: a mapping of its own is a file in src/${MAPPINGS_DIR}/.`)
67
+ continue
68
+ }
69
+ const files = await workspaceMappingFiles(name, srcDir)
70
+ if (files.length > 1) {
71
+ found.push(`${at}, which ${files.length} packages define: ${files.join(', ')}. Keep one.`)
72
+ continue
73
+ }
74
+ if (!files.length) {
75
+ warnings.push(
76
+ `${at}, which no package here defines, so the build can't check it. \`uniweb register\` names it, and the registry refuses the foundation unless it is registered under @${scope}.`
77
+ )
78
+ map[name] = null
79
+ continue
80
+ }
81
+ const file = files[0]
82
+ const root = dirname(dirname(file))
83
+ const shown = `${MAPPINGS_DIR}/${basename(file)} (for ${name})`
84
+ let decl
85
+ try {
86
+ decl = await loadMappingFile(file)
87
+ } catch (err) {
88
+ found.push(`${shown}: ${String(err?.message || err).split('\n')[0]}`)
89
+ continue
90
+ }
91
+ const problems = mappingProblems(decl, {
92
+ types: new Set(ownSchemaRefs(root)),
93
+ names: new Set(ownMappingFiles(root).map((m) => `@/${m.name}`)),
94
+ })
95
+ if (problems.length) {
96
+ found.push(...problems.map((p) => `${shown}: ${p}`))
97
+ continue
98
+ }
99
+ map[name] = mappingInFull(decl, (ref) => qualifyRef(ref, scope))
100
+ }
101
+ if (found.length) {
102
+ throw new Error(
103
+ found.length === 1
104
+ ? `[uniweb] ${found[0]}`
105
+ : `[uniweb] library mappings the foundation cannot carry:\n${found.map((f) => ` - ${f}`).join('\n')}`
106
+ )
107
+ }
108
+ return { map, warnings }
109
+ }
@@ -25,7 +25,8 @@
25
25
  *
26
26
  * - a file that is not a map, or a `label`/`description` that is not text;
27
27
  * - a `model` that is missing or names no type, or an `@/x` the foundation doesn't define;
28
- * - a `keys_from` that names no mapping, or an `@/x` that isn't one of the foundation's.
28
+ * - a `keys_from` that names no mapping, or an `@/x` that isn't one of the foundation's;
29
+ * - a reverse `follow` line's `model` that names no type, or an `@/x` the foundation doesn't define.
29
30
  *
30
31
  * Mappings are editor-time only: they ride in `schema.json` as `_mappings` and never reach a page.
31
32
  *
@@ -60,7 +61,8 @@ export function ownMappingFiles(srcDir) {
60
61
  .map((file) => ({ name: basename(file, extname(file)), file: join(dir, file) }))
61
62
  }
62
63
 
63
- async function loadMappingFile(file) {
64
+ /** One mapping file, as written. */
65
+ export async function loadMappingFile(file) {
64
66
  if (file.endsWith('.js')) return (await import(pathToFileURL(file).href)).default
65
67
  const text = await readFile(file, 'utf8')
66
68
  return file.endsWith('.json') ? JSON.parse(text) : yaml.load(text, YAML_OPTIONS)
@@ -72,10 +74,11 @@ async function loadMappingFile(file) {
72
74
  * @param {string} srcDir - the foundation's source root
73
75
  * @param {object} [opts]
74
76
  * @param {Iterable<string>} [opts.ownTypes] - the `@/x` data schemas the foundation defines
77
+ * @param {string} [opts.shownIn] - the folder as its errors name it (a library's is `mappings`)
75
78
  * @returns {Promise<Object<string, object>>} `{ '@/<name>': declaration }` as written (`@/x` unqualified)
76
79
  * @throws {Error} naming every mapping that cannot be registered, and why
77
80
  */
78
- export async function buildMappingMap(srcDir, { ownTypes = [] } = {}) {
81
+ export async function buildMappingMap(srcDir, { ownTypes = [], shownIn = `src/${MAPPINGS_DIR}` } = {}) {
79
82
  const files = ownMappingFiles(srcDir)
80
83
  if (!files.length) return {}
81
84
  const types = new Set(ownTypes)
@@ -83,7 +86,7 @@ export async function buildMappingMap(srcDir, { ownTypes = [] } = {}) {
83
86
  const map = {}
84
87
  const found = []
85
88
  for (const { name, file } of files) {
86
- const at = `src/${MAPPINGS_DIR}/${basename(file)}`
89
+ const at = `${shownIn}/${basename(file)}`
87
90
  let decl
88
91
  try {
89
92
  decl = await loadMappingFile(file)
@@ -136,6 +139,63 @@ export function mappingProblems(decl, { types, names }) {
136
139
  found.push(`\`keys_from\` is \`${decl.keys_from}\`, which is not one of the foundation's mappings (src/${MAPPINGS_DIR}/).`)
137
140
  }
138
141
  }
142
+ found.push(...followProblems(decl.follow, types))
143
+ return found
144
+ }
145
+
146
+ /**
147
+ * A mapping with every type and mapping it names in full (`scoped`: `@/x` → `@scope/x`): its `model`,
148
+ * its `keys_from` and a reverse `follow` line's `model`. Nothing else moves, and the file's own object
149
+ * is never touched.
150
+ *
151
+ * @param {object} decl - as written
152
+ * @param {(ref: string) => string} scoped
153
+ * @returns {object}
154
+ */
155
+ export function mappingInFull(decl, scoped) {
156
+ const out = { ...decl }
157
+ if (typeof out.model === 'string') out.model = scoped(out.model)
158
+ if (typeof out.keys_from === 'string') out.keys_from = scoped(out.keys_from)
159
+ if (Array.isArray(out.follow)) {
160
+ out.follow = out.follow.map((line) =>
161
+ isMap(line)
162
+ ? Object.fromEntries(
163
+ Object.entries(line).map(([alias, spec]) =>
164
+ isMap(spec) && typeof spec.model === 'string' ? [alias, { ...spec, model: scoped(spec.model) }] : [alias, spec]
165
+ )
166
+ )
167
+ : line
168
+ )
169
+ }
170
+ return out
171
+ }
172
+
173
+ /**
174
+ * `follow` — a list of one-entry maps, `{ alias: { field, from? } }` forward, or
175
+ * `{ alias: { model, field, names? } }` reverse. Only a reverse line's `model` is a name (a type);
176
+ * the rest is local and the backend's to judge, as is a `follow` shaped any other way.
177
+ */
178
+ export function followModels(follow) {
179
+ if (!Array.isArray(follow)) return []
180
+ const out = []
181
+ for (const [i, line] of follow.entries()) {
182
+ if (!isMap(line)) continue
183
+ for (const [alias, spec] of Object.entries(line)) {
184
+ if (isMap(spec) && 'model' in spec) out.push({ at: `follow[${i}].${alias}.model`, spec })
185
+ }
186
+ }
187
+ return out
188
+ }
189
+
190
+ function followProblems(follow, types) {
191
+ const found = []
192
+ for (const { at, spec } of followModels(follow)) {
193
+ const problem = refProblem(at, spec.model, 'type')
194
+ if (problem) found.push(problem)
195
+ else if (spec.model.startsWith('@/') && !types.has(spec.model)) {
196
+ found.push(`\`${at}\` is \`${spec.model}\`, which is not one of the foundation's types (src/schemas/).`)
197
+ }
198
+ }
139
199
  return found
140
200
  }
141
201
 
@@ -24,6 +24,7 @@ import { workspacePackages } from '../workspace-patterns.js'
24
24
  import { workspaceRootOf } from '../site/workspace-extension.js'
25
25
  import { classifyPackage, isSchemasPackage } from '../utils/classify-package.js'
26
26
  import { librariesOf } from './libraries.js'
27
+ import { MAPPINGS_DIR, ownMappingFiles } from './mappings.js'
27
28
 
28
29
  const real = (path) => {
29
30
  try {
@@ -177,6 +178,25 @@ function packageDirOf(dir) {
177
178
  * @returns {Promise<string[]>} the files that define it, each once; the caller refuses more than one
178
179
  */
179
180
  export async function workspaceTypeFiles(ref, fromDir) {
181
+ return workspaceDeclarationFiles(ref, fromDir, 'schemas', ownSchemaFiles)
182
+ }
183
+
184
+ /**
185
+ * ⭐ A MAPPING ANOTHER PACKAGE DEFINES, found by the rule `workspaceTypeFiles` finds a type by: a
186
+ * foundation whose name gives the scope and whose own `mappings/` holds it, or a library (a schemas
187
+ * package) of that scope that holds `mappings/` (`foundation/mappings.js`). A foundation names one in
188
+ * its `main.js` (`mappings: ['@acme/vehicle-to-meta']`); the build reads it from there.
189
+ *
190
+ * @param {string} ref - `@scope/name`
191
+ * @param {string} fromDir - the foundation (its source folder) or site asking
192
+ * @returns {Promise<string[]>} the files that define it, each once; the caller refuses more than one
193
+ */
194
+ export async function workspaceMappingFiles(ref, fromDir) {
195
+ return workspaceDeclarationFiles(ref, fromDir, MAPPINGS_DIR, ownMappingFiles)
196
+ }
197
+
198
+ /** The files of the packages a package may read that define `ref` in their `folder`. */
199
+ async function workspaceDeclarationFiles(ref, fromDir, folder, ownFiles) {
180
200
  const m = /^(@[^/]+)\/(.+)$/.exec(ref || '')
181
201
  if (!m) return []
182
202
  const [, scope, name] = m
@@ -198,8 +218,8 @@ export async function workspaceTypeFiles(ref, fromDir) {
198
218
  if (!kind) continue
199
219
  if ((await cachedTypeScope(dir, kind)) !== scope) continue
200
220
  const typesAt = typeRoot(dir, kind)
201
- if (!existsSync(join(typesAt, 'schemas'))) continue
202
- for (const own of ownSchemaFiles(typesAt)) {
221
+ if (!existsSync(join(typesAt, folder))) continue
222
+ for (const own of ownFiles(typesAt)) {
203
223
  if (own.name === name) files.set(real(own.file), own.file)
204
224
  }
205
225
  }
@@ -21,11 +21,6 @@
21
21
 
22
22
  import { visitModelStrings } from './model-fields.js'
23
23
 
24
- /** Types that are never translatable regardless of schema. */
25
- export const NON_TRANSLATABLE_TYPES = new Set([
26
- 'number', 'boolean', 'date', 'datetime', 'url', 'email', 'image'
27
- ])
28
-
29
24
  /** Field names skipped by the heuristic extractor (structural, not human-readable). */
30
25
  export const HEURISTIC_SKIP_FIELDS = new Set([
31
26
  'slug', 'id', 'type', 'status', 'href', 'url', 'src', 'icon',
package/src/i18n/index.js CHANGED
@@ -407,12 +407,15 @@ function declaredLanguages(config, defaultLocale, locales) {
407
407
  * @param {string} [options.outputDir] - the built site, `<siteRoot>/dist` by default
408
408
  * @param {boolean} [options.projections=false] - write each language's projections. The bundle
409
409
  * lane's: a static host serves them. The link lane builds for a host, which derives its own.
410
+ * @param {boolean} [options.records=true] - translate each language's compiled records
411
+ * (`dist/<locale>/data/`). The bundle lane's: a static host serves them. The link lane compiles
412
+ * none — a push sends each record, in every language, to a host's records service.
410
413
  * @returns {Promise<{ content: Object, records: Object, projections: Object }>} what each step
411
414
  * wrote, by language — `projections[locale]` the files (`projectionFiles`), each `fileName`
412
415
  * from `outputDir`
413
416
  */
414
417
  export async function buildLocalizedSite(siteRoot, options = {}) {
415
- const { localesDir = DEFAULTS.localesDir, outputDir = join(siteRoot, 'dist'), projections = false } = options
418
+ const { localesDir = DEFAULTS.localesDir, outputDir = join(siteRoot, 'dist'), projections = false, records: withRecords = true } = options
416
419
 
417
420
  // The default language is the root tree, never a subdirectory — by the default the content step
418
421
  // reads (`buildLocalizedContent`), so the records step writes no `dist/<default>/data/` either.
@@ -422,12 +425,14 @@ export async function buildLocalizedSite(siteRoot, options = {}) {
422
425
 
423
426
  // Records first: a language's projections list its record pages, and translating them reads
424
427
  // the site's own files alone — never a translated page.
425
- const records = await buildLocalizedRecords(siteRoot, {
426
- locales,
427
- outputDir,
428
- recordLocalesDir: join(siteRoot, localesDir, RECORD_LOCALES_DIR),
429
- localesDir: join(siteRoot, localesDir)
430
- })
428
+ const records = withRecords
429
+ ? await buildLocalizedRecords(siteRoot, {
430
+ locales,
431
+ outputDir,
432
+ recordLocalesDir: join(siteRoot, localesDir, RECORD_LOCALES_DIR),
433
+ localesDir: join(siteRoot, localesDir)
434
+ })
435
+ : {}
431
436
  const content = await buildLocalizedContent(siteRoot, { localesDir, locales, outputDir, fallbackToSource: true })
432
437
 
433
438
  const written = {}