react-pdf: ESM contains server code, doesn't provide named exports
Describe the bug When using Vite to package a React app, we hit some issues because:
- The ESM that
@react-pdf/rendererprovides doesn’t seem to provide the same exports as the CJS module. - There is server code included in the ESM, but this library is a browser module.
To Reproduce You can reproduce with this repository: https://github.com/thekevinbrown/vite-react-pdf-reproduction
Expected behavior I expected React PDF to be able to be bundled and used in an ESM context.
Screenshots
Uncaught ReferenceError: global is not defined
at node_modules/blob/index.js (@react-pdf_renderer.js:554)
at __require (chunk-KVFJW2XH.js:12)
at node_modules/blob-stream/index.js (@react-pdf_renderer.js:616)
at __require (chunk-KVFJW2XH.js:12)
at @react-pdf_renderer.js:126368
Desktop (please complete the following information):
- OS: MacOS
- Browser: Chrome
- React-pdf version: “@react-pdf/renderer”: “^2.0.12”,
Additional Notes
An issue was logged with Vite here: https://github.com/vitejs/vite/issues/3405
This may be related to this issue as well: #1029
I’d be happy to help with a PR if one is welcome, just wanted to check to ensure I wasn’t duplicating effort and that this was something you’d accept a PR for.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 8
- Comments: 33 (12 by maintainers)
Commits related to this issue
- Apply import fix for vite / ESM bundling As suggested in https://github.com/diegomura/react-pdf/issues/1317#issuecomment-855230597 — committed to carlobeltrame/ecamp3 by carlobeltrame 3 years ago
- Apply import fix for vite / ESM bundling As suggested in https://github.com/diegomura/react-pdf/issues/1317#issuecomment-855230597 — committed to carlobeltrame/ecamp3 by carlobeltrame 3 years ago
- Apply import fix for vite / ESM bundling As suggested in https://github.com/diegomura/react-pdf/issues/1317#issuecomment-855230597 — committed to carlobeltrame/ecamp3 by carlobeltrame 3 years ago
- Apply import fix for vite / ESM bundling As suggested in https://github.com/diegomura/react-pdf/issues/1317#issuecomment-855230597 — committed to carlobeltrame/ecamp3 by carlobeltrame 3 years ago
- Apply import fix for vite / ESM bundling As suggested in https://github.com/diegomura/react-pdf/issues/1317#issuecomment-855230597 — committed to carlobeltrame/ecamp3 by carlobeltrame 3 years ago
- Apply import fix for vite / ESM bundling As suggested in https://github.com/diegomura/react-pdf/issues/1317#issuecomment-855230597 — committed to carlobeltrame/ecamp3 by carlobeltrame 3 years ago
- Apply import fix for vite / ESM bundling As suggested in https://github.com/diegomura/react-pdf/issues/1317#issuecomment-855230597 — committed to carlobeltrame/ecamp3 by carlobeltrame 3 years ago
- Apply import fix for vite / ESM bundling As suggested in https://github.com/diegomura/react-pdf/issues/1317#issuecomment-855230597 — committed to carlobeltrame/ecamp3 by carlobeltrame 3 years ago
- Simplify imports Works around missing named exports in react-pdf, see https://github.com/diegomura/react-pdf/issues/1317 — committed to carlobeltrame/ecamp3 by carlobeltrame 2 years ago
- Simplify imports Works around missing named exports in react-pdf, see https://github.com/diegomura/react-pdf/issues/1317 — committed to carlobeltrame/ecamp3 by carlobeltrame 2 years ago
- Revert f37ea34b47935f34246e50219c5e94f2216b7663 React-pdf now removed the @react-pdf/unicode-properties package entirely, switching back to the mainline foliojs unicode-properties package. So our fi... — committed to carlobeltrame/vite-plugin-shim-react-pdf by carlobeltrame 2 years ago
Alrighty, finally found a set of shims and workarounds that make it work, both in Vite Dev and Production builds.
node_modules. Yuck.)readable-streamwhich is needed for Node streams in the browser works fine in dev mode, but in a production build it fails because of circular imports which Rollup doesn’t handle particularly well, but also probably shouldn’t be there in the first place in ESM land (https://github.com/rollup/rollup-plugin-commonjs/issues/293, https://github.com/nodejs/readable-stream/issues/348). I changed the package to use a Registry pattern so it’d work (https://github.com/techinsite/readable-stream) and published it as vite-compatible-readable-stream.node_modulesI bundled that up into vite-plugin-shim-react-pdf.So, if you’re coming here from Google, you do the following in your Vite project:
Then in
vite.config.js:Then you’ll also need to use the default import because of the missing exports. E.g.
Instead of:
You have to do:
React PDF also didn’t have a development project I could use to test changes, so I made a really rough one out of the examples: https://github.com/TechInSite/react-pdf-development-harness
@diegomura I’d be happy to transfer ownership of the development harness repo to you if you want to have it as part of the React PDF project moving forward. Could use quite a bit of CSS work, but I had enough to get through with the above that I didn’t put any time into making it pretty at all.
I created PR #1891 to fix this issue, please have a look 😃
I don’t think so. As far as I am aware, react-pdf is still broken in web bundlers newer than Webpack 4, which hasn’t seen any development in almost a year now: https://github.com/webpack/webpack/tree/webpack-4
The fixes from the shim in index.js and shim-react-pdf.js should be applied directly in the react-pdf code base, while making sure that server side operation of react-pdf still works. I can open a PR if @thekevinbrown does not have time for it, but I do not have the know-how to test whether it still works in a node.js environment.
This has now been released, sorry for the delay!
I’m having the same issue as well, and the shim of @thekevinbrown worked wonders, even for my special case of packing the react component into a webcomponent and using it inside a Vue.js app! Thanks a lot for that! Let’s hope this can be fixed directly in react-pdf in the future.
Edit: – Removed as I was using a older version, so comments were obsolete.—
Many thanks to the maintainers of the repo 🙌
Yeah, and even with my shim it still doesn’t provide named exports and you still have to do:
I’d say we should break this into two pieces that are separate issues:
npm i @react-pdf/renderer, then run in Vite, Webpack 5, and Node without issue.@carlobeltrame If you have time to work on a PR, we’re using React PDF in both Vite and Node, so I can test server side (and create an example project) for you if that helps.
I worked around the import issue by adding a file “reactPdf.js” like follows to my project that “converts” the default export to named exports:
And then I can import from that file normally:
This always loads all the primitives from react-pdf, but as @diegomura stated in #1119, that is not a big concern, since they are mostly just plain strings.
I’m have the same issue, I was generating a pdf using pdfRenderer in React and the @thekevinbrown solution works, I’m very thankful =D
Anyway, the react-pdf needs to fix the problem