Tree-shaking standard fonts possible?
See original GitHub issueHey all 👋🏻
I’m wondering if there is a way to tree-shake the “standard fonts” as they are a huge portion of the bundle chunk containing pdf-lib.
Even when “only” using `PDFDocument, the fonts are included in the bundle.
Reproduction
- Create a new folder and add a
test.jsfile with the following content`
import PDFDocument from 'pdf-lib'
async function foo () {
const newPdf = await PDFDocument.create()
}
foo()
This ensures that we “only” create an empty PDF Document, nothing else.
- Add webpack, babel and the lib as dependencies:
yarn add pdf-lib webpack webpack-cli babel-loader @babel/core - Run
webpack ./test.js -o bundle.js -p --display-chunks --display-modules --display-origins --module-bind js=babel-loader
Expected
Having a small result chunk without the StandardFonts.
Actual
A 344 KiB Bundle.
Related issues: #82
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Developers - Tree-shaking standard fonts possible? - - Bountysource
I'm wondering if there is a way to tree-shake the "standard fonts" as they are a huge portion of the bundle chunk containing...
Read more >Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module...
Read more >Tree-Shaking: A Reference Guide - Smashing Magazine
Tree-shaking ” is a must-have performance optimization when bundling JavaScript. In this article, we dive deeper on how exactly it works and ...
Read more >Tree-Shaking | Font Awesome Docs
Using Font Awesome with Tree-shaking. The Font Awesome icon packages support tree-shaking out of the box but only if your tool is tree-shaking...
Read more >Implementing Tree Shaking for FontAwesome - CodePicky
There's no standard way of implementing tree shaking, however, there are a few tools ... (Lighthouse) tool, made available by Google itself.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hey @Hopding! Thanks for the detailed answer 🙏
I see the point there, definitely. Maybe it’d be possible to “feature-flag” such a new “improved” API or use another separated named output, say
MinimalPDFDocument(or so, naming things is hard 🙈 )?That’d make sense, definitely! Still, removing the other 13 fonts would be an improvement already.
Hello @manniL! It is not currently possible to tree-shake the standard fonts. This is an issue I would like to fix, as the standard fonts occupy a huge amount of the bundle size. This is annoying if you don’t use them. But it also impacts users that do use them, because most of the time only one or two of the fonts are used. Rarely all 14 of them!
That being said, I’m not sure if this can be fixed without a breaking API change. I think an API like this would solve the problem quite nicely:
or this
At first glance, I don’t see a way to do achieve either of these APIs without a breaking change. It might be possible, though. If anybody would like to investigate this further, I’d be happy to accept a PR for it (again, assuming it can be done without breaking changes).
If, in fact, breaking changes are necessary, then fixing this will have to wait for
pdf-lib@2.0. There are several breaking features I’d like to include in 2.0, but don’t currently have a roadmap for this.