How does the Angular CLI know which one is the default environment?

Viewed 660

I had to rename my Angular environments, because it was requested by the DevOps team to follow their conventions. Now, I have 3 environment files under src/environments:

  • environment.dev.ts (default, previously environment.ts)
  • environment.stable.ts (prod, previously environment.prod.ts)
  • environment.mock.ts (environment to use with development mock server)

I have the angular.json file configured accordingly, with file replacements and all, ng serve and ng build works well with the --configuration option.

My question, however, how does the Angular CLI know which environment to serve when I don't set the --configuration option. It starts the development environment now, which is great, but I just really want to understand why does this happen. I have seen that before Angular 6 there was an environments field in the angular.json file, but now it is not the case.

How does the CLI know which environment to start when I don't explicitly set the environment?

Is it maybe just depending on which env file I am importing in my components defaultly when I use the environment variables? Or does it look at which file I am replacing when going to prod?

Here is my angular.json file:

{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
    "poc": {
        "projectType": "application",
        "schematics": {
            "@schematics/angular:component": {
                "style": "scss"
            }
        },
        "root": "",
        "sourceRoot": "src",
        "prefix": "app",
        "architect": {
            "build": {
                "builder": "@angular-devkit/build-angular:browser",
                "options": {
                    "outputPath": "dist/poc",
                    "index": "src/index.html",
                    "main": "src/main.ts",
                    "polyfills": "src/polyfills.ts",
                    "tsConfig": "tsconfig.app.json",
                    "aot": true,
                    "assets": [
                        "src/favicon.ico",
                        "src/assets"
                    ],
                    "styles": [
                        "src/styles.scss",
                        "node_modules/fundamental-styles/dist/icon.css"
                    ],
                    "scripts": []
                },
                "configurations": {
                    "stable": {
                        "fileReplacements": [
                            {
                                "replace": "src/environments/environment.dev.ts",
                                "with": "src/environments/environment.stable.ts"
                            }
                        ],
                        "optimization": true,
                        "outputHashing": "all",
                        "sourceMap": false,
                        "namedChunks": false,
                        "extractLicenses": true,
                        "vendorChunk": false,
                        "buildOptimizer": true,
                        "budgets": [
                            {
                                "type": "initial",
                                "maximumWarning": "2mb",
                                "maximumError": "5mb"
                            },
                            {
                                "type": "anyComponentStyle",
                                "maximumWarning": "6kb",
                                "maximumError": "10kb"
                            }
                        ]
                    },
                    "mock": {
                        "fileReplacements": [
                            {
                                "replace": "src/environments/environment.dev.ts",
                                "with": "src/environments/environment.mock.ts"
                            }
                        ]
                    }
                }
            },
            "serve": {
                "builder": "@angular-devkit/build-angular:dev-server",
                "options": {
                    "browserTarget": "poc:build"
                },
                "configurations": {
                    "stable": {
                        "browserTarget": "poc:build:stable"
                    },
                    "mock": {
                        "browserTarget": "poc:build:mock"
                    }
                }
            },
            "extract-i18n": {
                "builder": "@angular-devkit/build-angular:extract-i18n",
                "options": {
                    "browserTarget": "poc: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",
                    "assets": [
                        "src/favicon.ico",
                        "src/assets"
                    ],
                    "styles": [
                        "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": "poc:serve"
                },
                "configurations": {
                    "stable": {
                        "devServerTarget": "poc:serve:stable"
                    }
                }
            }
        }
    }
},
"defaultProject": "poc"

}

0 Answers
Related