fix(invoice-inbox): switch from pdfjs-dist to unpdf (#409)

* fix(invoice-inbox): switch from pdfjs-dist to unpdf for PDF text extraction

After three rounds of fighting pdfjs on Vercel (#407 stubbed DOM globals,
#408 tried to ship the worker file via outputFileTracingIncludes), text
extraction still failed in prod with "Setting up fake worker failed" —
Next's tracer can't reliably include pdfjs-dist's worker file when the
package is marked as a server external.

unpdf is a serverless-first wrapper around pdfjs (by unjs) that ships its
own bundled pdfjs build with no canvas/worker dependencies. Drop-in
replacement: extractText returns merged page text directly.

- Remove DOM stubs, serverExternalPackages, outputFileTracingIncludes
- Replace pdfjs-dist with unpdf (no transitive deps)
- Update test mock from getDocument → extractText

All 45 invoice-inbox unit tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(invoice-inbox): make unpdf import static, rename pdfjs test labels

Per Greptile review on #409:

1. CRITICAL: tryExtractPdfText was still using await import('unpdf'), a
   dynamic import. CLAUDE.md forbids dynamic imports in extensions
   precisely because Next.js bundling can't reliably trace them — which
   is the same class of failure that caused the pdfjs prod bug. unpdf
   bundles statically (no canvas/worker), so a top-level static import
   is safe and correct.

2. NIT: two test descriptions still said "pdfjs" after the mock rename.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-05-07 11:32:50 +02:00
committed by GitHub
co-authored by Claude Opus 4.7
parent 3a74f0ab61
commit 8676f10fdd
5 changed files with 43 additions and 1881 deletions
-13
View File
@@ -20,19 +20,6 @@ const cspDirectives = [
const nextConfig: NextConfig = {
output: 'standalone',
// Keep pdfjs-dist out of the bundle. The legacy build references
// @napi-rs/canvas at module load and breaks Vercel's bundling step.
// Text extraction works in pure Node once DOM globals are stubbed at
// module load (see extensions/general/invoice-inbox/lib/extract-invoice-fields.ts).
serverExternalPackages: ['pdfjs-dist'],
// Force the pdfjs worker file into the function bundle. Next.js's tracer
// can't see it (loaded dynamically by name), so without this it's missing
// in /var/task and getDocument() fails with "Setting up fake worker failed".
// Scoped to the invoice-inbox sub-path — other extensions don't use pdfjs
// and shouldn't pay the ~1 MB worker cost.
outputFileTracingIncludes: {
'/api/extensions/ext/invoice-inbox/**': ['./node_modules/pdfjs-dist/legacy/build/pdf.worker.mjs'],
},
async redirects() {
return [
{