42

After I update my Angular Application from Angular 11.2.12 to Angular 12.0.0

Angular CLI: 12.0.0
Node: 14.15.4
Package Manager: npm 6.14.10
OS: win32 x64

Angular: undefined
...

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1200.0 (cli-only)
@angular-devkit/core         12.0.0 (cli-only)
@angular-devkit/schematics   12.0.0 (cli-only)
@schematics/angular          12.0.0 (cli-only)
typescript                   4.2.4

ng serve generating browser application bundles takes horrible long.

Build at: 2021-05-28T06:03:02.008Z - Hash: 3b63141b76ac88698d67 - Time: 63941ms <- change one line in template
Build at: 2021-05-28T06:08:04.424Z - Hash: e684399b5f80b2b4d785 - Time: 59834ms <- change property name
Build at: 2021-05-28T06:10:05.725Z - Hash: 8125e1bdc31a25d398b5 - Time: 65621ms <- change one line in model

I try different properties for ng serve, at the moment I use:

ng serve --source-map=true --hmr --live-reload

After update to Angular 12.0.0 I generate an complete new angular app and take over the angular.json to my application myapp:

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "myapp": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "myapp",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/myapp",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.json",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["./node_modules/@angular/material/_theming.scss", "src/styles.scss"],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": {
                "scripts": true,
                "styles": false,
                "fonts": false
              },
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "myapp:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "myapp:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "myapp:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["./node_modules/@angular/material/prebuilt-themes/indigo-pink.css", "src/styles.scss"],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": ["tsconfig.app.json", "tsconfig.spec.json", "e2e/tsconfig.json"],
            "exclude": ["**/node_modules/**"]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "myapp:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "myapp:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "myapp"
}

Is there something I can do to speed up the Generating browser application bundles (phase: sealing)?

