From 7946880a2f2d9d2b079910acce78832b0f6a4c80 Mon Sep 17 00:00:00 2001 From: Andrii Vitiv Date: Sat, 7 Mar 2026 20:27:59 +0200 Subject: [PATCH] Move responsibility for configuring PDF worker to the consumer --- example/package-lock.json | 3 +++ example/package.json | 3 +++ example/src/App.tsx | 4 ++++ package-lock.json | 1 - package.json | 1 - src/PDFPreviewer.tsx | 5 +---- src/index.ts | 3 ++- src/pdf.worker.d.ts | 1 - 8 files changed, 13 insertions(+), 8 deletions(-) delete mode 100644 src/pdf.worker.d.ts diff --git a/example/package-lock.json b/example/package-lock.json index bcde552..e4bfccd 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -43,6 +43,9 @@ "webpack": "^5.104.1", "webpack-cli": "^5.1.4", "webpack-dev-server": "^4.15.1" + }, + "peerDependencies": { + "pdfjs-dist": "^4.x || ^5.x" } }, "node_modules/@aashutoshrathi/word-wrap": { diff --git a/example/package.json b/example/package.json index 72467ba..180df68 100644 --- a/example/package.json +++ b/example/package.json @@ -35,6 +35,9 @@ "react-dom": "^18.2.0", "react-fast-pdf": "^1.0.23" }, + "peerDependencies": { + "pdfjs-dist": "^4.x || ^5.x" + }, "devDependencies": { "@babel/cli": "^7.22.9", "@babel/core": "^7.22.9", diff --git a/example/src/App.tsx b/example/src/App.tsx index 88c3bad..9d6b477 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -1,7 +1,11 @@ import React, {useState} from 'react'; +import pdfWorkerSource from 'pdfjs-dist/build/pdf.worker.min.mjs'; +import * as pdfjs from 'pdfjs-dist'; import ReactFastPDF, {PDFPreviewer} from 'react-fast-pdf'; import './index.css'; +pdfjs.GlobalWorkerOptions.workerSrc = URL.createObjectURL(new Blob([pdfWorkerSource], {type: 'text/javascript'})); + function App() { const [file, setFile] = useState(null); diff --git a/package-lock.json b/package-lock.json index db29a5c..1789beb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,6 @@ }, "peerDependencies": { "lodash": "4.x", - "pdfjs-dist": "5.4.296", "react": "18.x", "react-dom": "18.x" } diff --git a/package.json b/package.json index b949689..1d4dee8 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ }, "peerDependencies": { "lodash": "4.x", - "pdfjs-dist": "5.4.296", "react": "18.x", "react-dom": "18.x" }, diff --git a/src/PDFPreviewer.tsx b/src/PDFPreviewer.tsx index 8b88ad1..aa1cebf 100644 --- a/src/PDFPreviewer.tsx +++ b/src/PDFPreviewer.tsx @@ -1,9 +1,8 @@ -import pdfWorkerSource from 'pdfjs-dist/build/pdf.worker.min.mjs'; import React, {memo, useCallback, useLayoutEffect, useRef, useState} from 'react'; import type {CSSProperties, ReactNode} from 'react'; import times from 'lodash/times.js'; import {VariableSizeList as List} from 'react-window'; -import {Document, pdfjs} from 'react-pdf'; +import {Document} from 'react-pdf'; import 'react-pdf/dist/Page/AnnotationLayer.css'; import 'react-pdf/dist/Page/TextLayer.css'; @@ -32,8 +31,6 @@ type Props = { type OnPasswordCallback = (password: string | null) => void; -pdfjs.GlobalWorkerOptions.workerSrc = URL.createObjectURL(new Blob([pdfWorkerSource], {type: 'text/javascript'})); - const DefaultLoadingComponent =

Loading...

; const DefaultErrorComponent =

Failed to load the PDF file :(

; diff --git a/src/index.ts b/src/index.ts index 7fb5b72..7c3800d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,9 @@ +import {pdfjs} from 'react-pdf'; import PDFPreviewer from './PDFPreviewer.js'; const PACKAGE_NAME = 'react-fast-pdf'; -export {PDFPreviewer}; +export {PDFPreviewer, pdfjs}; export default { PackageName: PACKAGE_NAME, diff --git a/src/pdf.worker.d.ts b/src/pdf.worker.d.ts deleted file mode 100644 index c636372..0000000 --- a/src/pdf.worker.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'pdfjs-dist/build/pdf.worker.min.mjs';