Skip to content

Commit 19a6117

Browse files
authored
Merge pull request #51 from andriivitiv/76303-upgrade-react-pdf-and-migrate-to-ESM
Upgrade `react-pdf` and migrate to ESM
2 parents 0790d12 + cfe5b4a commit 19a6117

16 files changed

Lines changed: 1385 additions & 1738 deletions

.eslintrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
"parserOptions": {
6565
"project": "./tsconfig.json"
6666
}
67+
},
68+
{
69+
"files": ["*"],
70+
"rules": {
71+
"import/extensions": ["error", "ignorePackages"]
72+
}
6773
}
6874
],
6975
"settings": {

.prettierrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
tabWidth: 4,
33
singleQuote: true,
44
trailingComma: 'all',
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
module.exports = {
1+
import path from 'path';
2+
import {fileURLToPath} from 'url';
3+
4+
const filename = fileURLToPath(import.meta.url);
5+
const dirname = path.dirname(filename);
6+
7+
export default {
28
root: true,
39
env: {
410
browser: true,
@@ -9,7 +15,7 @@ module.exports = {
915
parserOptions: {
1016
ecmaVersion: 'latest',
1117
sourceType: 'module',
12-
tsconfigRootDir: __dirname,
18+
tsconfigRootDir: dirname,
1319
},
1420
extends: ['eslint:recommended', 'airbnb', 'plugin:@typescript-eslint/recommended', 'plugin:react/jsx-runtime', 'plugin:jsx-a11y/recommended', 'plugin:react/recommended', 'prettier'],
1521
plugins: ['@typescript-eslint', 'react', 'jsx-a11y'],
@@ -65,6 +71,12 @@ module.exports = {
6571
project: './tsconfig.json',
6672
},
6773
},
74+
{
75+
files: ['*'],
76+
rules: {
77+
'import/extensions': ['error', 'ignorePackages'],
78+
},
79+
},
6880
],
6981
settings: {
7082
react: {

example/package-lock.json

Lines changed: 461 additions & 460 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"description": "react-fast-pdf",
55
"main": "./src/index.js",
6+
"type": "module",
67
"files": [
78
"dist",
89
"src"
@@ -61,7 +62,7 @@
6162
"style-loader": "^3.3.3",
6263
"ts-loader": "^9.4.4",
6364
"typescript": "^5.1.6",
64-
"webpack": "^5.88.2",
65+
"webpack": "^5.104.1",
6566
"webpack-cli": "^5.1.4",
6667
"webpack-dev-server": "^4.15.1"
6768
}

example/src/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import './index.css';
55
function App() {
66
const [file, setFile] = useState<string | null>(null);
77

8+
// `.default` is required when referencing the legacy CJS package.
9+
const packageName = ('default' in ReactFastPDF ? (ReactFastPDF.default as {PackageName: string}) : ReactFastPDF).PackageName;
10+
811
return (
912
<main className="container">
10-
<h1 className="title">Hello, I am {ReactFastPDF.PackageName}!</h1>
13+
<h1 className="title">Hello, I am {packageName}!</h1>
1114

1215
{file ? (
1316
<>

example/src/pdf.worker.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'pdfjs-dist/build/pdf.worker.min.mjs';

example/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"compilerOptions": {
44
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
55
"jsx": "react" /* Specify what JSX code is generated. */,
6-
"module": "commonjs" /* Specify what module code is generated. */,
6+
"module": "nodenext" /* Specify what module code is generated. */,
77
"rootDir": "./src" /* Specify the root folder within your source files. */,
88
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
99
"declarationMap": true /* Create sourcemaps for d.ts files. */,

example/webpack.config.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
/* eslint-disable @typescript-eslint/no-var-requires */
2-
const path = require('path');
3-
const HtmlWebpackPlugin = require('html-webpack-plugin');
4-
const CopyWebpackPlugin = require('copy-webpack-plugin');
1+
import path from 'path';
2+
import {fileURLToPath} from 'url';
3+
import HtmlWebpackPlugin from 'html-webpack-plugin';
4+
import CopyWebpackPlugin from 'copy-webpack-plugin';
55

6-
const cMapsDir = path.join(path.dirname(require.resolve('pdfjs-dist/package.json')), 'cmaps');
6+
const filename = fileURLToPath(import.meta.url);
7+
const dirname = path.dirname(filename);
78

8-
module.exports = {
9-
entry: path.join(__dirname, 'src', 'index.js'),
9+
const pdfjsPkgUrl = import.meta.resolve('pdfjs-dist/package.json');
10+
const pdfjsPkgPath = fileURLToPath(pdfjsPkgUrl);
11+
12+
const cMapsDir = path.join(path.dirname(pdfjsPkgPath), 'cmaps');
13+
14+
export default {
15+
entry: path.join(dirname, 'src', 'index.js'),
1016
output: {
11-
path: path.resolve(__dirname, 'dist'),
17+
path: path.resolve(dirname, 'dist'),
1218
filename: 'bundle.js',
1319
},
1420
resolve: {
15-
modules: [path.join(__dirname, 'src'), 'node_modules'],
21+
modules: [path.join(dirname, 'src'), 'node_modules'],
1622
alias: {
17-
react: path.join(__dirname, 'node_modules', 'react'),
23+
react: path.join(dirname, 'node_modules', 'react'),
1824
},
1925
extensions: ['.tsx', '.ts', '.js'],
2026
},
@@ -53,7 +59,7 @@ module.exports = {
5359
},
5460
plugins: [
5561
new HtmlWebpackPlugin({
56-
template: path.join(__dirname, 'public', 'index.html'),
62+
template: path.join(dirname, 'public', 'index.html'),
5763
}),
5864
new CopyWebpackPlugin({
5965
patterns: [

0 commit comments

Comments
 (0)