Skip to content

Commit

Permalink
feat: add support for native ESM imports, make repository ESM-first (#…
Browse files Browse the repository at this point in the history
…2409)

* Set "type": "module" and add "exports"

* Convert Babel config to ESM

* Add our own node-gyp so that canvas can be built

* Fix parse:afm command, remove useless babel-node

* Convert Jest config and CJS Jest tests to ESM

* Convert ESLint config to JSON

* Allow extraneous dependencies in test files

* Change .size-limit config extension to .cjs

* Fix build

* Add changeset

* Add types

* Fix faux ESM yoga-layout import

* Make ESLint happy

* Remove unnecessary eslint-disable

* Fix broken imports in browserify-zlib

See browserify/browserify-zlib#45

* Fix Jest command?

* Use .cjs and .js instead of .cjs.js and .es.js respectively, copy types for each outputted file separately

* Remove direct node-gyp dependency

* Move Yoga hack to a separate file
  • Loading branch information
wojtekmaj authored Jan 18, 2024
1 parent e705d98 commit b6a14fd
Show file tree
Hide file tree
Showing 134 changed files with 645 additions and 436 deletions.
17 changes: 17 additions & 0 deletions .changeset/lazy-news-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@react-pdf/primitives': minor
'@react-pdf/stylesheet': minor
'@react-pdf/examples': minor
'@react-pdf/renderer': minor
'@react-pdf/textkit': minor
'@react-pdf/layout': minor
'@react-pdf/pdfkit': minor
'@react-pdf/png-js': minor
'@react-pdf/render': minor
'@react-pdf/svgkit': minor
'@react-pdf/image': minor
'@react-pdf/font': minor
'@react-pdf/fns': minor
---

Add support for native ESM
45 changes: 0 additions & 45 deletions .eslintrc.js

This file was deleted.

