Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compatibility with modern web bundlers and browsers #1891

Merged
merged 29 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
11018ce
Browser compatibility for renderer package
carlobeltrame Jun 16, 2022
50af00b
Also compile textkit package as ES6 module and fix imports
carlobeltrame Jun 16, 2022
cf5816b
Also compile fns package as ES6 module
carlobeltrame Jun 16, 2022
c0a414d
Also compile yoga package as ES6 module
carlobeltrame Jun 16, 2022
c364fb7
Also compile stylesheet package as ES6 module and fix imports
carlobeltrame Jun 16, 2022
941a4be
Also compile image package as ES6 module
carlobeltrame Jun 16, 2022
64b9a0b
Also compile font package as ES6 module
carlobeltrame Jun 16, 2022
955bac4
Update restructure to version 2.0.1
carlobeltrame Jun 16, 2022
a9e8bb1
Use a more modern style of importing the restructure package
carlobeltrame Jun 16, 2022
73cabe3
Add node polyfills for unicode-properties package in the browser
carlobeltrame Jun 16, 2022
81d1ec3
Add node polyfills for pdfkit package in the browser
carlobeltrame Jun 16, 2022
df8827f
fixup! Browser compatibility for renderer package
carlobeltrame Jun 17, 2022
b036178
Fix node polyfills for renderer package in the browser
carlobeltrame Jun 17, 2022
9115d13
Polyfill zlib in the browser
carlobeltrame Jun 17, 2022
f6e3d7e
Polyfill Buffer in our own code
carlobeltrame Jun 17, 2022
527ef2c
Polyfill Buffer in our own code
carlobeltrame Jun 17, 2022
9389d1b
Webpack apparently prefers relative paths in the browser field
carlobeltrame Jun 29, 2022
a6a4ab4
Increase size limit
carlobeltrame Jun 29, 2022
f2a04e1
chore: remove unicode-properties
diegomura Jul 2, 2022
c42ab13
fix: fns rollup config
diegomura Jul 4, 2022
96f45b5
Improve package size by externalizing pako
carlobeltrame Jul 5, 2022
8974f19
Use the global babel config in all packages
carlobeltrame Jul 5, 2022
7174c9a
Remove webpack 5 shims now that they are no longer needed
carlobeltrame Jul 6, 2022
f4e1ed5
chore: remove unecessary polyfills on font package
diegomura Jul 11, 2022
f8f0b98
chore: restore import formatting
diegomura Jul 11, 2022
238b666
chore: no need to make textkit browser separate bundle
diegomura Jul 11, 2022
603be3d
chore: remove unecessary polyfills on renderer package
diegomura Jul 11, 2022
50dbbcf
chore: don't filter blob-stream anymore
diegomura Jul 11, 2022
7f866a9
chore: add changeset
diegomura Jul 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/famous-glasses-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@react-pdf/fns': major
'@react-pdf/pdfkit': major
'@react-pdf/stylesheet': major
'@react-pdf/yoga': major
'@react-pdf/font': minor
'@react-pdf/image': minor
'@react-pdf/layout': minor
'@react-pdf/png-js': minor
'@react-pdf/renderer': minor
'@react-pdf/textkit': minor
---

feat: compatibility with modern web bundlers and browsers
37 changes: 0 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,6 @@ This package is used to _create_ PDFs using React. If you wish to _display_ exis
yarn add @react-pdf/renderer
```

### Webpack 5

Webpack 5 doesn't include node shims automatically anymore and we must opt-in to all shims we want. To do this we have to add a few dependencies to our project:

```sh
yarn add process browserify-zlib stream-browserify util buffer assert
```

after the modules are installed, we need to adjust our `webpack.config` file:

```js
const webpack = require("webpack");

module.exports = {
/* ... */

resolve: {
fallback: {
process: require.resolve("process/browser"),
zlib: require.resolve("browserify-zlib"),
stream: require.resolve("stream-browserify"),
util: require.resolve("util"),
buffer: require.resolve("buffer"),
assert: require.resolve("assert"),
}
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
process: "process/browser",
}),
]

