Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify an angular environment on ionic build?

The Ionic framework uses Angular.
Angular 6 defines environments in ./src/environments/environment.stage.ts .

When building an Angular app, I can select the environment with the parameter --env=stage or --configuration==stage in Angular 6.

To build the ionic app, I use ionic cordova build <platform> which in the background first builds the angular app, before packing it in the Cordova framework.

How can I specify the environment aka configuration for the angular build?

like image 248
Martin M Avatar asked Dec 19 '18 12:12

Martin M


People also ask

Can Ionic be used with Angular?

Ionic Angular offers Angular-optimized mobile and web components for building blazing fast mobile, web, and desktop apps.

How do you create an Ionic development environment?

Open your command window and choose where you want to create your app. This command will change the working directory. The app will be created on the Desktop. Ionic Start command will create a folder named myApp and setup Ionic files and folders.

What are Angular environments?

An Angular Application Environment is JSON configuration information that tells the build system which files to change when you use ng build and ng serve . Let's say you have a back end REST API deployed on a server that provides services to your Angular application.

How are path variables set for Ionic?

Click “Advanced System Settings”. Select Environment Variables. Select New environment variable. In the new window, in the name of the variable place JAVA_HOME, and in the value put the path where you installed the JDK.


2 Answers

You can add a corresponding configuration entry in angular.json at ionic-cordova-build:

"ionic-cordova-build": {
  "builder": "@ionic/angular-toolkit:cordova-build",
  "options": {
    "browserTarget": "app:build"
  },
  "configurations": {
    "production": {
      "browserTarget": "app:build:production"
    },
    "staging": {
      "browserTarget": "app:build:staging"
    }
  }
},
$ ionic cordova run android --device -c staging

Note the difference from -c staging to -- -c=staging on ionic serve.

The configuration staging must exists under architect.build.configurations in the same file.

like image 145
Jörn Krüger Avatar answered Oct 14 '22 02:10

Jörn Krüger


On my hand, I created a simple bash file.

#!/usr/bin/env bash

env=$1

targetFile=$PWD/src/environment/environment.ts
filePath=$PWD/src/environment/$1.environment.ts

echo REPLACING FILE ENVIRONMENT : $1
cp $filePath $targetFile

I added environment.ts in .gitignore, and I created a dev.environment.ts and prod.environment.ts.

I switch :

$ bash launcher.sh dev && ionic serve
like image 1
Stefdelec Avatar answered Oct 14 '22 02:10

Stefdelec