Are "enableProdMode()" and "build --prod" equivalent?

The --prod flag triggers the --environment=prod flag (among other things). This means that the environment file defined as prod in the environments array of the .angular-cli.json is used during compilation.

If you have the default main.ts this will indeed mean it runs enabledProdMode(). Because they use this to trigger it:

if (environment.production) {
    enableProdMode();
}

and in the environment file the production property is set to true.


But remember that if you do something like this:

ng build --env=dev --prod

then flags from production envy are going to be overwritten. I checked it as I was still having problems with performance despite using --prod flag on non prod envy. This is important when building demos, benchmarking builds and so on.


ng build --prod is shorthand for ng build --configuration=production. As part of this, the build process will reference your angular.json file, specifically in this situation the 'configurations' section:

"configurations": {
    "production": {
        "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
         ],
         ...

In environment.prod.ts is the following (by default):

export const environment = {
  production: true
};

If you then look at your main.ts file you will see this code (again, by default):

if (environment.production) {
  enableProdMode();
}

So, as you can see ng build --prod will enable prod mode. Enabling prod mode will disable some validation, speeding up your code. Additional the build process when running with the production configuration will also do the following (again, by default):

  • Ahead-of-Time (AOT) Compilation: pre-compiles Angular component templates.
  • Production mode: deploys the production environment which enables production mode.
  • Bundling: concatenates your many application and library files into a few bundles.
  • Minification: removes excess whitespace, comments, and optional tokens.
  • Uglification: rewrites code to use short, cryptic variable and function names.
  • Dead code elimination: removes unreferenced modules and much unused code.

source