Created
January 10, 2022 02:05
-
-
Save tkutcher/7152f19ec938dc72eb1e0782d4c30e18 to your computer and use it in GitHub Desktop.
Environment-Specific Configuration for Angular Applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"configurations": { | |
"production": { | |
// ... | |
"fileReplacements": [ | |
{ | |
"replace": "src/environments/environment.ts", | |
"with": "src/environments/environment.prod.ts" | |
} | |
] | |
}, | |
"development": { | |
// ... | |
"fileReplacements": [ | |
{ | |
"replace": "src/environments/environment.ts", | |
"with": "src/environments/environment.dev.ts" | |
} | |
] | |
}, | |
"local": { | |
// same properties like "sourceMap" and "optimization" from development | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const MY_API_BASE = new InjectionToken<string>("My API Address"); | |
@Injectable({ providedIn: "root" }) | |
export class MyApiService { | |
constructor( | |
private http: HttpClient, | |
@Inject(MY_API_BASE) private apiAddress: string | |
) {} | |
getFoo(): Observable<any> { | |
return this.http.get(`${this.apiAddress}/foo`); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app-environment.model.ts | |
/** A configuration object with environment-specific properties. */ | |
export interface AppEnvironment { | |
/** True if the @angular/core enableProdMode should be called. */ | |
production: boolean; | |
/** The api server address. */ | |
apiAddress: string; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule } from '@angular/core'; | |
import { BrowserModule } from '@angular/platform-browser'; | |
import { AppComponent } from './app.component'; | |
import { MY_API_BASE } from './my-api.service'; | |
import { environment } from '../environments/environment'; | |
@NgModule({ | |
declarations: [ AppComponent ], | |
imports: [ BrowserModule ], | |
providers: [ | |
{ provide: MY_API_BASE, useValue: environment.apiAddress } | |
], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// environment.local.ts | |
export const environment: AppEnvironment = { | |
production: false, | |
apiAddress: "http://localhost:7071", | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { environment } from "../../environments/environment"; | |
@Injectable({ providedIn: "root" }) | |
export class MyApiService { | |
constructor(private http: HttpClient) {} | |
getFoo(): Observable<any> { | |
return this.http.get(`${environment.apiAddress}/foo`); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"serve": { | |
"builder": "@angular-devkit/build-angular:dev-server", | |
"configurations": { | |
"production": { | |
"browserTarget": "demo-angular-env-config:build:production" | |
}, | |
"development": { | |
"browserTarget": "demo-angular-env-config:build:development" | |
}, | |
"local": { | |
"browserTarget": "demo-angular-env-config:build:local" | |
} | |
}, | |
"defaultConfiguration": "local" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// architect.build.configurations.production | |
"fileReplacements": [ | |
{ | |
"replace": "src/environments/environment.ts", | |
"with": "src/environments/environment.prod.ts" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file can be replaced during build by using the `fileReplacements` array. | |
// `ng build` replaces `environment.ts` with `environment.prod.ts`. | |
// The list of file replacements can be found in `angular.json`. | |
export const environment = { | |
production: false | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
src/ | |
├── app/ | |
├── assets/ | |
├── environments/ | |
│ ├── environment.prod.ts | |
│ └── environment.ts | |
├── ... | |
└── test.ts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment