Skip to content

Commit a56cfdd

Browse files
moosPavel Zhytko
authored and
Pavel Zhytko
committed
fix facebook#2223 - [feature] Implement dotenv-expand to accept variable expa… (facebook#3387)
* fix facebook#2223 - [feature] Implement dotenv-expand to accept variable expansion in dot env files * add to README TOC * fix readme * Update README.md
1 parent 00ecb61 commit a56cfdd

File tree

8 files changed

+91
-4
lines changed

8 files changed

+91
-4
lines changed

packages/react-scripts/config/env.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ var dotenvFiles = [
3535

3636
// Load environment variables from .env* files. Suppress warnings using silent
3737
// if this file is missing. dotenv will never modify any environment variables
38-
// that have already been set.
38+
// that have already been set. Variable expansion is supported in .env files.
3939
// https://github.com/motdotla/dotenv
40+
// https://github.com/motdotla/dotenv-expand
4041
dotenvFiles.forEach(dotenvFile => {
4142
if (fs.existsSync(dotenvFile)) {
42-
require('dotenv').config({
43-
path: dotenvFile,
44-
});
43+
require('dotenv-expand')(
44+
require('dotenv').config({
45+
path: dotenvFile,
46+
})
47+
);
4548
}
4649
});
4750

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
REACT_APP_X = x-from-original-env
22
REACT_APP_ORIGINAL_1 = from-original-env-1
33
REACT_APP_ORIGINAL_2 = from-original-env-2
4+
REACT_APP_BASIC = basic
5+
REACT_APP_BASIC_EXPAND = ${REACT_APP_BASIC}
6+
REACT_APP_BASIC_EXPAND_SIMPLE = $REACT_APP_BASIC
7+
REACT_APP_EXPAND_EXISTING = $REACT_APP_SHELL_ENV_MESSAGE

packages/react-scripts/fixtures/kitchensink/integration/env.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,22 @@ describe('Integration', () => {
6767
doc.getElementById('feature-shell-env-variables').textContent
6868
).to.equal('fromtheshell.');
6969
});
70+
71+
it('expand .env variables', async () => {
72+
const doc = await initDOM('expand-env-variables');
73+
74+
expect(doc.getElementById('feature-expand-env-1').textContent).to.equal(
75+
'basic'
76+
);
77+
expect(doc.getElementById('feature-expand-env-2').textContent).to.equal(
78+
'basic'
79+
);
80+
expect(doc.getElementById('feature-expand-env-3').textContent).to.equal(
81+
'basic'
82+
);
83+
expect(
84+
doc.getElementById('feature-expand-env-existing').textContent
85+
).to.equal('fromtheshell');
86+
});
7087
});
7188
});

packages/react-scripts/fixtures/kitchensink/src/App.js

+5
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ class App extends Component {
179179
this.setFeature(f.default)
180180
);
181181
break;
182+
case 'expand-env-variables':
183+
import('./features/env/ExpandEnvVariables').then(f =>
184+
this.setFeature(f.default)
185+
);
186+
break;
182187
default:
183188
throw new Error(`Missing feature "${feature}"`);
184189
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React from 'react';
9+
10+
export default () => (
11+
<span>
12+
<span id="feature-expand-env-1">{process.env.REACT_APP_BASIC}</span>
13+
<span id="feature-expand-env-2">{process.env.REACT_APP_BASIC_EXPAND}</span>
14+
<span id="feature-expand-env-3">
15+
{process.env.REACT_APP_BASIC_EXPAND_SIMPLE}
16+
</span>
17+
<span id="feature-expand-env-existing">
18+
{process.env.REACT_APP_EXPAND_EXISTING}
19+
</span>
20+
</span>
21+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React from 'react';
9+
import ReactDOM from 'react-dom';
10+
import ExpandEnvVariables from './ExpandEnvVariables';
11+
12+
describe('expand .env variables', () => {
13+
it('renders without crashing', () => {
14+
const div = document.createElement('div');
15+
ReactDOM.render(<ExpandEnvVariables />, div);
16+
});
17+
});

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"chalk": "1.1.3",
3333
"css-loader": "0.28.7",
3434
"dotenv": "4.0.0",
35+
"dotenv-expand": "4.0.1",
3536
"eslint": "4.10.0",
3637
"eslint-config-react-app": "^2.0.1",
3738
"eslint-loader": "1.9.0",

packages/react-scripts/template/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,25 @@ Please refer to the [dotenv documentation](https://github.com/motdotla/dotenv) f
978978
>Note: If you are defining environment variables for development, your CI and/or hosting platform will most likely need
979979
these defined as well. Consult their documentation how to do this. For example, see the documentation for [Travis CI](https://docs.travis-ci.com/user/environment-variables/) or [Heroku](https://devcenter.heroku.com/articles/config-vars).
980980

981+
#### Expanding Environment Variables In `.env`
982+
983+
>Note: this feature is available with `[email protected]` and higher.
984+
985+
Expand variables already on your machine for use in your .env file (using [dotenv-expand](https://github.com/motdotla/dotenv-expand)). See [#2223](https://github.com/facebookincubator/create-react-app/issues/2223).
986+
987+
For example, to get the environment variable `npm_package_version`:
988+
```
989+
REACT_APP_VERSION=$npm_package_version
990+
# also works:
991+
# REACT_APP_VERSION=${npm_package_version}
992+
```
993+
Or expand variables local to the current `.env` file:
994+
```
995+
DOMAIN=www.example.com
996+
REACT_APP_FOO=$DOMAIN/foo
997+
REACT_APP_BAR=$DOMAIN/bar
998+
```
999+
9811000
## Can I Use Decorators?
9821001

9831002
Many popular libraries use [decorators](https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841) in their documentation.<br>

0 commit comments

Comments
 (0)