From c77b5ff3371506ede45f2410c5000b9a5c2049bc Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 26 Mar 2024 13:13:40 -0400 Subject: [PATCH 1/4] 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 From a84ccbdcd372fe854606984a0bd4c50edfb52080 Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 7 Jun 2024 13:06:26 -0400 Subject: [PATCH 2/4] upgrade to latest document-viewer-ts --- package-lock.json | 20 ++++++++++---------- package.json | 2 +- src/base.ts | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 99efb12..833481d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ "core-js": "~3.23.3", "eslint": "~8.38.0", "express": "^4.18.3", - "pdfjs-dist": "4.0.379", + "pdfjs-dist": "4.3.136", "process": "~0.11.10", "react": "^16.9.0", "react-dom": "^16.9.0", @@ -3178,27 +3178,27 @@ "node": ">=8" } }, - "node_modules/path2d-polyfill": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/path2d-polyfill/-/path2d-polyfill-2.0.1.tgz", - "integrity": "sha512-ad/3bsalbbWhmBo0D6FZ4RNMwsLsPpL6gnvhuSaU5Vm7b06Kr5ubSltQQ0T7YKsiJQO+g22zJ4dJKNTXIyOXtA==", + "node_modules/path2d": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/path2d/-/path2d-0.2.0.tgz", + "integrity": "sha512-KdPAykQX6kmLSOO6Jpu2KNcCED7CKjmaBNGGNuctOsG0hgYO1OdYQaan6cYXJiG0WmXOwZZPILPBimu5QAIw3A==", "dev": true, "optional": true, "engines": { - "node": ">=8" + "node": ">=6" } }, "node_modules/pdfjs-dist": { - "version": "4.0.379", - "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-4.0.379.tgz", - "integrity": "sha512-6H0Gv1nna+wmrr3CakaKlZ4rbrL8hvGIFAgg4YcoFuGC0HC4B2DVjXEGTFjJEjLlf8nYi3C3/MYRcM5bNx0elA==", + "version": "4.3.136", + "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-4.3.136.tgz", + "integrity": "sha512-gzfnt1qc4yA+U46golPGYtU4WM2ssqP2MvFjKga8GEKOrEnzRPrA/9jogLLPYHiA3sGBPJ+p7BdAq+ytmw3jEg==", "dev": true, "engines": { "node": ">=18" }, "optionalDependencies": { "canvas": "^2.11.2", - "path2d-polyfill": "^2.0.1" + "path2d": "^0.2.0" } }, "node_modules/picocolors": { diff --git a/package.json b/package.json index b1faac7..d324ffc 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "core-js": "~3.23.3", "eslint": "~8.38.0", "express": "^4.18.3", - "pdfjs-dist": "4.0.379", + "pdfjs-dist": "4.3.136", "process": "~0.11.10", "react": "^16.9.0", "react-dom": "^16.9.0", diff --git a/src/base.ts b/src/base.ts index 9fa915e..d2b6e43 100644 --- a/src/base.ts +++ b/src/base.ts @@ -37,7 +37,7 @@ const scaleTextLayer = async ( textContentSource: textContent, container: textLayerFragment, viewport: vs - }).promise; + })?.promise; textLayerDiv.appendChild(textLayerFragment); }; From 2a8743cb4864c35c800482f9d39bc93eba2eb7e2 Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 7 Jun 2024 13:24:21 -0400 Subject: [PATCH 3/4] top-level await no longer needed --- example/example.ts | 2 +- package.json | 8 ++++---- src/base.ts | 11 ++--------- tsconfig.json | 8 ++++---- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/example/example.ts b/example/example.ts index 365ce9f..6b14448 100644 --- a/example/example.ts +++ b/example/example.ts @@ -1,3 +1,3 @@ -import { init } from '../dist/esnext'; +import { init } from '../dist/es2015'; init('http://localhost:8080/pdf.worker.min.mjs'); \ No newline at end of file diff --git a/package.json b/package.json index d324ffc..6a9dcca 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,8 @@ "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/esnext/index.js", - "types": "dist/esnext/index.d.ts", + "module": "dist/es2015/index.js", + "types": "dist/es2015/index.d.ts", "engines": { "node": ">=18.15.0" }, @@ -13,8 +13,8 @@ "styles" ], "scripts": { - "build": "npm run build-esnext", - "build-esnext": "tsc -p tsconfig.json", + "build": "npm run build-es2015", + "build-es2015": "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}'", diff --git a/src/base.ts b/src/base.ts index d2b6e43..5fa6f38 100644 --- a/src/base.ts +++ b/src/base.ts @@ -1,7 +1,6 @@ -import type { PDFDocumentProxy, PDFPageProxy, PageViewport } from 'pdfjs-dist/legacy/build/pdf.mjs'; +import { GlobalWorkerOptions, PDFDocumentProxy, PDFPageProxy, PageViewport, getDocument, renderTextLayer,} 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 = ` `; @@ -271,13 +270,7 @@ export const renderDocument = (workerSrc: string) => (containerDiv: Element) => try { (() => globalThis)(); new File([], 'test.txt'); - - if (typeof window !== 'undefined' && 'Worker' in window) { - GlobalWorkerOptions.workerPort = new Worker( - new URL(workerSrc, import.meta.url), - { type: 'module' } - ); - } + GlobalWorkerOptions.workerSrc = workerSrc; renderPDF(containerDiv, documentUrl); } catch (err) { diff --git a/tsconfig.json b/tsconfig.json index 2b9e567..fec37f1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,9 +8,9 @@ "jsx": "react", "lib": [ "dom", - "ESNext" + "es2015" ], - "module": "ESNext", + "module": "es2015", "moduleResolution": "node", "noEmitOnError": true, "noErrorTruncation": true, @@ -21,10 +21,10 @@ "noUnusedLocals": false, "noUnusedParameters": false, "pretty": true, - "outDir": "./dist/esnext", + "outDir": "./dist/es2015", "sourceMap": true, "strict": true, - "target": "ESNext" + "target": "es2015" }, "include": [ "src/*" From 6cf9ab7737f3ffed336d036acabfceb901944458 Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 7 Jun 2024 13:25:50 -0400 Subject: [PATCH 4/4] fix some formatting --- src/base.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/base.ts b/src/base.ts index 5fa6f38..069a87e 100644 --- a/src/base.ts +++ b/src/base.ts @@ -1,4 +1,4 @@ -import { GlobalWorkerOptions, PDFDocumentProxy, PDFPageProxy, PageViewport, getDocument, renderTextLayer,} from 'pdfjs-dist/legacy/build/pdf.mjs'; +import { GlobalWorkerOptions, PDFDocumentProxy, PDFPageProxy, PageViewport, getDocument, renderTextLayer } from 'pdfjs-dist/legacy/build/pdf.mjs'; import type { TextContent } from 'pdfjs-dist/types/src/display/api'; const chevronLeft = ` @@ -271,7 +271,6 @@ export const renderDocument = (workerSrc: string) => (containerDiv: Element) => (() => globalThis)(); new File([], 'test.txt'); GlobalWorkerOptions.workerSrc = workerSrc; - renderPDF(containerDiv, documentUrl); } catch (err) { renderErrorMessage(containerDiv)('Your browser does not support showing PDF previews. Click the download button to view this document.');