Repository navigation
Resolve the pdf.js worker explicitly so it survives bundling - #5
Merged
Merged
Conversation
Under Node, pdf.js runs its worker in-process and reaches it with a dynamic import() of a path it works out for itself. Nothing static points at that file, so a bundler or a file tracer cannot see it, and a serverless build leaves it out of the deployment. The first getDocument then fails with: Setting up fake worker failed: Cannot find module '/var/task/node_modules/pdfjs-dist/legacy/build/pdf.worker.mjs' Setting GlobalWorkerOptions.workerSrc from require.resolve with a literal specifier makes the dependency statically analysable, so it gets traced and shipped like any other file. No behaviour change locally, where the guessed path was already correct. Note for consumers: standard_fonts/ and cmaps/ are still reached through a computed path, because pdf.js wants a directory rather than a file. A serverless deployment has to include those explicitly or CID-keyed and standard-font PDFs render as empty boxes. Signed-off-by: Govind Pandey <govindup63@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
Deploying a consumer of this library to a serverless host fails on the first redaction:
Under Node, pdf.js runs the worker in-process and reaches it with a dynamic
import()of a path it computes at runtime. Nothing static points at that file, so a bundler or a file tracer cannot see the dependency, and the file never makes it into the deployment. It works locally only because the guessed path happens to be right when the wholenode_modulestree is present.The fix
Set
GlobalWorkerOptions.workerSrcfromrequire.resolvewith a literal specifier. Tracers followrequire.resolve()on a static string, so the worker now gets traced and shipped like any other file. Behaviour is unchanged locally, where the path pdf.js guessed was already correct.Still on the consumer
standard_fonts/andcmaps/are handed to pdf.js as directories, not files, so there is no specifier to resolve and no way to make them statically visible from here. A serverless deployment has to include them explicitly, or a PDF that relies on a standard or CID-keyed font renders as empty boxes rather than failing loudly. That is called out in the commit message so the next person hits it as a documented caveat instead of a mystery.Tests 14/14, typecheck clean.