feat: Vite plugin that emits the PDF.js data next to the bundled viewer

Merged
AL
14h ago
+299-5

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 with dist/viewer.mjs and dist/pdf.mjs byte for byte instead. That works on every version tried, still follows any assetFileNames setting, and needs no configResolved hook.
  • No apply: 'build'. The only hook is generateBundle, which the dev server never calls.
  • No options. Everything is emitted. An include option can follow if someone asks for a smaller output.

Also changed

  • README: the plugin for Vite, copy + assets-base for 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 plain pnpm build step.
  • package.json: the ./vite export and plugins in files.

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 of assets/ as immutable would keep serving the old jbig2.wasm after 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 from dist with 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 with jbig2.wasm, the file from the issue. Without the plugin the page stays blank and /assets/wasm/jbig2.wasm is 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:

    BreakageTests that fail
    wasm removed from the folder list3
    Assets never match3
    Data emitted at the output root3
    Sandbox emitted next to viewer.mjs1 (the build with separate folders)
    Typo in the ./vite export 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-run lists plugins/vite.js and plugins/vite.d.ts.

  • Types. A config that passes pdfjsViewerData() to defineConfig compiles 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 matches dist byte for byte.
  • The package gains a ./vite export and plugins is included in published files.

Part of #83.