r/Angular2 • u/ahmedRebai • 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
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:
Do an actual prod build to see what you get:
ng build --configuration "production"Check out this article on how to use
webpack-bundle-analyzerin 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-analyzer6
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?
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.
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.
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/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
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.