From 47280e603096da41c41f5695b04d0150d4d5d9be Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Thu, 3 Dec 2026 17:46:36 -0800 Subject: [PATCH] Add 'page-output' to manifest KIND_PRIORITY and make table exhaustive --- lib/domstack-manifest/reconcile.js | 35 ++++++------ lib/domstack-manifest/reconcile.test.js | 73 +++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 lib/domstack-manifest/reconcile.test.js diff --git a/lib/domstack-manifest/reconcile.js b/lib/domstack-manifest/reconcile.js index bbbdfd5e..b4369285 100644 --- a/lib/domstack-manifest/reconcile.js +++ b/lib/domstack-manifest/reconcile.js @@ -1,6 +1,6 @@ /** * @import { DomStackWarning } from '../helpers/domstack-warning.js' - * @import { DomstackManifestRecord, DomstackManifestEntry, DomstackManifestInternalEntry, DomstackManifestOptions, ReconcileDomstackManifestResult } from './schema.js' + * @import { DomstackManifestKind, DomstackManifestRecord, DomstackManifestEntry, DomstackManifestInternalEntry, DomstackManifestOptions, ReconcileDomstackManifestResult } from './schema.js' */ import { createHash } from 'node:crypto' import ignore from 'ignore' @@ -11,20 +11,21 @@ import { updateManifestVersionHash, updateManifestVersionValue } from './hash.js // Prefer the output type with the most specific runtime meaning when builders // report the same output path more than once. -const KIND_PRIORITY = new Map([ - ['page', 100], - ['service-worker', 95], - ['template', 90], - ['worker-manifest', 80], - ['worker', 70], - ['script', 60], - ['style', 50], - ['chunk', 40], - ['static', 30], - ['copy', 20], - ['sourcemap', 10], - ['metadata', 0], -]) +const KIND_PRIORITY = /** @satisfies {Record} */ ({ + page: 100, + 'service-worker': 95, + 'page-output': 90, + template: 90, + 'worker-manifest': 80, + worker: 70, + script: 60, + style: 50, + chunk: 40, + static: 30, + copy: 20, + sourcemap: 10, + metadata: 0, +}) /** * Reconcile records emitted by build steps into a normalized, revisioned manifest. @@ -93,8 +94,8 @@ export async function reconcileDomstackManifest ({ dest, records = [], entries: function setEntry (entries, entry, warnings) { if (!entry) return const existing = entries.get(entry.outputRelname) - const entryPriority = KIND_PRIORITY.get(entry.kind) ?? -1 - const existingPriority = existing ? KIND_PRIORITY.get(existing.kind) ?? -1 : -1 + const entryPriority = KIND_PRIORITY[entry.kind] ?? -1 + const existingPriority = existing ? KIND_PRIORITY[existing.kind] ?? -1 : -1 const replaceExisting = !existing || entryPriority >= existingPriority if (existing) { diff --git a/lib/domstack-manifest/reconcile.test.js b/lib/domstack-manifest/reconcile.test.js new file mode 100644 index 00000000..7be28577 --- /dev/null +++ b/lib/domstack-manifest/reconcile.test.js @@ -0,0 +1,73 @@ +import { describe, it } from 'node:test' +import assert from 'node:assert/strict' +import { mkdtemp, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { reconcileDomstackManifest } from './reconcile.js' + +describe('reconcileDomstackManifest KIND_PRIORITY', () => { + it('prefers page-output over static, copy, sourcemap, and metadata records', async () => { + const dest = await mkdtemp(join(tmpdir(), 'domstack-manifest-reconcile-')) + try { + const filepath = join(dest, 'feed.xml') + await writeFile(filepath, '') + + for (const lowerKind of /** @type {const} */ (['static', 'copy', 'sourcemap', 'metadata'])) { + const { manifest } = await reconcileDomstackManifest({ + dest, + records: [ + { + outputRelname: 'feed.xml', + filepath, + kind: 'page-output', + sourceRelname: 'index.md', + }, + { + outputRelname: 'feed.xml', + filepath, + kind: lowerKind, + sourceRelname: 'other.xml', + }, + ], + }) + + assert.equal(manifest.entries.length, 1) + assert.equal(manifest.entries[0]?.kind, 'page-output', `expected page-output to beat ${lowerKind}`) + } + } finally { + await rm(dest, { recursive: true, force: true }) + } + }) + + it('prefers page over page-output when both target the same output path', async () => { + const dest = await mkdtemp(join(tmpdir(), 'domstack-manifest-reconcile-')) + try { + const filepath = join(dest, 'index.html') + await writeFile(filepath, 'Home') + + const { manifest } = await reconcileDomstackManifest({ + dest, + records: [ + { + outputRelname: 'index.html', + filepath, + kind: 'page', + sourceRelname: 'index.md', + page: { path: '', url: '/' }, + }, + { + outputRelname: 'index.html', + filepath, + kind: 'page-output', + sourceRelname: 'index.md', + }, + ], + }) + + assert.equal(manifest.entries.length, 1) + assert.equal(manifest.entries[0]?.kind, 'page') + } finally { + await rm(dest, { recursive: true, force: true }) + } + }) +})