Increased main.js bundle size but less lazy chunks after updating to angular 9.0.0-rc.1
π Bug report
Command (mark with an x)
- [ ] new
- [x] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc
Description
After updating from Angular 8.2.3 to 9.0.0-rc.1 the bundle size was increased for main module and some of the feature modules:
Before it was:
After updating to Angular 9.0.0-rc.1 it is

π¬ Minimal Reproduction
running ng build --prod --namedChunks with following configuration:
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"commonChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "1mb",
"maximumError": "2mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb"
}
],
"serviceWorker": true,
"ngswConfigPath": "ngsw-config.json"
}
}
π Your Environment
Angular CLI: 9.0.0-rc.1
Node: 10.15.3
OS: win32 x64
Angular: 9.0.0-rc.1
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router, service-worker
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.0-rc.1
@angular-devkit/build-angular 0.900.0-rc.1
@angular-devkit/build-optimizer 0.900.0-rc.1
@angular-devkit/build-webpack 0.900.0-rc.1
@angular-devkit/core 9.0.0-rc.1
@angular-devkit/schematics 9.0.0-rc.1
@angular/cdk 8.2.3
@angular/material 8.2.3
@ngtools/webpack 9.0.0-rc.1
@schematics/angular 9.0.0-rc.1
@schematics/update 0.900.0-rc.1
rxjs 6.5.3
typescript 3.6.4
webpack 4.41.2
I don't understand how you could use NgModuleFactoryLoader.load at all
I put it as NgModuleFactoryLoader.load in above messages for shortness. So the instance needs to be injected:
constructor(
private moduleLoader: NgModuleFactoryLoader,
) {}
And then you call it like on this line: const moduleFactory = await this.moduleLoader.load("./_db-view/db-view.module#DbViewModule");. On that commit on the left side is a use of old/deprecated way of lazy-loading the modules with NgModuleFactoryLoader, on the right side is the ES import + webpack optimization.splitChunks tweak.
or instructions on how to build your repo and what changes to make to see this behaviour
Repo is here https://github.com/vladimiry/ElectronMail/ To build Angular-related stuff you run yarn build:web:browser-window (the build outcome comes into ./app/web/browser-window/). The commit that switched the lazy module loading from using NgModuleFactoryLoader to regular ES import + webpack optimization.splitChunks tweak.
@vladimiry I did this:
git clone https://github.com/vladimiry/ElectronMail/ && cd ElectronMailyarnyarn build:web:browser-window && ls -la app/web/browser-window/
total 5109
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:05 .
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:05 ..
-rw-r--r-- 1 kamik 197609 131593 Feb 13 16:05 _db-view.js
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:05 assets
-rw-r--r-- 1 kamik 197609 833 Feb 13 16:05 index.css
-rw-r--r-- 1 kamik 197609 542 Feb 13 16:05 index.html
-rw-r--r-- 1 kamik 197609 443705 Feb 13 16:05 index.js
-rw-r--r-- 1 kamik 197609 195805 Feb 13 16:05 vendor.css
-rw-r--r-- 1 kamik 197609 4438375 Feb 13 16:05 vendor.js
- edited
webpack-configs/web/browser-window.tsto remove the cache group for dbview
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]|[\\/]vendor[\\/]/,
name: "vendor",
chunks: "all",
},
// "_db-view": {
// test: /src[\\/]web[\\/]browser-window[\\/]app[\\/]_db-view/,
// name: "_db-view",
// chunks: "all",
// },
},
rm -rf app/web/browser-window/ && yarn build:web:browser-window && ls -la app/web/browser-window/
total 5101
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:15 .
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:15 ..
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:15 assets
-rw-r--r-- 1 kamik 197609 833 Feb 13 16:15 index.css
-rw-r--r-- 1 kamik 197609 486 Feb 13 16:15 index.html
-rw-r--r-- 1 kamik 197609 574975 Feb 13 16:15 index.js
-rw-r--r-- 1 kamik 197609 195805 Feb 13 16:15 vendor.css
-rw-r--r-- 1 kamik 197609 4438375 Feb 13 16:15 vendor.js
So without the cacheGroups your build doesn't actually produce another chunk. This makes me think that _db-view ends up not being lazy loaded at all.
I checked your tsconfig and noticed you were using "module": "commonjs",. This causes typescript to not actually emit the ES import statements. I added "compilerOptions": { "module": "esnext", }, in src/web/browser-window/tsconfig.json (couldn't add it in the base tsconfig because you also use it for compiling the webpack configs) and built again:
rm -rf app/web/browser-window/ && yarn build:web:browser-window && ls -la app/web/browser-window/
total 5145
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:25 .
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:25 ..
-rw-r--r-- 1 kamik 197609 147580 Feb 13 16:25 0.js
-rw-r--r-- 1 kamik 197609 195806 Feb 13 16:25 1.js
-rw-r--r-- 1 kamik 197609 30699 Feb 13 16:25 2.js
-rw-r--r-- 1 kamik 197609 145509 Feb 13 16:25 3.js
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:25 assets
-rw-r--r-- 1 kamik 197609 833 Feb 13 16:25 index.css
-rw-r--r-- 1 kamik 197609 486 Feb 13 16:25 index.html
-rw-r--r-- 1 kamik 197609 144076 Feb 13 16:25 index.js
-rw-r--r-- 1 kamik 197609 195805 Feb 13 16:25 vendor.css
-rw-r--r-- 1 kamik 197609 4380827 Feb 13 16:25 vendor.js
Now we can actually see multiple separate chunks be created automatically (one for each lazy import) and a much smaller index.js.
You can even do better if you disable your vendor cacheGroups. That doesn't help at all because it prevents optimizations in the eagerly loaded chunks, and also eagerly loads dependencies for your lazy chunks.
total 5129
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:28 .
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:28 ..
-rw-r--r-- 1 kamik 197609 596791 Feb 13 16:28 0.js
-rw-r--r-- 1 kamik 197609 268453 Feb 13 16:28 1.js
-rw-r--r-- 1 kamik 197609 150874 Feb 13 16:28 2.js
-rw-r--r-- 1 kamik 197609 195806 Feb 13 16:28 3.js
-rw-r--r-- 1 kamik 197609 30699 Feb 13 16:28 4.js
-rw-r--r-- 1 kamik 197609 483746 Feb 13 16:28 5.js
-rw-r--r-- 1 kamik 197609 145509 Feb 13 16:28 6.js
drwxr-xr-x 1 kamik 197609 0 Feb 13 16:28 assets
-rw-r--r-- 1 kamik 197609 196638 Feb 13 16:28 index.css
-rw-r--r-- 1 kamik 197609 370 Feb 13 16:28 index.html
-rw-r--r-- 1 kamik 197609 3155513 Feb 13 16:28 index.js
This way you get a slightly smaller total payload, and a significantly smaller initial payload.
More to the point insofar as this issue is concerned though, the cache group doesn't do anything useful when you already have ES import() statements with Ivy.
@filipesilva, thanks for thorough analysis. So "compilerOptions": { "module": "esnext", } is the key thing here which I missed.
You can even do better if you disable your vendor cacheGroups. That doesn't help at all because it prevents optimizations in the eagerly loaded chunks, and also eagerly loads dependencies for your lazy chunks.
Yeah, that vendor cacheGroups added there initially and intentionally for my convenience (the css part of this bundle gets independently loaded by different windows, hacky stuff) but maybe it's time to reconsider and get rid of it even though that's a desktop app (the size is not so important, but code parsing still takes time same like in the regular browser). You also probably noticed that there is no any code minification applied, it's also by design as it's the desktop app, not the real web.
This was pretty frustrating when expecting the file size to improve. I have a fairly large app and the file size on main went up from 587K to 610K. I'm not even sure where to start looking for the issue.
"dependencies": { "@angular/animations": "^9.0.2", "@angular/common": "^9.0.2", "@angular/compiler": "^9.0.2", "@angular/core": "^9.0.2", "@angular/forms": "^9.0.2", "@angular/localize": "~9.0.2", "@angular/material": "^9.0.0", "@angular/platform-browser": "^9.0.2", "@angular/platform-browser-dynamic": "^9.0.2", "@angular/platform-server": "^9.0.2", "@angular/router": "^9.0.2", "@nguniversal/common": "^8.1.1", "@nguniversal/express-engine": "^8.1.1", "@nguniversal/module-map-ngfactory-loader": "^8.1.1", "angular2-text-mask": "^9.0.0", "core-js": "^2.6.5", "custom-event-polyfill": "^1.0.7", "fingerprintjs2": "^2.0.6", "gridlex": "^2.7.1", "http-proxy-middleware": "^0.18.0", "immutable": "^3.8.2", "jsvat": "^2.1.2", "ngx-cookie-service": "^2.2.0", "node-fetch": "^2.3.0", "primeng": "9.0.0-rc.4", "rxjs": "^6.5.3", "sass-loader": "^7.1.0", "ts-loader": "^4.5.0", "tslib": "^1.10.0", "url-search-params-polyfill": "^4.0.1", "zone.js": "~0.10.2" }, "devDependencies": { "@angular-devkit/build-angular": "~0.900.3", "@angular/cdk": "^9.0.0", "@angular/cli": "9.0.3", "@angular/compiler-cli": "9.0.2", "@angular/language-service": "9.0.2", "@types/google.analytics": "0.0.39", "@types/jasmine": "~2.5.53", "@types/jasminewd2": "^2.0.6", "@types/node": "^12.11.1", "codelyzer": "^5.1.2", "compression": "^1.7.4", "cookie-parser": "^1.4.4", "express": "^4.16.4", "git-describe": "^4.0.4", "jasmine-core": "~2.6.2", "jasmine-spec-reporter": "~4.1.0", "karma": "^4.0.1", "karma-chrome-launcher": "~2.1.1", "karma-cli": "~1.0.1", "karma-coverage-istanbul-reporter": "^1.4.3", "karma-jasmine": "^1.1.2", "karma-jasmine-html-reporter": "^0.2.2", "nodemon": "^1.18.11", "protractor": "^5.4.2", "ts-node": "~3.2.0", "tslint": "~5.11.0", "typescript": "^3.7.5", "webpack-cli": "^3.3.0" }
@santhony7 can you check if your total app size, with the size of the lazy bundles included, went down?
@filipesilva got same problem, here is bundle-analyzer results
Before:
After:

I think this may be due to ngcc processing @angular/* packages, should ngcc do this?
Yes, ngcc should process those packages.
Webpack Bundle analyzer won't give the correct numbers in a lot of cases though. Using source-map-explorer will give you a more accurate picture of the code in your bundles, as described in https://angular.io/guide/deployment#inspect-the-bundles.
@filipesilva Yeah, I do know that the total size has been reduced but I also think it is important to be very specific about that when claiming file size reduction. As you know, the browser's time-to-interactive or the page-speed-index is pretty much the only metric that matters to leadership. Selling them the upgrade on the promise of faster (initial) download only to find out you made it worse can be a bit disheartening.
To the best of my knowledge, we only have one situation currently where the main bundle gets bigger: https://github.com/angular/angular-cli/issues/16866. Still trying to figure out what we can do in that case. Do you also use material in your app?
Yes. I use material, but it is not big part of my app. Thanks for your help.
@filipesilva Yes sir we do. "@angular/material": "^9.1.0",
Hi everyone! my main.js is considerably bigger with ivy. Any idea what it could be?
Before:
After:

@yamidvo the tool you are using (webpack-bundle-analyzer) will not provide very precise numbers with Angular CLI 9, because we process bundles after they leave webpack. A better tool to get that data would be source-map-explorer as described in https://angular.io/guide/deployment#inspect-the-bundles. That will give you very precise measurements.
@filipesilva
Full bundle: 2.88 MB
Main bundle: 972, 9 KB (size in webpack-bundle-analyzer 973.6 KB (less than 1KB)
Angular modules: 704.45 KB + rxjs 48.26 + zone.js 35.7 = 788 KB is only for angular stuff. ( and I've already moved to lazy modules almost all material modules that not used at the index page. As I remember angular 9 decrease full size from 3.4 to 2.9 MB but increased main bundle a lot( and almost all is taken by angular(
Similarly, the difference is large
before:

after:

@parys I can't say much regarding that screenshot as I don't have anything to compare it to. Could you include a screenshot of the source map explorer output from before 9 please?
@yamidvo I noticed you have a large shared module, which can be a problem as described in https://github.com/angular/angular-cli/issues/16146#issuecomment-557559287. Have you tried what I mention in the "What you can do right now" section of that comment? You might have static imports to your shared code from your main entry point, causing it to not be loaded lazily at all. I don't think this is a problem we can fix on our side.
Either way, something else you two can do is to opt out of Ivy, as described in https://angular.io/guide/ivy#opting-out-of-ivy-in-version-9. This should get back to the setup used in Angular version 8.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.