Repository navigation
feat: Vite plugin that emits the PDF.js data next to the bundled viewer - #84
Conversation
…d viewer Vite emits the viewer's runtime files under hashed names and leaves the PDF.js data folders behind, so a bundled app answers 404 for wasm/jbig2.wasm and scanned pages lose their image. The plugin, exported as @missing-elements/pdfjs-viewer/vite, finds the emitted viewer.mjs and pdf.mjs by content and writes the data next to them, where the element already looks for it. Tests build a fixture app from dist with and without the plugin and open both in Firefox. CI runs them through pnpm test:plugin, which builds the package first. Part of #83 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
There was a problem hiding this comment.
2 issues found across 14 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="plugins/vite.js">
<violation number="1" location="plugins/vite.js:34">
P3: `anchors` reads both `dist` anchor files eagerly on every build, even when the bundle has no viewer asset. Read them only when a candidate asset needs comparing, so apps that don't use the viewer don't pay for it or fail when `dist` is absent.</violation>
</file>
<file name="tests/plugin/vite.test.ts">
<violation number="1" location="tests/plugin/vite.test.ts:34">
P2: The renamed-output case is not portable to Vite 4–7 because those versions ignore `build.rolldownOptions`. Use the version-neutral `rollupOptions` setting, or select the appropriate option for each Vite generation, so this test actually covers the supported versions.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| configFile: false, | ||
| logLevel: 'error', | ||
| plugins, | ||
| build: { outDir, emptyOutDir: true, rolldownOptions: { output } } |
There was a problem hiding this comment.
P2: The renamed-output case is not portable to Vite 4–7 because those versions ignore build.rolldownOptions. Use the version-neutral rollupOptions setting, or select the appropriate option for each Vite generation, so this test actually covers the supported versions.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/plugin/vite.test.ts, line 34:
<comment>The renamed-output case is not portable to Vite 4–7 because those versions ignore `build.rolldownOptions`. Use the version-neutral `rollupOptions` setting, or select the appropriate option for each Vite generation, so this test actually covers the supported versions.</comment>
<file context>
@@ -0,0 +1,176 @@
+ configFile: false,
+ logLevel: 'error',
+ plugins,
+ build: { outDir, emptyOutDir: true, rolldownOptions: { output } }
+ })
+ copyFileSync(samplePdf, join(outDir, 'ccitt-g4.pdf'))
</file context>
| build: { outDir, emptyOutDir: true, rolldownOptions: { output } } | |
| build: { outDir, emptyOutDir: true, rollupOptions: { output } } |
| export const pdfjsViewerData = () => ({ | ||
| name: 'pdfjs-viewer-data', | ||
| generateBundle(_options, bundle) { | ||
| const anchors = Object.entries(dataByAnchor).map(([name, entries]) => ({ file: readFile(name), entries })) |
There was a problem hiding this comment.
P3: anchors reads both dist anchor files eagerly on every build, even when the bundle has no viewer asset. Read them only when a candidate asset needs comparing, so apps that don't use the viewer don't pay for it or fail when dist is absent.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At plugins/vite.js, line 34:
<comment>`anchors` reads both `dist` anchor files eagerly on every build, even when the bundle has no viewer asset. Read them only when a candidate asset needs comparing, so apps that don't use the viewer don't pay for it or fail when `dist` is absent.</comment>
<file context>
@@ -0,0 +1,52 @@
+export const pdfjsViewerData = () => ({
+ name: 'pdfjs-viewer-data',
+ generateBundle(_options, bundle) {
+ const anchors = Object.entries(dataByAnchor).map(([name, entries]) => ({ file: readFile(name), entries }))
+
+ Object.values(bundle)
</file context>
The check compared file sizes only, so wrong content of the right length passed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Summary
Part of #83. An app bundled with Vite gets the viewer's runtime files but not the PDF.js data folders.
assets/wasm/jbig2.wasmthen answers 404 and scanned pages lose their image, with only console warnings to show for it.This adds a Vite plugin that writes the data into the build output, next to the emitted viewer files, where the element already looks for it:
How it differs from the sketch in the issue
originalFileNames, which Vite only fills in from 5.4. On Vite 5.0.13 and 4.5.14 the sketch matched nothing and emitted nothing, with no error. The plugin compares each emitted asset withdist/viewer.mjsanddist/pdf.mjsbyte for byte instead. That works on every version tried, still follows anyassetFileNamessetting, and needs noconfigResolvedhook.apply: 'build'. The only hook isgenerateBundle, which the dev server never calls.includeoption can follow if someone asks for a smaller output.Also changed
assets-basefor other bundlers, and a note on caching (below).pnpm test:plugin: builds the package, then runs the plugin tests in Node. CI runs it in place of the plainpnpm buildstep.package.json: the./viteexport andpluginsinfiles.Two things to know
assets/. A host that caches all ofassets/asimmutablewould keep serving the oldjbig2.wasmafter an upgrade. The README warns about it. Putting the data in a folder named after the hashed viewer file would remove the risk, but the element would have to look there, so it isn't in this PR.Not in this PR
The remaining steps of #83: the webpack / Rspack variant, the console error for a missing data folder, and the hosting page on the docs site (after the release).
Validation
pnpm test:plugin: 5 passed. The tests build a fixture app fromdistwith and without the plugin and open both in Firefox from a static server that answers 404 for a missing file. The sample PDF is a hand-written CCITT image, which PDF.js decodes withjbig2.wasm, the file from the issue. Without the plugin the page stays blank and/assets/wasm/jbig2.wasmis a 404. With it the image is drawn and nothing under/assets/is missing.Each test fails when the plugin is broken. Five deliberate breakages, each restored afterwards:
wasmremoved from the folder listviewer.mjs./viteexport pathVite versions. A build with the plugin has 296 files (10 without) on Vite 4.5.14, 5.0.13, 5.4.21, 6.4.6, 7.3.8 and 8.3.8.
pnpm test -- --run: 40 passed. The browser suite is unchanged and skips the plugin tests.npm pack --dry-runlistsplugins/vite.jsandplugins/vite.d.ts.Types. A config that passes
pdfjsViewerData()todefineConfigcompiles under--strict.Not tested: plain Rollup and Rolldown, SSR builds, Windows. In a document, only the CCITT path is exercised; for the other folders the tests check that every file in
distis emitted at the right place and size.Checklist
🤖 Generated with Claude Code
Summary by cubic
Adds a Vite plugin that copies the PDF.js data folders (
wasm,cmaps,standard_fonts,iccs,images,pdf.sandbox.mjs) into the build output next to the viewer runtime files, fixing blank scanned pages and missing CJK text in bundled apps.Export it from@missing-elements/pdfjs-viewer/viteand add it tovite.config.js:assetFileNames.assets/as immutable, exclude them from that rule to get new files after upgrades.pnpm test:plugin, which builds the package, bundles a fixture app with and without the plugin, checks the rendered image in Firefox, and verifies every emitted data file matchesdistbyte for byte../viteexport andpluginsis included in published files.Part of #83.
Written for commit 22eeef1. Summary will update on new commits.