unjs / unpdf

📄 Utilities to work with PDFs in Node.js, browser and workers
MIT License
438 stars 12 forks source link

Does not work in BGSW using Plasmo framework #2

Open Fiattarone opened 10 months ago

Fiattarone commented 10 months ago

Environment

Framework: Plasmo 0.84.0 Client side Chrome Browser Extension

Reproduction

Can be reproduced by creating a BGSW in the Plasmo framework and importing unpdf. Error message is:

🔴 ERROR | Build failed. To debug, run plasmo dev --verbose. 🔴 ERROR | Failed to resolve 'unpdf/pdfjs' from './node_modules/.pnpm/unpdf@0.10.0/node_modules/unpdf/dist/index.mjs'

Describe the bug

Bug is as aforementioned: unpdf seems to be looking for a pdfjs dependency that is inaccessible.

Additional context

No response

Logs

No response

johannschopplich commented 10 months ago

Mmh, interesting. Which bundler does Plasmo use? unpdf provides a sub path export unpdf/pdfjs:

https://github.com/unjs/unpdf/blob/f640d4de5999ddbeedb731b8580a1e7e1cfddb80/package.json#L34

Seems to me more like a Plasmo issue than unpdf. I have tested unpdf with esbuild, Rollup and more, which all correctly bundle unpdf/pdfjs.

Could you please provide a reproducible example?

Fiattarone commented 10 months ago

Bundler used is https://parceljs.org/ I believe... I know that I couldn't get plasmo to work with pdfjs either, so this very well could be a plasmo issue.

Will drop an example in here when I get a moment, sorry about not including that the first round.

My current workaround is to use unpdf on my proxy (vercel) server (normally used for other things) but this is really just to continue development, it would be really not-prime to handle everyone's PDF shenanigans when they could be doing it themselves on the client side.

Fiattarone commented 9 months ago

Hey there, sorry for taking so long!

https://github.com/Fiattarone/unpdf-plasmo-example under core/test.ts is where I put it

Or, even easier to reproduce with a one-liner:

git clone git@github.com:Fiattarone/unpdf-plasmo-example.git  --recurse-submodules &&
cd unpdf-plasmo-example &&
pnpm install &&
pnpm dev

fails upon compiling

If this is more of an issue with plasmo, I'll take it up with them--just let me know!

johannschopplich commented 9 months ago

Hi @Fiattarone, thanks for the reproduction. I tried to import the pdfjs subpath export, but somehow the Parcel bundler doesn't seem to resolve the dependency. A quick look at Parcel's dependency resolution docs didn't help.

Even if we point to the PDF.js source file, the bundler fails to locate it:

import { configureUnPDF, extractText } from "unpdf"

configureUnPDF({
  pdfjs: () => import("unpdf/dist/pdfjs.mjs"),
});

Sorry, but at the moment, I don't have a fix for the Plasmo framework. Noted that Vite (using Rollup) and esbuild correctly resolves the import…