fix(export-viewer): make the self-contained guard, and its spec, actually load-bearing

The font-inlining guard only caught a RENAME. It asked "did /fonts/<listed
family>.woff2 disappear?", so an ADDITION walked straight past it — and an
addition is the likelier accident: someone doing ordinary app work adds a
display font or a decorative background to the shared theme, has no reason to
open a viewer build config, and ships a keepsake that reaches for
/fonts/Playfair.woff2 on the guest's own disk. font-display: swap hides it, so
the artifact looks right to everyone who happens to have the file locally and
renders in Times New Roman for the couple.

It now asserts the invariant instead of a list: nothing in the emitted
keepsake may reference an external URL. Self-maintaining, and it covers fonts,
images and stylesheets alike. In writeBundle rather than generateBundle —
generateBundle runs more than once and the stylesheet is not inlined on the
earlier pass, so asserting there fails a perfectly good build.

viewer-no-broken-tiles gets a positive anchor. Its "nothing is broken" check
filters img elements, so a viewer that rendered NOTHING yields [] and passes:
the one spec whose whole subject is that the images resolve was the one that
would have stayed green through a total viewer regression. Everything else it
checks comes from the backend and the classic head script, neither of which
needs the viewer bundle to have run.

And a CI job, because neither of the above fires on its own: no workflow,
Dockerfile or script built this viewer, so the guard could sit disarmed
indefinitely, and the committed artifact — compiled into the binary with
include_dir! — could drift from its source with nothing to say so.
This commit is contained in:
fabi
2026-08-12 09:15:24 +02:00
parent a2b3cb0e8d
commit 9f239882ac
3 changed files with 81 additions and 1 deletions

View File

@@ -55,6 +55,38 @@ function inlineThemeFonts() {
);
}
}
},
// The FONTS loop above only catches a RENAME. It cannot catch an ADDITION, and an addition
// is the likelier accident by far: someone doing ordinary app work adds a display font or a
// decorative background to the shared theme, has no reason to open a viewer build config,
// and ships a keepsake that reaches for `/fonts/Playfair.woff2` on the guest's own disk.
// `font-display: swap` hides it, so the artifact looks correct to everyone who happens to
// have the file locally, and renders in Times New Roman for the couple.
//
// So assert the invariant itself rather than a list: nothing in the emitted keepsake may
// reference an external URL. Self-maintaining — it covers renames, additions, fonts,
// images and stylesheets alike, and nobody has to remember it exists.
//
// In `writeBundle`, NOT `generateBundle`: the latter runs more than once, and on the
// earlier pass the stylesheet has not been inlined yet, so asserting there fails a
// perfectly good build. This hook sees only what was actually written.
writeBundle(_options, bundle) {
const html = bundle['index.html'];
if (!html || typeof html.source !== 'string') return;
const external = [...html.source.matchAll(/url\(\s*(['"]?)([^'")]+)\1\s*\)/g)]
.map((m) => m[2].trim())
.filter((u) => !u.startsWith('data:'));
if (external.length) {
this.error(
`inline-theme-fonts: the built keepsake still references ${external.length} ` +
`external asset(s): ${[...new Set(external)].join(', ')}. The viewer is opened ` +
`from file:// with no network and no origin, so every one of these resolves to ` +
`the root of the guest's disk and 404s silently. Inline them (see FONTS above) ` +
`or remove them from the theme the viewer imports.`
);
}
}
};
}