Mar Tin
  • 2,132
  • 1
  • 26
  • 46
  • Try setting `scripts` to `false` under optimization. Setting `sourceMap` to `false` can make it faster too. I'm still having huge issues with a reduction in speed. I think it's due to Webpack 5 but not sure yet. Not looking good though :-( – Simon_Weaver May 28 '21 at 08:27
  • @Simon_Weaver thanks for your advice, unfortunately I need `sourceMap` for debugging. But it is good to know, that I am not the only one struggling with that issue. – Mar Tin May 28 '21 at 08:57
  • 1
    There's a few issues open regarding performance. https://github.com/angular/angular-cli/issues/20792 https://github.com/angular/angular-cli/issues/20713 – Simon_Weaver May 28 '21 at 09:03
  • 1
    Also try setting `aot="false"`. Make sure to only do that in your development build and to test with it turned on before deploying. Currently my aot build is 109 seconds and my non aot build is 69 seconds. – Simon_Weaver May 28 '21 at 09:04
  • I've been surviving without `sourceMap` for years now. It just made things too slow, adding about 10 seconds. I still recognize my code in the debugger even when it's been rearranged! And I can always find the line I need by just searching for it. Anyway good luck! – Simon_Weaver May 28 '21 at 09:10
  • @Simon_Weaver I understand that argument, but I had all that features before the update. With **Angular 11.2.12** I had compile times around 10s (max.) after an update. That's why I thought I configure something wrong. I also can reproduce it, if I checkout my commit before the update to **Angular 12.0.0** everything is fine. – Mar Tin May 28 '21 at 13:22
  • I just realized you didn't reply specifically about setting `scripts` to false. I don't think you should see 'sealing' at all if you set `scripts` to false. And I just added an answer that's a slight tangent but if you have a lot of SASS it could be another reason why it's slower. – Simon_Weaver May 28 '21 at 20:52
  • I just posted a separate question, hoping to get some answers about Webpack 5 with Angular 12. I'm getting mixed up and downvotes and TBH I don't know why. https://stackoverflow.com/questions/67746047 – Simon_Weaver May 28 '21 at 22:36
  • @Simon_Weaver I set the optimization to `"scripts": false` into my `angular.json`, but the `sealing` phase still takes a very long time (50-60s) after an update. I have the feeling it doesn't matter what I change inside the `angular.json`. – Mar Tin May 29 '21 at 05:28
  • @Simon_Weaver when I try to set `"aot": false` to false I get `The 'buildOptimizer' option cannot be used without 'aot'.` even when I set `"buildOptimizer": false`. It seems to be not possible to set `aot` off? – Mar Tin May 29 '21 at 05:39
  • When I have `aot="false"` off it definitely runs faster and I do also have `buildOptimizer="false"`. Haven't seen that error. Maybe you spelled it wrong or have it in the wrong level in the config? I have it in `architect/build/configurations/development` with ` "defaultConfiguration": "development" in `architect/build` – Simon_Weaver May 29 '21 at 06:35

7 Answers7

47

I try to do a lot of stuff to solve that issue, but hopefully I can reduce my changes to two important points:

  1. update angular 12.0.0 to angular 12.0.2
  2. set defaultConfiguration in angular.json to development ("defaultConfiguration": "development")

"defaultConfiguration": "production"

Build at: 2021-05-29T06:49:51.461Z - Hash: 9d68fbaa715d790c89ed - Time: 29446ms <- change one character in string
Build at: 2021-05-29T06:50:31.850Z - Hash: cc2399413ce6f0800553 - Time: 32195ms <- change one character in string

"defaultConfiguration": "development"

Build at: 2021-05-29T06:52:16.258Z - Hash: b9b4991fa89ef1e785fc - Time: 781ms <- change one character in string
Build at: 2021-05-29T06:52:27.484Z - Hash: 3838e3c45e33228591f9 - Time: 622ms <- change one character in string

final angular.json

  • "sourceMap": true is important for me
  • simply call ng serve, no additional parameters needed
{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "dotbot": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "dotbot",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/dotbot",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.json",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["./node_modules/@angular/material/_theming.scss", "src/styles.scss"],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": {
                "scripts": false,
                "styles": false,
                "fonts": false
              },
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "development"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "dotbot:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "dotbot:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "dotbot:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["./node_modules/@angular/material/prebuilt-themes/indigo-pink.css", "src/styles.scss"],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": ["tsconfig.app.json", "tsconfig.spec.json", "e2e/tsconfig.json"],
            "exclude": ["**/node_modules/**"]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "dotbot:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "dotbot:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "dotbot"
}
Mar Tin
  • 2,132
  • 1
  • 26
  • 46
  • 3
    Thanks, you save our time! – B.Habibzadeh Oct 20 '21 at 12:03
  • 1
    When this issue occurs after updating Angular, make sure that your `package.json` does not overwrite the configuration. I had `ng serve --configuration=dev` and a `dev` configuration that that didn't have the "new" Angular configs, and somehow the build still defaulted to `production`. – Adrian Jandl Jan 12 '22 at 09:53
  • I just upgraded from Angular 10 to 14, and saw an increase in hot reload time from ~3s to ~17s, but after implementing your configuration, it is back to ~3s Thank you! – JW_ Sep 14 '22 at 08:34
  • Solution for me was to run `ng serve --configuration=development`. I'm guessing the spelling varies by settings in angular.json (I'm using Angular ^14.2.0). Thank you! – DeFeNdog Sep 23 '22 at 22:13
  • Working for Angular 13.3.9 – diegot Nov 09 '22 at 14:57
5

I too upgraded an Angular 8 application to 13 and noticed a huge degradation in the sealing phase time during the edit-compile cycle (almost a whole minute). My fix is as follow as none of the suggestions I came across helped me.

Whatever new Angular version you've updated to, do the following:

  1. Generate a new app (e.g., ng new proj13) in the version you're upgrading to. Here, maintain the project creation defaults as performed in the old project (e.g., scss instead of css, router or no router...)
  2. Either copy the new angular.json as-is from the new throwaway project to the upgraded one, or at least make sure you rewrite the old one to look exactly like the new one.
  3. Make sure the project name is the one used under the projects, browserTarget, outputPath... properties in case a filename change took place.

Once done, the edit-compile cycle was the expected "instant", milliseconds no more.

