Skip to content

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

Merged
alekswebnet merged 2 commits into
masterfrom
feat/vite-data-plugin
Nov 24, 2026
Merged

alekswebnet merged 2 commits into
masterfrom
feat/vite-data-plugin

Conversation

@alekswebnet

@alekswebnet alekswebnet commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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:

    Breakage Tests that fail
    wasm removed from the folder list 3
    Assets never match 3
    Data emitted at the output root 3
    Sandbox emitted next to viewer.mjs 1 (the build with separate folders)
    Typo in the ./vite export path 1
  • 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.

Written for commit 22eeef1. Summary will update on new commits.

View guided diff Turn on auto-fix

…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>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread tests/plugin/vite.test.ts Resolved
Comment thread tests/plugin/vite.test.ts
configFile: false,
logLevel: 'error',
plugins,
build: { outDir, emptyOutDir: true, rolldownOptions: { output } }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
build: { outDir, emptyOutDir: true, rolldownOptions: { output } }
build: { outDir, emptyOutDir: true, rollupOptions: { output } }

Comment thread plugins/vite.js
export const pdfjsViewerData = () => ({
name: 'pdfjs-viewer-data',
generateBundle(_options, bundle) {
const anchors = Object.entries(dataByAnchor).map(([name, entries]) => ({ file: readFile(name), entries }))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@alekswebnet
alekswebnet merged commit 1673385 into master Nov 24, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant