question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tree-shaking standard fonts possible?

See original GitHub issue

Hey 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.

Webpack analyze

Even when “only” using `PDFDocument, the fonts are included in the bundle.


Reproduction

  1. Create a new folder and add a test.js file 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.

  1. Add webpack, babel and the lib as dependencies: yarn add pdf-lib webpack webpack-cli babel-loader @babel/core
  2. 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.

Webpack result

Related issues: #82

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
manniLcommented, Sep 19, 2020

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 🙈 )?

I should also mention that StandardFonts.Helvetica is used internally by pdf-lib. This is why you can draw text and fill forms without specifying a font explicitly. I don’t think it would be possible to tree-shake this.

That’d make sense, definitely! Still, removing the other 13 fonts would be an improvement already.

2reactions
Hopdingcommented, Sep 19, 2020

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:

import { PDFDocument, HelveticaFont } from 'pdf-lib';

const pdfDoc = await PDFDocument.create();
const helvetica = await pdfDoc.embedFont(HelveticaFont);

or this

import { PDFDocument } from 'pdf-lib';
import { Helvetica } from '@pdf-lib/standard-fonts';

const pdfDoc = await PDFDocument.create();
const helvetica = await pdfDoc.embedFont(Helvetica);

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found