I think during such upgrades, the angular.json file is not getting the proper new-version-format attention it needs as you should discover if you inspect the old vs the new file formats/keys/properties.

mohsenmadi
  • 2,277
  • 1
  • 23
  • 34
3

I believe the changes in speed are due mostly to webpack 5. Unless there is some horrible bug in Angular 12.

I installed a webpack plugin SpeedMeasurePlugin. To do that you have to install the @angular-builders/custom-webpack.

The config file I used was something like this:

const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");

const smp = new SpeedMeasurePlugin();

module.exports = smp.wrap({

  output: {
    scriptType: "module"
  },

  plugins: []
});

For my project that gave me the following output.

enter image description here

As you can see a lot of the time is spent on sass-loader (and possibly css-loader).

I'm looking into ways to cache the sass. I believe most of it is from Angular Material.

I completely blanked out my styles.css file and ended up with:

enter image description here

The ordering is not exactly the same, but the time spent on sass in total seems to be considerably less.

I'm not quite sure where to go from here. Sass isn't the whole story and my aot build is still extremely slow. Is that webpack 5? Not sure.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • 1
    For anyone on Windows if it looks like it's building twice then it's probably this bug https://github.com/angular/angular-cli/issues/20891 – Simon_Weaver May 29 '21 at 06:56
  • What should i do if "sealing" phase is completely stuck? How to find out what's wrong?? I upgraded my solution from Angular 13 to Angular 15 and cannot pass "sealing" phrase, no matter what configuration is. – Alexander Feb 23 '23 at 17:32
  • @Alexander I recently updated to 15 and apart from a weeks worth of fixing Angular Material issues I had surprisingly few other issues. I feel like I haven't even seen `sealing` in a while. I'd start by commenting out all my routes (and comment out imports) and see if you can build a minimal project. – Simon_Weaver Feb 24 '23 at 00:01
  • I have already debugged "ng serve" and now can see the error. But don't know why it came up after Angular upgrade and how to fix it: "Module parse failed: Unexpected token (1:0). You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file". Looks like now "css-loader" does not see .css anymore... – Alexander Feb 24 '23 at 00:12
  • Did you find a solution for this? I'm also experiencing this issue. – Gui Brunow Mar 24 '23 at 05:07
2

Upgrading my packages with npm upgrade has solved my issue.

Skatox
  • 4,237
  • 12
  • 42
  • 47
1

I did a lot of stuff but my error was solved by exporting the node.js openssl-legacy-provider

export NODE_OPTIONS=--openssl-legacy-provider(in git bash)
André
  • 1,602
  • 2
  • 12
  • 26
Ram Thapa
  • 11
  • 1
0

For me, disabling some features on angular.json solve the problem But it depends on the configuration you are running

this is my develop configuration:

"develop": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.develop.ts"
            }
          ],
          "optimization": false,
          "aot": false,
          "outputHashing": "all",
          "sourceMap": true,
          "namedChunks": false,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": false,
          "budgets": [
            {
              "type": "initial",
              "maximumWarning": "2mb",
              "maximumError": "5mb"
            },
            {
              "type": "anyComponentStyle",
              "maximumWarning": "6kb",
              "maximumError": "10kb"
            }
          ]
        }

Those are the changes:

"optimization": false,
"aot": false,
"sourceMap": true,
"buildOptimizer": false,
Daniel
  • 2,780
  • 23
  • 21
0

For me it the slow process has been introduced by wrong tailwind config:

Too permisive glob pattern (very slow):

let config = require('./node_modules/@my-library/tailwind.config.js');
config.content = [
  './apps/**/*.{html,ts,scss}',
  './libs/**/*.{html,ts,scss}',
  './node_modules/@my-library/**/*.*'
];

after fixing the pattern:

let config = require('./node_modules/@my-library/tailwind.config.js');
config.content = [
  './apps/**/*.{html,ts,scss}',
  './libs/**/*.{html,ts,scss}',
  './node_modules/@my-library/**/*.{html,js,ts,scss}'
];

this fix has decreased the build process by ~30seconds.

Felix
  • 3,999
  • 3
  • 42
  • 66