feat: Vite plugin that emits the PDF.js data next to the bundled viewer
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.wasm then 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:
// vite.config.js
import { pdfjsViewerData } from '@missing-elements/pdfjs-viewer/vite'
export default {
plugins: [pdfjsViewerData()]
}
How it differs from the sketch in the issue
- It finds the viewer files by content, not by source path. The sketch reads
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. - No
apply: 'build'. The only hook isgenerateBundle, which the dev server never calls. - No options. Everything is emitted. An
includeoption can follow if someone asks for a smaller output.
Also changed
- README: the plugin for Vite, copy +
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
- Caching. The data files keep the names PDF.js asks for, so they sit unhashed in
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. - Size. The data is 286 files and 3.4 MB in bytes. The 4.3 MB in the issue is disk usage, which rounds every small CMap file up to a block.
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:
Breakage Tests that fail wasmremoved from the folder list3 Assets never match 3 Data emitted at the output root 3 Sandbox emitted next to viewer.mjs1 (the build with separate folders) Typo in the ./viteexport path1 -
Vite 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 dist is emitted at the right place and size.
Checklist
- The change is focused and includes tests for behavior changes.
- Relevant tests and the build pass locally.
- Public API, behavior, or user-guidance documentation is updated where needed.
- Accessibility impact has been considered, including keyboard navigation and focus visibility. The viewer's markup and styles are untouched.
- This pull request does not disclose a security vulnerability.
š¤ 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/vite and add it to vite.config.js:
import { pdfjsViewerData } from '@missing-elements/pdfjs-viewer/vite'
export default { plugins: [pdfjsViewerData()] }
- The plugin locates the emitted runtime files by content, so it works across Vite versions and respects
assetFileNames. - Data files keep unhashed names; if your server caches
assets/as immutable, exclude them from that rule to get new files after upgrades. - CI now runs
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. - The package gains a
./viteexport andpluginsis included in published files.
Part of #83.