The Segfault
The Segfault

Reputation: 1040

Passing --configuration to angular through Cordova

Ionic4 app with 2 projects with 3 env each. I am using angular.json for multi projects along with ionic.config.json -> Documentation

angular.json has 2 projects, each with this kind of configuration:

 "configurations": {
        "production": {
          "fileReplacements": [
            {
              "replace": "src/environment/environment.ts",
              "with": "src/environment/environment.xx.prod.ts"
            }
          ]
         // some parameters not important for our matter
        },
        "ci": {
          "progress": false
        },
        "int": {
          "fileReplacements": [
            {
              "replace": "src/environment/environment.ts",
              "with": "src/environment/environment.xx.int.ts"
            }
          ]
        },
        "dev": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.xx.dev.ts"
            }
          ]
        }
      }

I am running those apps in the browser with

ionic serve --project=xx -- --configuration=dev

This -- allow me to pass the --configuration to angular through ionic, so ionic runs

ng run xx:serve --host=0.0.0.0 --port=8100 --configuration=dev

For the browser everything works fine.

But now for the native, cordova doesn't work this way, my command is:

ionic cordova build android --project=xx -- --configuration=dev

So ionic runs

cordova run android --configuration=dev

This is not working, my angular doesn't know about my configuration.

Upvotes: 2

Views: 939

Answers (1)

The Segfault
The Segfault

Reputation: 1040

In order to mimic the --configuration browser to cordova you need to implement angular-ionic-cli-builders

After implementing that you can stop doing the -- tricks for cordova build

ionic cordova build android --project=xx -- --configuration=dev

To

ionic cordova build android --configuration=dev

Here is my angular.json for a better understanding, sadly I dont have time to make a full tutorial, this is a complicated topic

Check this tutorial

Point of interest are the "serve" and "ionic-cordova-build" part, you can also see that my BrowserTarger are pointing to my differents environment dev int and prod

And also notice that "builder" from "ionic-cordova-serve" have been changed in order to use angular-ionic-cli-builders

   "xx": {
      "root": "",
      "sourceRoot": "/",
      "projectType": "application",
      "prefix": "",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./custom.webpack.config.js",
              "mergeStrategies": {
                "externals": "replace"
              }
            },
            "outputPath": "www",
            "index": "src/index.html",
            "main": "src/main-tpc.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              {
                "glob": "**/*",
                "input": "src/assets",
                "output": "assets"
              }
            ],
            "styles": [
              "src/global.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "prod": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.tpc.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": true,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "10mb",
                  "maximumError": "10mb"
                }
              ]
            },
            "ci": {
              "progress": false
            },
            "int": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.tpc.int.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            },
            "dev": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.tpc.dev.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-builders/dev-server:generic",
          "options": {
            "browserTarget": "tpc:build"
          },
          "configurations": {
            "prod": {
              "browserTarget": "tpc:build:prod"
            },
            "ci": {
              "progress": false
            },
            "int": {
              "browserTarget": "tpc:build:int"
            },
            "dev": {
              "browserTarget": "tpc:build:dev"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "tpc:build"
          }
        },
        "test": {
          "builder": "@angular-builders/custom-webpack:karma",
          "options": {
            "customWebpackConfig": {
              "path": "./custom.webpack.config.js",
              "mergeStrategies": {
                "externals": "replace"
              }
            },
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [],
            "scripts": [],
            "assets": [
              {
                "glob": "favicon.ico",
                "input": "src/",
                "output": "/"
              },
              {
                "glob": "**/*",
                "input": "src/assets",
                "output": "/assets"
              }
            ]
          },
          "configurations": {
            "ci": {
              "progress": false,
              "watch": false
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "ionic-cordova-build": {
          "builder": "@ionic/angular-toolkit:cordova-build",
          "options": {
            "browserTarget": "tpc:build"
          },
          "configurations": {
            "prod": {
              "browserTarget": "tpc:build:prod"
            },
            "dev": {
              "browserTarget": "tpc:build:dev"
            },
            "int": {
              "browserTarget": "tpc:build:int"
            }
          }
        },
        "ionic-cordova-serve": {
          "builder": "angular-ionic-cli-builders:generic-dev-server",
          "options": {
            "cordovaBuildTarget": "tpc:ionic-cordova-build",
            "devServerTarget": "tpc:serve",
            "browserTarget": "tpc:build"
          },
          "configurations": {
            "prod": {
              "cordovaBuildTarget": "tpc:ionic-cordova-build:prod",
              "devServerTarget": "tpc:serve:prod"
            },
            "dev": {
              "cordovaBuildTarget": "tpc:ionic-cordova-build:dev",
              "devServerTarget": "tpc:serve:dev"
            },
            "int": {
              "cordovaBuildTarget": "tpc:ionic-cordova-build:int",
              "devServerTarget": "tpc:serve:int"
            }
          }
        }
      }
    },

Upvotes: 0

Related Questions