exogee-technology/vite-plugin-shim-react-pdf

Buffer not defined

aclec opened this issue · 5 comments

aclec commented

Hi,

I recently got the following error. Is this something to add in the shim?


Uncaught ReferenceError: Buffer is not defined
    node_modules react-pdf/unicode-properties/lib/unicode-properties.es.js@http://localhost:3000/node_modules/.vite/deps/@react-pdf_renderer.js?v=941513e7:14317
    __init http://localhost:3000/node_modules/.vite/deps/chunk-5537U2Q2.js?v=4b05e6b4:44
    node_modules react-pdf/fontkit/lib/fontkit.browser.es.js@http://localhost:3000/node_modules/.vite/deps/@react-pdf_renderer.js?v=941513e7:15575
    __init http://localhost:3000/node_modules/.vite/deps/chunk-5537U2Q2.js?v=4b05e6b4:44
    node_modules react-pdf/font/lib/index.browser.js@http://localhost:3000/node_modules/.vite/deps/@react-pdf_renderer.js?v=941513e7:50819
    __require2 http://localhost:3000/node_modules/.vite/deps/chunk-5537U2Q2.js?v=4b05e6b4:47
    <anonymous> http://localhost:3000/node_modules/.vite/deps/@react-pdf_renderer.js?v=941513e7:109628

I got the same error.

Can't get the shim to work for a newly created project.

aclec commented

@MrEmanuel

To solve the bug you can install the "buffer" package with yarn or npm.

Then you add this in your main page where you declare your react app:

import {Buffer} from "buffer";
//@ts-ignore
globalThis.Buffer = Buffer;

But I don't know if we can add that to the shim.

@aclec this solution doesn't work for me, adding Buffer to globalThis doesn't seem to make it available to the restructure module when it needs it. I also tried to use the NodeGlobalsPolyfillPlugin module in the Vite config, but that didn't work either.

Hey guys, just a heads up: react-pdf introduced a new usage of Buffer a few weeks ago, with @react-pdf/renderer 2.1.2 and @react-pdf/unicode-properties 2.6.0. I created a fix for this in #8.

aclec commented

@thekevinbrown Can you push the new version on npm please ?