55 changes: 55 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"parser": "babel-eslint",
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": [
"plugin:react/recommended",
"airbnb",
"prettier",
"plugin:react/jsx-runtime"
],
"globals": {
"URL": false,
"BROWSER": false,
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["react", "jest"],
"rules": {
"func-names": 0,
"no-continue": 1,
"no-cond-assign": 1,
"react/prop-types": 0,
"prefer-destructuring": 1,
"no-use-before-define": 1,
"no-underscore-dangle": 0,
"prefer-object-spread": 0,
"import/no-named-as-default": 0,
"react/state-in-constructor": 0,
"react/jsx-filename-extension": 0,
"react/jsx-props-no-spreading": 0,
"react/destructuring-assignment": 0,
"import/no-unresolved": ["error", { "ignore": ["^yoga-layout"] }]
},
"overrides": [
{
"files": ["**/*.test.js", "**/tests/**/*.js", "setup.jest.js"],
"env": {
"jest": true
},
"rules": {
"import/no-extraneous-dependencies": 0
}
}
]
}
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
presets: [
[
'@babel/preset-env',
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
projects: [
'<rootDir>packages/fns',
'<rootDir>packages/font',
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"author": "Diego Muracciole <[email protected]>",
"homepage": "https://github.com/diegomura/react-pdf#readme",
"type": "module",
"repository": "[email protected]:diegomura/react-pdf.git",
"workspaces": [
"packages/*"
Expand All @@ -16,7 +17,7 @@
"bootstrap": "lerna bootstrap",
"prepublish": "lerna run build",
"lint": "eslint packages",
"test": "jest",
"test": "NODE_OPTIONS=\"--experimental-vm-modules\" jest",
"dev": "lerna run dev --scope @react-pdf/examples",
"changeset": "changeset",
"version-packages": "changeset version",
Expand All @@ -25,7 +26,6 @@
"devDependencies": {
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.7",
"@babel/node": "^7.20.7",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-decorators": "^7.20.13",
"@babel/plugin-proposal-optional-chaining": "^7.20.7",
Expand All @@ -34,6 +34,7 @@
"@babel/preset-react": "^7.18.6",
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.0",
"@jest/globals": "^29.5.0",
"@rollup/plugin-alias": "^3.1.8",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^21.0.1",
Expand Down Expand Up @@ -67,6 +68,7 @@
"react-dom": "^18.2.0",
"rimraf": "^2.6.3",
"rollup": "^2.60.1",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-ignore": "^1.0.10",
"rollup-plugin-local-resolve": "^1.0.7",
"rollup-plugin-polyfill-node": "^0.9.0",
Expand Down
8 changes: 0 additions & 8 deletions packages/examples/.eslintrc.js

This file was deleted.

8 changes: 8 additions & 0 deletions packages/examples/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": ["../../.eslintrc.json", "plugin:react/recommended"],
"rules": {
"react/prop-types": 0,
"jsx-a11y/anchor-is-valid": 0,
"react/jsx-one-expression-per-line": 0
}
}
2 changes: 1 addition & 1 deletion packages/fns/babel.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = { extends: '../../babel.config.js' };
export default { extends: '../../babel.config.js' };
2 changes: 1 addition & 1 deletion packages/fns/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
export default {
testRegex: 'tests/.*?(test)\\.js$',
};
14 changes: 11 additions & 3 deletions packages/fns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
"description": "React-pdf helper functions",
"author": "Diego Muracciole <[email protected]>",
"homepage": "https://github.com/diegomura/react-pdf#readme",
"main": "lib/index.cjs.js",
"module": "lib/index.es.js",
"type": "module",
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"exports": {
".": {
"import": "./lib/index.js",
"require": "./lib/index.cjs",
"default": "./lib/index.js"
}
},
"repository": {
"type": "git",
"url": "https://github.com/diegomura/react-pdf.git",
"directory": "packages/fns"
},
"scripts": {
"test": "jest",
"test": "NODE_OPTIONS=\"--experimental-vm-modules\" jest",
"build": "rimraf ./lib && rollup -c",
"watch": "rimraf ./lib && rollup -c -w"
},
Expand Down
5 changes: 1 addition & 4 deletions packages/fns/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ const getPlugins = () => [

const config = {
input,
output: [
getESM({ file: 'lib/index.es.js' }),
getCJS({ file: 'lib/index.cjs.js' }),
],
output: [getESM({ file: 'lib/index.js' }), getCJS({ file: 'lib/index.cjs' })],
external: getExternal(),
plugins: getPlugins(),
};
Expand Down
2 changes: 1 addition & 1 deletion packages/font/babel.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = { extends: '../../babel.config.js' };
export default { extends: '../../babel.config.js' };
18 changes: 13 additions & 5 deletions packages/font/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@
"url": "https://github.com/diegomura/react-pdf.git",
"directory": "packages/font"
},
"main": "./lib/index.cjs.js",
"module": "./lib/index.es.js",
"type": "module",
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"browser": {
"./lib/index.es.js": "./lib/index.browser.es.js",
"./lib/index.cjs.js": "./lib/index.browser.cjs.js"
"./lib/index.js": "./lib/index.browser.js",
"./lib/index.cjs": "./lib/index.browser.cjs"
},
"exports": {
".": {
"import": "./lib/index.js",
"require": "./lib/index.cjs",
"default": "./lib/index.js"
}
},
"scripts": {
"test": "jest",
"test": "NODE_OPTIONS=\"--experimental-vm-modules\" jest",
"build": "rimraf ./lib && rollup -c",
"watch": "rimraf ./lib && rollup -c -w"
},
Expand Down
9 changes: 3 additions & 6 deletions packages/font/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,16 @@ const getPlugins = ({ browser }) => [

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

const browserConfig = {
input: './src/index.js',
output: [
getESM({ file: 'lib/index.browser.es.js' }),
getCJS({ file: 'lib/index.browser.cjs.js' }),
getESM({ file: 'lib/index.browser.js' }),
getCJS({ file: 'lib/index.browser.cjs' }),
],
external,
plugins: getPlugins({ browser: true }),
Expand Down
5 changes: 5 additions & 0 deletions packages/font/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* eslint-disable */
import { jest } from '@jest/globals';
import { createRequire } from 'module';

import '../../polyfills';

const require = createRequire(import.meta.url);

global.BROWSER = false;

const customGlobal = global;
Expand Down
2 changes: 1 addition & 1 deletion packages/image/babel.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = { extends: '../../babel.config.js' };
export default { extends: '../../babel.config.js' };
2 changes: 1 addition & 1 deletion packages/image/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
automock: false,
testRegex: 'tests/.*?(test)\\.js$',
setupFiles: ['<rootDir>setupTests.js'],
Expand Down
18 changes: 13 additions & 5 deletions packages/image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
"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.cjs.js",
"module": "./lib/index.es.js",
"type": "module",
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"browser": {
"./lib/index.cjs.js": "./lib/index.browser.cjs.js",
"./lib/index.es.js": "./lib/index.browser.es.js"
"./lib/index.cjs": "./lib/index.browser.cjs",
"./lib/index.js": "./lib/index.browser.js"
},
"exports": {
".": {
"import": "./lib/index.js",
"require": "./lib/index.cjs",
"default": "./lib/index.js"
}
},
"repository": {
"type": "git",
"url": "https://github.com/diegomura/react-pdf.git",
"directory": "packages/image"
},
"scripts": {
"test": "jest",
"test": "NODE_OPTIONS=\"--experimental-vm-modules\" jest",
"build": "rimraf ./lib && rollup -c",
"watch": "rimraf ./lib && rollup -c -w"
},
Expand Down
9 changes: 3 additions & 6 deletions packages/image/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,16 @@ const getPlugins = ({ browser }) => [

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

const browserConfig = {
input,
output: [
getESM({ file: 'lib/index.browser.es.js' }),
getCJS({ file: 'lib/index.browser.cjs.js' }),
getESM({ file: 'lib/index.browser.js' }),
getCJS({ file: 'lib/index.browser.cjs' }),
],
external: getExternal({ browser: true }),
plugins: getPlugins({ browser: true }),
Expand Down
4 changes: 4 additions & 0 deletions packages/image/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* eslint-disable import/no-extraneous-dependencies */
import { jest } from '@jest/globals';
import { createRequire } from 'module';

const require = createRequire(import.meta.url);

const customGlobal = global;
customGlobal.fetch = require('jest-fetch-mock');
Expand Down
3 changes: 3 additions & 0 deletions packages/image/tests/resolve.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import fs from 'fs';
import path from 'path';
import url from 'url';

import resolveImage, { IMAGE_CACHE } from '../src/resolve';

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

const jpgImageUrl = 'https://react-pdf.org/static/images/quijote1.jpg';
const pngImageUrl = 'https://react-pdf.org/static/images/quijote2.png';
const jpgLogalPath = path.join(__dirname, './assets/test.jpg');
Expand Down
2 changes: 1 addition & 1 deletion packages/layout/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
export default {
extends: '../../babel.config.js',
};
Loading

0 comments on commit b6a14fd

Please sign in to comment.