Skip to content

Commit 623b623

Browse files
committed
[breaking] Create ESM builds (#429)
* Create ESM builds * v4.2.0-beta * Update documentation for ES6 modules
1 parent 2ed370b commit 623b623

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

.babelrc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@
66
"plugins": [
77
"@babel/transform-runtime",
88
"@babel/proposal-class-properties"
9-
]
9+
],
10+
"env": {
11+
"production-esm": {
12+
"presets": [
13+
["@babel/env", { "modules": false }],
14+
"@babel/react"
15+
],
16+
"plugins": [
17+
["@babel/transform-runtime", { "useESModules": true }],
18+
"@babel/proposal-class-properties"
19+
]
20+
}
21+
}
1022
}

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,23 @@ It is crucial for performance to use PDF.js worker whenever possible. This ensur
8484
Instead of directly importing/requiring `'react-pdf'`, import it like so:
8585

8686
```js
87-
import { Document } from 'react-pdf/dist/entry.webpack';
87+
// using ES6 modules
88+
import { Document } from 'react-pdf/dist/esm/entry.webpack';
89+
90+
// using CommonJS modules
91+
import { Document } from 'react-pdf/dist/umd/entry.webpack';
8892
```
8993

9094
#### Parcel
9195

9296
Instead of directly importing/requiring `'react-pdf'`, import it like so:
9397

9498
```js
95-
import { Document } from 'react-pdf/dist/entry.parcel';
99+
// using ES6 modules
100+
import { Document } from 'react-pdf/dist/esm/entry.parcel';
101+
102+
// using CommonJS modules
103+
import { Document } from 'react-pdf/dist/umd/entry.parcel';
96104
```
97105

98106
#### Create React App
@@ -115,7 +123,11 @@ pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/$
115123
If you want to use annotations (e.g. links) in PDFs rendered by React-PDF, then you would need to include stylesheet necessary for annotations to be correctly displayed like so:
116124

117125
```js
118-
import 'react-pdf/dist/Page/AnnotationLayer.css';
126+
// using ES6 modules
127+
import 'react-pdf/dist/esm/Page/AnnotationLayer.css';
128+
129+
// using CommonJS modules
130+
import 'react-pdf/dist/umd/Page/AnnotationLayer.css';
119131
```
120132

121133
### Support for non-latin characters

copy-styles.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
const fs = require('fs');
22

3-
fs.copyFile('src/Page/AnnotationLayer.css', 'dist/Page/AnnotationLayer.css', (error) => {
3+
fs.copyFile('src/Page/AnnotationLayer.css', 'dist/esm/Page/AnnotationLayer.css', (error) => {
44
if (error) {
55
throw error;
66
}
77
// eslint-disable-next-line no-console
8-
console.log('AnnotationLayer.css copied successfully.');
8+
console.log('AnnotationLayer.css copied successfully to ESM build.');
9+
});
10+
11+
fs.copyFile('src/Page/AnnotationLayer.css', 'dist/umd/Page/AnnotationLayer.css', (error) => {
12+
if (error) {
13+
throw error;
14+
}
15+
// eslint-disable-next-line no-console
16+
console.log('AnnotationLayer.css copied successfully to UMD build.');
917
});

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
"name": "react-pdf",
33
"version": "4.2.0",
44
"description": "Display PDFs in your React app as easily as if they were images.",
5-
"main": "dist/entry.js",
5+
"main": "dist/umd/entry.js",
6+
"module": "dist/esm/entry.js",
67
"source": "src/entry.js",
78
"scripts": {
8-
"build": "yarn build-js && yarn copy-styles",
9-
"build-js": "babel src -d dist --ignore **/*.spec.js,**/*.spec.jsx",
9+
"build": "yarn build-js-all && yarn copy-styles",
10+
"build-js-all": "yarn build-js-esm && yarn build-js-umd",
11+
"build-js-esm": "BABEL_ENV=production-esm babel src -d dist/esm --ignore **/*.spec.js,**/*.spec.jsx",
12+
"build-js-umd": "BABEL_ENV=production-umd babel src -d dist/umd --ignore **/*.spec.js,**/*.spec.jsx",
1013
"clean": "rimraf dist",
1114
"copy-styles": "node ./copy-styles.js",
1215
"jest": "jest",

0 commit comments

Comments
 (0)