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.

Large bundle size when importing even a single module

See original GitHub issue

Bug description:

Adding a single ng-bootstrap module (like NgbCollapseModule in my example) leads to a surprisingly large increase of the application bundle size.

It looks like there is something fishy, as importing just this module in our application drags ~80kb of ng-bootstrap, plus 42kb of @angular/forms. I can reproduce with a bare CLI application (with numbers slightly lower).

Link to minimally-working StackBlitz that reproduces the issue:

https://github.com/cexbrayat/collapse-bundle-size

This repo has been created using the latest CLI (11.0.2)

  • 1rst commit: generate the application (branch chore/init)
ng new collapse-bundle-size --defaults

Building this bare application gives:

ng build --prod --source-map
Initial Chunk Files               | Names         |      Size
main.866a4eb66ed1bedd7877.js      | main          | 135.41 kB

The generated sourcemap looks like Capture d’écran 2020-11-19 à 14 09 31

  • 2nd commit: add ng-bootstrap (branch feat/ngb-module)

Adding ng-bootstrap produces the following results:

ng add @ng-bootstrap/ng-bootstrap
ng build --prod --source-map
Initial Chunk Files               | Names         |      Size
main.cb92446fa68ee2d403ee.js      | main          | 288.59 kB

That’s a large increase, but ng add adds the complete NgbModule to the imports, so that may be expected. Capture d’écran 2020-11-19 à 14 14 37

  • 3rd commit: import just NgbCollapseModule (master) Replace the import of NgbModule by NgbCollapseModule leads to the following results:
Initial Chunk Files               | Names         |      Size
main.1617f8bf4ea0ac9824ea.js      | main          | 270.76 kB
Capture d’écran 2020-11-19 à 14 18 43

Note that the size is slightly better but still very large for a module containing a single directive. It still drags in @angular/forms in the application bundle when you would expect it doesn’t.

Versions of Angular, ng-bootstrap and Bootstrap:

Angular: 11.0.1

ng-bootstrap: 8.0.0

Bootstrap: 4.5.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
devoto13commented, Dec 8, 2020

Inlining value accessors brings size down ~50%, so there is more to it. I’ll continue the investigation tomorrow.

UPD There was one more forwardRef().

1reaction
maxokorokovcommented, Dec 2, 2020

I’m getting the same results when using the library built with #3906.

Importing NgbCollapseModule:

  • ~4Kb with cli 10 and ng-bootstrap 8
  • ~73Kb with cli 11 and both ng-bootstrap 8 and ng-bootstrap built with #3906

So needs further investigation for sure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How CommonJS is making your bundles larger - web.dev
In this post, we'll look into what CommonJS is and why it's making your JavaScript bundles larger than necessary.
Read more >
Decrease your Handsontable bundle size using modules
Importing the Handsontable component adds 274 KB to your bundle size. Single module import. After making the changes, Handsontable component ...
Read more >
Large bundle size - Questions - Babylon.js Forum
After replacing all imports, the bundle size has dropped to about 4.5MB for dev, and 1.3 MB for prod. Not great, but a...
Read more >
Webpack import causing massive bundle size even with ...
The main package I have exports from other packages in its main index.ts file. I think it must be typescript related because importing...
Read more >
Everything you can do to reduce bundle size for webapps
One of the most common causes of bundle size bloat are modules that can't be tree-shaken, such as CommonJS modules. It can be...
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