Skip to content

Commit

Permalink
upgrade pdfjs to version 4
Browse files Browse the repository at this point in the history
  • Loading branch information
SuttonKyle committed Mar 26, 2024
1 parent 6bbcf04 commit c77b5ff
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 30 deletions.
4 changes: 2 additions & 2 deletions example/example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { init } from '../dist/es2015';
import { init } from '../dist/esnext';

init('http://localhost:8080/pdf.worker.min.js');
init('http://localhost:8080/pdf.worker.min.mjs');
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand All @@ -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}'",
Expand Down Expand Up @@ -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",
Expand Down
12 changes: 10 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -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 = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chevron-left" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z"/>
</svg>`;
Expand Down Expand Up @@ -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.');
Expand Down
7 changes: 0 additions & 7 deletions tsconfig.commonjs.json

This file was deleted.

1 change: 0 additions & 1 deletion tsconfig.example.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "./build",
},
"include": [
Expand Down
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"jsx": "react",
"lib": [
"dom",
"es2015"
"ESNext"
],
"module": "es2015",
"module": "ESNext",
"moduleResolution": "node",
"noEmitOnError": true,
"noErrorTruncation": true,
Expand All @@ -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/*"
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
},
plugins: [
new webpack.ProvidePlugin({
process: 'process/browser',
process: 'process/browser.js',
}),
]
};

0 comments on commit c77b5ff

Please sign in to comment.