Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions lib/domstack-manifest/reconcile.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<DomstackManifestKind, number>} */ ({
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.
Expand Down Expand Up @@ -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) {
Expand Down
73 changes: 73 additions & 0 deletions lib/domstack-manifest/reconcile.test.js
Original file line number Diff line number Diff line change
@@ -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, '<feed />')

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, '<!doctype html><title>Home</title>')

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 })
}
})
})
Loading