Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Webpack reduce bundle size by not bundling specific libraries

I have a legacy react app that I'd like to optimize in terms of the bundle size. There are a few dependencies that I'm using that include a pdfjs-dist library.

I can decide not to use the functionality that requires me to include that library. Is there a way I can ignore bundling of this dependency to slim down on the bundle size?

So far I've tried using externals, but this didn't work. I can still see the package in webpack-bundle-analyzer.

externals: {
        "pdfjs-dist": '',
    },
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The bundler is supposed to bundle all the dependencies. You cannot simply ask it to ignore a certain dependency.

But in your case, if you are not going to execute the code that depends on the pdfjs-dist module, then you can use external to fake the dependency. You can simply say:

externals: { 'pdfjs-dist': 'X' }

Here, the Webpack will assume that there is a global variable named X accessible as globalThis.X or window.X, etc. Webpack will link all the invocation of pdfjs-dist to this global X variable. Point here is that your external configuration cannot have a blank string. So, as long as you not calling X, your code would safely work.

Further, to create a fake X so that when any property or method is called accidentally, instead of error, you can make it fail silently using ES Proxy interceptor. Add this somewhere in your initialization sequence as:

const handler = {
  get: function(obj, prop) {
    return 0;
  }
};

// Set as a global object using globalThis or window.
window.X = new Proxy({}, handler);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!