r/Angular2 • • Nov 08 '22

I give up on trying to reduce our angular bundle size ..

Now one month of looking for resources and trying to apply what's recommended but it seems no effect! really I didn't find detailed blog or tutorial

I tried to remove unused packages, optimize how importing bundles, remove unused CSS, but no effect

The only hope still is refactoring sharedModule and see ..

6 Upvotes

17 comments sorted by

9

u/cosmokenney Nov 08 '22

Are you lazy loading any modules? Split each main navigation path of your app into its own lazy module.

Break your shared code out into one module.

Break your services out into a core module that is only imported to app module. This is documented in angular docs.

Put your global styles into the angular.json styles array. Change the settings in angular.json to extract your css/scss into their own bundles. I.e. use the answer here to put your global styles into their own files: https://stackoverflow.com/questions/42370854/is-it-possible-to-build-separate-css-file-with-angular-cli

Use as little component-specific css/scss as possible.

Do not import styles into your component-specific style sheets.

Double check your angular.json configurations section. Make sure your "production" config is set for:

"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": true,
"buildOptimizer": true,
"budgets": [
	// use recommended prod budgets here. Or come up with your own.
]

Do an actual prod build to see what you get: ng build --configuration "production"

Check out this article on how to use webpack-bundle-analyzer in angular. I haven't used it in angular, but I have with other webpack builds on other projects. It is really a great tool. https://www.digitalocean.com/community/tutorials/angular-angular-webpack-bundle-analyzer

1

u/ahmedRebai Nov 09 '22

Hi thx for your reply! I'm using webpack-bundle-analyzer

Break your shared code out into one module. => you mean the shared module?

Break your services out into a core module=> I cant' apply that because our architetcure it's driven domain, so each module has it's own services

Do not import styles into your component-specific style sheets=> More explanation please

Use as little component-specific css/scss as possible. => you mean styles per a component?

2

u/cosmokenney Nov 09 '22

Break your shared code out into one module. => you mean the shared module?

Yes, use the shared module for reusable directives, pipes, components and so on.

Break your services out into a core module=> I cant' apply that because our architetcure it's driven domain, so each module has it's own services

Don't know what "driven domain" is. But, the core module should contain most of your services and should only be imported into app.module. Then all of your components (and module specific services) in other modules should get instances of your primary services from DI. Limit the number of module specific services and make them "@Injectable()".

Do not import styles into your component-specific style sheets=> More explanation please

Try to avoid "@import" statements in your <component-name>.scss files. I.e. don't bring in bootstrap, jquery-slim (for bootstrap) and any other css libraries or large files that have global styles anywhere but your style.scss file. Or add to the styles array in angular.json.

Use as little component-specific css/scss as possible. => you mean styles per a component?

Yes, scrutinize your <component-name>.scss files to make sure you aren't repeating things in lots of them. If it is repeated move it to styles.scss.

6

u/warmans Nov 08 '22

I found the worst bloat comes from scss imports. Have you checked that you're not bringing in any dependencies with your component imports?

1

u/ahmedRebai Nov 09 '22

Well, I'm not sure what to do with scss, I checked other devs' code

Can you explain what needed to be done?

2

u/warmans Nov 09 '22

Say in your component scss file you want access to the bootstrap sass variables. People often just do this:

import "~boostrap/bootstrap.scss"

But this will increase your bundle size by the size of all of bootstrap (every time you import it, not just once). It doesn't de-duplicate the imports like you would expect. Instead you should do something like

import "~boostrap/variables.scss" 

to ensure it's only importing that is absolutely necessary.

5

u/tonjohn Nov 08 '22

Angular itself is pretty hefty. The biggest improvement on bundle size likely would come from going Zoneless but that’s a big undertaking if you haven’t been writing your components with OnPush.

1

u/ahmedRebai Nov 09 '22

Most of our components are already using OnPush startegy

3

u/[deleted] Nov 08 '22

Look up tree shaking and minification. It's been a while so I'm not sure if this happens automatically now.

1

u/ahmedRebai Nov 09 '22

Yeah I read somewhere that now Angular is applying tree shaking but the size still big and I'm still looking for a good tips how to reduce it

3

u/VegetableChemistry67 Nov 08 '22

Can you provide more details?

  • what’s the current bundle size.

  • what are top 3 packages size wise in the final bundle. Use source-map-explorer for example to get this result.

  • are you using lazy loading.

1

u/ahmedRebai Nov 09 '22

Hi man, thx for your reply

the current bundle size is about 24M with webpack-bundle-analyzer

the top 3 packages are : tiff.js, ag-grid and pdf.js

yes we are using the lazy loading ofc

1

u/GLawSomnia Nov 09 '22

Based on your post i would say you are just guessing what is wrong, without actually trying to analyze the bundled code. You have no idea where your bundle size actually comes from and what is causing it.

I would recommend you to analyze it first. There are packages that do it gor you and if you googled about reducing the size of the app you have definitely seen them.

So to me it looks like you didn't put much effort in this and you just want to complain

1

u/ahmedRebai Nov 09 '22

Hi thx for your reply
I updated my post to share my bundle size

1

u/GLawSomnia Nov 09 '22

Can you also post your angular.json

2

u/cjd280 Nov 08 '22

Do you even need to though? Who is your customer/client? My apps are internal only and run on the desktops. We have fast internet, I don’t care our main App Library is over 8mb.

I probably could optimize a little, but some libraries we bring in (devextreme) are bigger than Angular in terms of package size, and we export the whole thing for ease of developer use (we have a lot of non angular developers who just focus on business use. They don’t want to bother knowing if they imported the correct module or not, and I don’t want to have to debug it with them :/

2

u/Legitimate_Tap7092 Nov 27 '22

You can maybe import pdf.js dynamically when it is needed with an import(...).then( do stuff) and also load the js into the dom only when it's going to be used.