5

I am working on a angular 7 app which works fine except loading issues, on research I found that I should be using AOT build instead of JIT or normal build, I was not able to make aot build because of huge application size, it was returning heap our of memory issue. Later I removed some modules and components to make AOT build possible which worked but then I realized that AOT build file size is much larger than normal build which take more time to load and site load speed is worse than before now and it take 10-12 seconds to load.

Following is my angular.json file code

"configurations": {
  "production": {
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": true,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": true,
    "buildOptimizer": false,
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ]
  }
}

and following are screenshot of file sizes for builds

When I build with ng build --prod --aot false

aot false build

When I build with ng build --prod --aot

aot true build

I am not sure what is the issue and even not sure if it works like this only, all articles suggests for aot build to reduce size but mine increases. Either I dont have proper configuration or missing something.

All I want to is reduce site load time without using universal.

Any help will be appreciated, thanks

Updated, I doubt that I have not used proper modules/packages so following is my package.json file to review

"dependencies": {
    "@agm/core": "^1.0.0-beta.2",
    "@angular/animations": "^7.2.3",
    "@angular/cdk": "^7.3.1",
    "@angular/common": "^7.2.4",
    "@angular/compiler": "^7.2.4",
    "@angular/core": "^7.2.4",
    "@angular/forms": "^7.2.4",
    "@angular/http": "^7.2.4",
    "@angular/material": "^7.3.1",
    "@angular/platform-browser": "^7.2.4",
    "@angular/platform-browser-dynamic": "^7.2.4",
    "@angular/router": "^7.2.4",
    "@ngx-loading-bar/core": "^4.2.0",
    "@ngx-loading-bar/router": "^4.2.0",
    "@ngx-translate/core": "^11.0.1",
    "@ngx-translate/http-loader": "^4.0.0",
    "angular2-text-mask": "^9.0.0",
    "angularx-social-login": "^1.2.6",
    "core-js": "^2.6.5",
    "crypto-js": "^3.1.9-1",
    "file-saver": "^1.3.4",
    "hammerjs": "^2.0.8",
    "magnific-popup": "^1.1.0",
    "moment": "^2.24.0",
    "moment-timezone": "^0.5.23",
    "mydatepicker": "^2.6.6",
    "ng2-ckeditor": "^1.2.2",
    "ng2-file-upload": "^1.3.0",
    "ng6-toastr": "6.0.0",
    "ngx-google-places-autocomplete": "^2.0.4",
    "ngx-slick": "^0.2.1",
    "rxjs": "^6.4.0",
    "rxjs-compat": "^6.4.0",
    "tslib": "^1.9.0",
    "videogular2": "^6.4.0",
    "web-animations-js": "^2.3.1",
    "zone.js": "^0.8.29"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.0",
    "@angular/cli": "^7.3.3",
    "@angular/compiler-cli": "^7.2.4",
    "@angular/language-service": "^7.2.4",
    "@types/core-js": "^0.9.45",
    "@types/jasmine": "^3.3.8",
    "@types/jasminewd2": "^2.0.6",
    "@types/node": "^10.12.21",
    "codelyzer": "^4.5.0",
    "jasmine-core": "^3.3.0",
    "jasmine-spec-reporter": "^4.2.1",
    "karma": "^4.0.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "^1.0.1",
    "karma-coverage-istanbul-reporter": "^2.0.4",
    "karma-jasmine": "^2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "^5.4.2",
    "rxjs-tslint": "^0.1.6",
    "ts-node": "^8.0.2",
    "tslint": "^5.12.1",
    "typescript": "3.2.4"
  }
Vikram
  • 3,171
  • 7
  • 37
  • 67
  • 2
    The --prod flag enables aot by default. Try `ng build --prod --build-optimizer` – Avin Kavish Jun 12 '19 at 07:13
  • 1
    It is very unusual behavior of angular, I suggest you to open an issue in Angular community in GitHub. Also if you migrate your application to v8, you can use of [IVY](https://blog.angular.io/its-time-for-the-compatibility-opt-in-preview-of-ivy-38f3542a282f) compiler which reduce the size of your build result. – Arash Jun 12 '19 at 07:17
  • @AvinKavish I tried it too but same results and file sizes are alomost same as shown in second screenshot – Vikram Jun 12 '19 at 07:17
  • I'm curious what articles told you that an AoT build would generate smaller files? I'm not an expert, but I know that AoT (Ahead-of-Time) compilation is primarily used to generate the code that runs that represents your components. It allows Angular to use O(1) instead of O(n) efficiency on look ups when performing change detection. So you trade more code for a faster app. – Matt Hintzke Jun 12 '19 at 07:18
  • No, AOT compilation means that the angular compiler does not need to be in the final bundle. and the angular compiler is about 50% of the framework itself. https://angular.io/guide/aot-compiler – Avin Kavish Jun 12 '19 at 07:19
  • Are you compiling components dynamically? In that case, AOT will not remove the compiler. – Avin Kavish Jun 12 '19 at 07:20
  • @AvinKavish not sure what you mean by "Compiling dynamically" can you explain ? – Vikram Jun 12 '19 at 07:22
  • As in compiling templates from strings or building components in the front-end? Anywhere you need to import `@angular/compiler`? Regardless, I'm seeing that your Reschedule bookings module is 2Mb and AddBookings module is 1.7Mb, that is a bigger issue than Angular. Your next largest module is 279kb. You must have some bad imports. – Avin Kavish Jun 12 '19 at 07:27
  • @AvinKavish Thanks for explaining , I dont have @angular/compiler in components, add and reschdule booking are huge modules as they have 2 pages of around 300 fields and calculation logic, I am still trying to figure out a better way to optimize it, but first page that loads is home page which takes 10 sec – Vikram Jun 12 '19 at 07:39
  • 1
    Assuming the home page is in main, your initial download is main+vendor+common+polyfills+styles = 580+24+42+1077+307=~2MB. Which should go down to about 1MB when you **enable GZIP compression on the server.** If 1MB takes 10 secs that sounds like an internet issue. You will have to use Univseral or something to work around it. But as for AOT, it seems to be working, the vendor bundle goes down from 1.5Mb to 1Mb, the other modules go up most likely due to incorrect import configuration between lazy loaded modules. For one I don't see a shared module to share components between lazy modules. – Avin Kavish Jun 12 '19 at 07:52

1 Answers1

0

Use this command if you have a huge application size

node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --aot
Yash Rami
  • 2,276
  • 1
  • 10
  • 16
  • This command is useful only when ng build fails due to FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory. – khush Jun 12 '19 at 07:56
  • it can be used if aot build fails with out of memory issue but nothing related for optimization of load – Vikram Jun 12 '19 at 08:00