Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 53fc341

Browse files
nphyattimhoffd
authored andcommitted
feat(environments): configuration via process.env.VAR replacement (#1471)
1 parent ab96e23 commit 53fc341

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ To run the `build` script found in the `package.json` `scripts` property, execut
5252
npm run build
5353
```
5454

55+
## Environments
56+
57+
You can use Node style `process.env.MY_VAR` syntax directly in your typescript
58+
and when the application is bundled it'll be replaced with the following order of precedence:
59+
* If the variable exists in the process environment it will be replaced with that value.
60+
* If the variable is not defined in the process environment it will be read from a `.env.dev`
61+
file for dev builds or `.env.prod` file for prod builds which are located in the root of the app
62+
* If the variable is not defined in either place it will be `undefined`
63+
64+
In order to take advantage of this apps will need a `src/declarations.d.ts` file with the following declaration:
65+
```typescript
66+
declare var process: { env: { [key: string]: string | undefined; } };
67+
```
5568

5669
## Custom Configuration
5770

config/webpack.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
var path = require('path');
1111
var webpack = require('webpack');
1212
var ionicWebpackFactory = require(process.env.IONIC_WEBPACK_FACTORY);
13+
const Dotenv = require('dotenv-webpack');
1314

1415
var ModuleConcatPlugin = require('webpack/lib/optimize/ModuleConcatenationPlugin');
1516
var PurifyPlugin = require('@angular-devkit/build-optimizer').PurifyPlugin;
@@ -91,6 +92,11 @@ var devConfig = {
9192
},
9293

9394
plugins: [
95+
new Dotenv({
96+
path: '.env.dev', // load this now instead of the ones in '.env'
97+
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
98+
silent: true // hide any errors
99+
}),
94100
ionicWebpackFactory.getIonicEnvironmentPlugin(),
95101
ionicWebpackFactory.getCommonChunksPlugin()
96102
],
@@ -124,6 +130,11 @@ var prodConfig = {
124130
},
125131

126132
plugins: [
133+
new Dotenv({
134+
path: '.env.prod', // load this now instead of the ones in '.env'
135+
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
136+
silent: true // hide any errors
137+
}),
127138
ionicWebpackFactory.getIonicEnvironmentPlugin(),
128139
ionicWebpackFactory.getCommonChunksPlugin(),
129140
new ModuleConcatPlugin(),

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"chokidar": "^1.7.0",
3838
"clean-css": "^4.1.11",
3939
"cross-spawn": "^5.1.0",
40+
"dotenv-webpack": "^1.5.7",
4041
"express": "^4.16.3",
4142
"fs-extra": "^4.0.2",
4243
"glob": "^7.1.2",

0 commit comments

Comments
 (0)