/* ... */
}
```

## How it works

```jsx
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"rollup": "^2.60.1",
"rollup-plugin-ignore": "^1.0.10",
"rollup-plugin-local-resolve": "^1.0.7",
"rollup-plugin-polyfill-node": "^0.9.0",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-terser": "^7.0.2"
},
Expand Down
7 changes: 4 additions & 3 deletions packages/fns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
"description": "React-pdf helper functions",
"author": "Diego Muracciole <[email protected]>",
"homepage": "https://github.com/diegomura/react-pdf#readme",
"main": "lib/index.js",
"main": "lib/index.cjs.js",
"module": "lib/index.es.js",
"repository": {
"type": "git",
"url": "https://github.com/diegomura/react-pdf.git",
"directory": "packages/fns"
},
"scripts": {
"test": "jest",
"build": "rimraf ./lib && babel src --out-dir lib",
"watch": "rimraf ./lib && babel src --out-dir lib --watch"
"build": "rimraf ./lib && rollup -c",
"watch": "rimraf ./lib && rollup -c -w"
},
"files": [
"lib"
Expand Down
38 changes: 38 additions & 0 deletions packages/fns/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import babel from '@rollup/plugin-babel';
import pkg from './package.json';

const cjs = {
exports: 'named',
format: 'cjs',
};

const esm = {
format: 'es',
};

const getCJS = override => Object.assign({}, cjs, override);
const getESM = override => Object.assign({}, esm, override);

const input = 'src/index.js';

const getExternal = () => [/@babel\/runtime/];

const getPlugins = () => [
babel({
babelrc: true,
babelHelpers: 'runtime',
exclude: 'node_modules/**',
}),
];

const config = {
input,
output: [
getESM({ file: 'lib/index.es.js' }),
getCJS({ file: 'lib/index.cjs.js' }),
],
external: getExternal(),
plugins: getPlugins(),
};

export default config;
35 changes: 8 additions & 27 deletions packages/font/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import babel from '@rollup/plugin-babel';
import replace from '@rollup/plugin-replace';
import sourceMaps from 'rollup-plugin-sourcemaps';

import pkg from './package.json';

const cjs = {
exports: 'named',
format: 'cjs',
exports: 'named',
};

const esm = {
Expand All @@ -16,6 +14,12 @@ const esm = {
const getCJS = override => Object.assign({}, cjs, override);
const getESM = override => Object.assign({}, esm, override);

const babelConfig = () => ({
babelrc: true,
exclude: 'node_modules/**',
babelHelpers: 'runtime',
});

const external = [
'@babel/runtime/regenerator',
'@babel/runtime/helpers/extends',
Expand All @@ -24,31 +28,8 @@ const external = [
...Object.keys(pkg.dependencies),
];

const babelConfig = ({ browser }) => ({
babelrc: false,
exclude: 'node_modules/**',
babelHelpers: 'runtime',
presets: [
[
'@babel/preset-env',
{
loose: true,
modules: false,
...(browser
? { targets: { browsers: 'last 2 versions' } }
: { targets: { node: '12' } }),
},
],
],
plugins: [
['@babel/plugin-transform-runtime', { version: '^7.16.4' }],
['@babel/plugin-proposal-class-properties', { loose: true }],
],
});

const getPlugins = ({ browser }) => [
sourceMaps(),
babel(babelConfig({ browser })),
babel(babelConfig()),
replace({
preventAssignment: true,
values: {
Expand Down
10 changes: 6 additions & 4 deletions packages/image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
"description": "Parses the images in png or jpeg format for react-pdf document",
"author": "Diego Muracciole <[email protected]>",
"homepage": "https://github.com/diegomura/react-pdf#readme",
"main": "lib/index.js",
"main": "./lib/index.cjs.js",
"module": "./lib/index.es.js",
"browser": {
"./lib/index.cjs.js": "./lib/index.browser.cjs.js",
"./lib/index.es.js": "./lib/index.browser.es.js"
},
"repository": {
"type": "git",
"url": "https://github.com/diegomura/react-pdf.git",
"directory": "packages/image"
},
"browser": {
"./lib/index.js": "./lib/index.browser.js"
},
"scripts": {
"test": "jest",
"build": "rimraf ./lib && rollup -c",
Expand Down
105 changes: 50 additions & 55 deletions packages/image/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,70 @@
import babel from '@rollup/plugin-babel';
import replace from '@rollup/plugin-replace';
import ignore from 'rollup-plugin-ignore';
import sourceMaps from 'rollup-plugin-sourcemaps';

import pkg from './package.json';
import nodePolyfills from 'rollup-plugin-polyfill-node';

const cjs = {
format: 'cjs',
exports: 'named',
};

const esm = {
format: 'es',
};

const external = [
const getCJS = override => Object.assign({}, cjs, override);
const getESM = override => Object.assign({}, esm, override);

const input = './src/index.js';

const babelConfig = () => ({
babelrc: true,
exclude: 'node_modules/**',
babelHelpers: 'runtime',
});

const getExternal = ({ browser }) => [
'@babel/runtime/helpers/asyncToGenerator',
'@babel/runtime/regenerator',
...Object.keys(pkg.dependencies),
...(browser ? [] : ['fs', 'path', 'url']),
];

const babelConfig = ({ browser }) => ({
babelrc: false,
exclude: 'node_modules/**',
babelHelpers: 'runtime',
presets: [
[
'@babel/preset-env',
{
loose: true,
modules: false,
...(browser
? { targets: { browsers: 'last 2 versions' } }
: { targets: { node: '12' } }),
},
],
],
plugins: [
['@babel/plugin-transform-runtime', { version: '^7.16.4' }],
['@babel/plugin-proposal-class-properties', { loose: true }],
],
});
const getPlugins = ({ browser }) => [
babel(babelConfig()),
replace({
preventAssignment: true,
values: { BROWSER: JSON.stringify(browser) },
}),
...(browser
? [
ignore(['fs', 'path', 'url']),
nodePolyfills({
include: [/node_modules\/.+\.js/, /\/image\/src\/.*\.js/],
}),
]
: []),
];

const serverConfig = {
input: './src/index.js',
output: {
format: 'cjs',
file: 'lib/index.js',
exports: 'named',
sourcemap: true,
},
external: external.concat(['fs', 'path', 'url']),
plugins: [
sourceMaps(),
babel(babelConfig({ browser: false })),
replace({
preventAssignment: true,
values: { BROWSER: JSON.stringify(false) },
}),
input,
output: [
getESM({ file: 'lib/index.es.js' }),
getCJS({ file: 'lib/index.cjs.js' }),
],
external: getExternal({ browser: false }),
plugins: getPlugins({ browser: false }),
};

const browserConfig = {
input: './src/index.js',
output: {
format: 'cjs',
file: 'lib/index.browser.js',
exports: 'named',
sourcemap: true,
},
external,
plugins: [
sourceMaps(),
babel(babelConfig({ browser: true })),
replace({
preventAssignment: true,
values: { BROWSER: JSON.stringify(true) },
}),
ignore(['fs', 'path', 'url']),
input,
output: [
getESM({ file: 'lib/index.browser.es.js' }),
getCJS({ file: 'lib/index.browser.cjs.js' }),
],
external: getExternal({ browser: true }),
plugins: getPlugins({ browser: true }),
};

export default [serverConfig, browserConfig];
3 changes: 1 addition & 2 deletions packages/layout/src/steps/resolvePageSizes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import flatten from '@react-pdf/stylesheet/lib/flatten';

import { flatten } from '@react-pdf/stylesheet';
import getPageSize from '../page/getSize';

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/pdfkit/.babelrc.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/pdfkit/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: '../../babel.config.js' };
8 changes: 5 additions & 3 deletions packages/pdfkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "@react-pdf/pdfkit",
"version": "2.4.0",
"description": "A PDF generation library for Node.js",
"main": "lib/pdfkit.cjs.js",
"module": "lib/pdfkit.es.js",
"main": "./lib/pdfkit.cjs.js",
"module": "./lib/pdfkit.es.js",
"repository": {
"type": "git",
"url": "https://github.com/diegomura/react-pdf.git",
Expand All @@ -30,8 +30,10 @@
"dependencies": {
"@babel/runtime": "^7.16.4",
"@react-pdf/png-js": "^2.1.0",
"browserify-zlib": "^0.2.0",
"crypto-js": "^4.0.0",
"fontkit": "^2.0.2"
"fontkit": "^2.0.2",
"vite-compatible-readable-stream": "^3.6.1"
},
"devDependencies": {
"iconv-lite": "^0.4.13"
Expand Down
Loading