From 50c2e0e8c77e69c3e70c3a7eb08c4e6d00a1b869 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sun, 1 Mar 2026 15:53:05 -0300 Subject: [PATCH 1/2] fix(asyncReader): handle CJS/ESM interop and null GlobalWorkerOptions In Vite watch mode, pdfjs-dist is served without pre-bundling when the package is linked via file: path, causing the module to be wrapped under .default instead of exposing named exports directly. This results in PDFWorker being undefined at runtime. - Add normalizePdfjs() to unwrap .default when present (CJS/ESM interop) - Guard ensureWorkerSrc against undefined GlobalWorkerOptions - Reset sharedWorker to null in setWorkerPath to force recreation - Add .catch with console.warn in setWorkerPath for failed async updates Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- src/utils/asyncReader.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/utils/asyncReader.ts b/src/utils/asyncReader.ts index 7da94db..a8770f4 100644 --- a/src/utils/asyncReader.ts +++ b/src/utils/asyncReader.ts @@ -8,9 +8,16 @@ let pdfjsPromise: Promise | null = null let workerUrlPromise: Promise | null = null let workerSrcOverride: string | null = null +type PdfjsModule = typeof import('pdfjs-dist') + +function normalizePdfjs(mod: PdfjsModule): PdfjsModule { + const m = mod as PdfjsModule & { default?: PdfjsModule } + return (m.default?.PDFWorker ? m.default : m) as PdfjsModule +} + function loadPdfjs() { if (!pdfjsPromise) { - pdfjsPromise = import('pdfjs-dist') + pdfjsPromise = import('pdfjs-dist').then(normalizePdfjs) } return pdfjsPromise } @@ -24,7 +31,10 @@ function loadWorkerUrl() { return workerUrlPromise } -async function ensureWorkerSrc(pdfjs: typeof import('pdfjs-dist')) { +async function ensureWorkerSrc(pdfjs: PdfjsModule) { + if (!pdfjs?.GlobalWorkerOptions) { + return + } if (workerSrcOverride) { pdfjs.GlobalWorkerOptions.workerSrc = workerSrcOverride return @@ -34,7 +44,7 @@ async function ensureWorkerSrc(pdfjs: typeof import('pdfjs-dist')) { } } -async function getSharedWorker(pdfjs: typeof import('pdfjs-dist')): Promise { +async function getSharedWorker(pdfjs: PdfjsModule): Promise { if (!sharedWorker) { await ensureWorkerSrc(pdfjs) sharedWorker = new pdfjs.PDFWorker({}) as PDFWorker @@ -44,9 +54,14 @@ async function getSharedWorker(pdfjs: typeof import('pdfjs-dist')): Promise { - pdfjs.GlobalWorkerOptions.workerSrc = path + if (pdfjs?.GlobalWorkerOptions) { + pdfjs.GlobalWorkerOptions.workerSrc = path + } + }).catch((error) => { + console.warn('setWorkerPath: failed to update pdfjs workerSrc immediately', error) }) } } From aa39f8366650e632f2e38921dabde5d417c62b9f Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sun, 1 Mar 2026 15:54:23 -0300 Subject: [PATCH 2/2] chore: bump version Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fc9e7ce..de85f3d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@libresign/pdf-elements", "description": "PDF viewer with draggable and resizable element overlays for Vue 3", - "version": "1.0.1", + "version": "1.0.2", "author": "LibreCode ", "private": false, "main": "dist/index.js",