From c77b5ff3371506ede45f2410c5000b9a5c2049bc Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 26 Mar 2024 13:13:40 -0400 Subject: [PATCH] upgrade pdfjs to version 4 --- example/example.ts | 4 ++-- package-lock.json | 12 ++++++------ package.json | 13 ++++++------- src/base.ts | 12 ++++++++++-- tsconfig.commonjs.json | 7 ------- tsconfig.example.json | 1 - tsconfig.json | 8 ++++---- webpack.config.js | 2 +- 8 files changed, 29 insertions(+), 30 deletions(-) delete mode 100644 tsconfig.commonjs.json diff --git a/example/example.ts b/example/example.ts index 4616598..365ce9f 100644 --- a/example/example.ts +++ b/example/example.ts @@ -1,3 +1,3 @@ -import { init } from '../dist/es2015'; +import { init } from '../dist/esnext'; -init('http://localhost:8080/pdf.worker.min.js'); \ No newline at end of file +init('http://localhost:8080/pdf.worker.min.mjs'); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a0fb873..99efb12 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "document-viewer-ts", - "version": "0.7.0-legacy", + "version": "1.0.0-legacy", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "document-viewer-ts", - "version": "0.7.0-legacy", + "version": "1.0.0-legacy", "license": "MIT", "devDependencies": { "@playwright/test": "^1.42.1", @@ -23,7 +23,7 @@ "core-js": "~3.23.3", "eslint": "~8.38.0", "express": "^4.18.3", - "pdfjs-dist": "3.11.174", + "pdfjs-dist": "4.0.379", "process": "~0.11.10", "react": "^16.9.0", "react-dom": "^16.9.0", @@ -3189,9 +3189,9 @@ } }, "node_modules/pdfjs-dist": { - "version": "3.11.174", - "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-3.11.174.tgz", - "integrity": "sha512-TdTZPf1trZ8/UFu5Cx/GXB7GZM30LT+wWUNfsi6Bq8ePLnb+woNKtDymI2mxZYBpMbonNFqKmiz684DIfnd8dA==", + "version": "4.0.379", + "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-4.0.379.tgz", + "integrity": "sha512-6H0Gv1nna+wmrr3CakaKlZ4rbrL8hvGIFAgg4YcoFuGC0HC4B2DVjXEGTFjJEjLlf8nYi3C3/MYRcM5bNx0elA==", "dev": true, "engines": { "node": ">=18" diff --git a/package.json b/package.json index cf187e6..b1faac7 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "document-viewer-ts", - "version": "0.7.0-legacy", + "version": "1.0.0-legacy", "description": "PDF and MS Doc viewer written in TypeScript for React and vanilla JavaScript", "main": "dist/lib/index.js", - "module": "dist/es2015/index.js", - "types": "dist/es2015/index.d.ts", + "module": "dist/esnext/index.js", + "types": "dist/esnext/index.d.ts", "engines": { "node": ">=18.15.0" }, @@ -13,9 +13,8 @@ "styles" ], "scripts": { - "build": "npm run build-es2015 && npm run build-cjs", - "build-cjs": "tsc -p tsconfig.commonjs.json", - "build-es2015": "tsc -p tsconfig.json", + "build": "npm run build-esnext", + "build-esnext": "tsc -p tsconfig.json", "build-example": "npm run build && tsc -p tsconfig.example.json && webpack", "clean": "rimraf ./dist ./build", "lint:no-fix": "eslint --config='.eslintrc.js' './src/*.{ts,tsx}' './tests/*.{ts,tsx}'", @@ -68,7 +67,7 @@ "core-js": "~3.23.3", "eslint": "~8.38.0", "express": "^4.18.3", - "pdfjs-dist": "3.11.174", + "pdfjs-dist": "4.0.379", "process": "~0.11.10", "react": "^16.9.0", "react-dom": "^16.9.0", diff --git a/src/base.ts b/src/base.ts index ec65820..a5b1fc2 100644 --- a/src/base.ts +++ b/src/base.ts @@ -1,6 +1,7 @@ -import { GlobalWorkerOptions, PDFDocumentProxy, PDFPageProxy, PageViewport, getDocument, renderTextLayer } from 'pdfjs-dist/legacy/build/pdf'; +import type { PDFDocumentProxy, PDFPageProxy, PageViewport } from 'pdfjs-dist/legacy/build/pdf.mjs'; import type { TextContent } from 'pdfjs-dist/types/src/display/api'; +const { GlobalWorkerOptions, getDocument, renderTextLayer } = await import('pdfjs-dist/legacy/build/pdf.mjs'); const chevronLeft = ` `; @@ -270,7 +271,14 @@ export const renderDocument = (workerSrc: string) => (containerDiv: Element) => try { (() => globalThis)(); new File([], 'test.txt'); - GlobalWorkerOptions.workerSrc = workerSrc; + + if (typeof window !== 'undefined' && 'Worker' in window) { + GlobalWorkerOptions.workerPort = new Worker( + new URL(workerSrc, import.meta.url), + { type: 'module' } + ); + } + renderPDF(containerDiv, documentUrl); } catch (err) { renderErrorMessage(containerDiv)('Your browser does not support showing PDF previews. Click the download button to view this document.'); diff --git a/tsconfig.commonjs.json b/tsconfig.commonjs.json deleted file mode 100644 index 6620f81..0000000 --- a/tsconfig.commonjs.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "module": "commonjs", - "outDir": "./dist/lib", - }, -} diff --git a/tsconfig.example.json b/tsconfig.example.json index 992ee72..d7b550f 100644 --- a/tsconfig.example.json +++ b/tsconfig.example.json @@ -1,7 +1,6 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "module": "commonjs", "outDir": "./build", }, "include": [ diff --git a/tsconfig.json b/tsconfig.json index fec37f1..2b9e567 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,9 +8,9 @@ "jsx": "react", "lib": [ "dom", - "es2015" + "ESNext" ], - "module": "es2015", + "module": "ESNext", "moduleResolution": "node", "noEmitOnError": true, "noErrorTruncation": true, @@ -21,10 +21,10 @@ "noUnusedLocals": false, "noUnusedParameters": false, "pretty": true, - "outDir": "./dist/es2015", + "outDir": "./dist/esnext", "sourceMap": true, "strict": true, - "target": "es2015" + "target": "ESNext" }, "include": [ "src/*" diff --git a/webpack.config.js b/webpack.config.js index 70c56ae..e2b73a8 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -14,7 +14,7 @@ module.exports = { }, plugins: [ new webpack.ProvidePlugin({ - process: 'process/browser', + process: 'process/browser.js', }), ] }; \ No newline at end of file