Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark package as sideEffect free in package.json #1199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

joewestcott
Copy link

@joewestcott joewestcott commented Mar 24, 2022

Hi @Hopding!

I'm utilising this library in a slightly different way from most, I'm just using the low-level primitives available in /core and creating PDFs semi-manually. This library is amazingly flexible, and means I don't have to re-implement difficult things like image/font loading. Hurrah!

However, despite using the ESM build (pdf-lib/es/index.js), many bundlers still bundle the entire library by default.

As a simple example, the file below only has one pdf-lib import:

// in.js
import { PDFRef } from "pdf-lib";
console.log(PDFRef);

Yet bundling with ESbuild...
esbuild --bundle ./in.js --minify --outfile=out.js
...gives an output size of 425.8kb, which is the whole library!

If pdf-lib declared itself as side-effect free, bundlers can be more aggressive with tree-shaking, and remove unused exports. I believe pdf-lib can do so, because all modules are self-contained and don't modify each other or global scope.

The same test with this PR applied... 13.9kb!

For more info, here's Webpack's documentation on this setting:
https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free

Finally - thank you for your hard work on this library! 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant