Skip to content

Commit 7867de1

Browse files
tuchk4gaearon
authored andcommitted
Feature/different env config files facebook#1343 (facebook#1344)
* support different env configs. * fomrat code * Hide doc * Slightly rework the PR * Remove .env in default template * Use just one entry in the paths * Unify env.js and loadEnv.js * Oops, forgot these folks
1 parent 845d618 commit 7867de1

File tree

13 files changed

+103
-22
lines changed

13 files changed

+103
-22
lines changed

config/env.js

+30
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@
1010
// @remove-on-eject-end
1111
'use strict';
1212

13+
var fs = require('fs');
14+
var paths = require('./paths');
15+
16+
var NODE_ENV = process.env.NODE_ENV;
17+
if (!NODE_ENV) {
18+
throw new Error(
19+
'The NODE_ENV environment variable is required but was not specified.'
20+
);
21+
}
22+
23+
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
24+
var dotenvFiles = [
25+
paths.dotenv + '.' + NODE_ENV + '.local',
26+
paths.dotenv + '.' + NODE_ENV,
27+
paths.dotenv + '.local',
28+
paths.dotenv,
29+
];
30+
// Load environment variables from .env* files. Suppress warnings using silent
31+
// if this file is missing. dotenv will never modify any environment variables
32+
// that have already been set.
33+
// https://github.com/motdotla/dotenv
34+
dotenvFiles.forEach(dotenvFile => {
35+
if (fs.existsSync(dotenvFile)) {
36+
require('dotenv').config({
37+
silent: true,
38+
path: dotenvFile,
39+
});
40+
}
41+
});
42+
1343
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
1444
// injected into the application via DefinePlugin in Webpack configuration.
1545
const REACT_APP = /^REACT_APP_/i;

config/paths.js

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function getServedPath(appPackageJson) {
7474

7575
// config after eject: we're in ./config/
7676
module.exports = {
77+
dotenv: resolveApp('.env'),
7778
appBuild: resolveApp('build'),
7879
appPublic: resolveApp('public'),
7980
appHtml: resolveApp('public/index.html'),
@@ -95,6 +96,7 @@ function resolveOwn(relativePath) {
9596

9697
// config before eject: we're in ./node_modules/react-scripts/config/
9798
module.exports = {
99+
dotenv: resolveApp('.env'),
98100
appPath: resolveApp('.'),
99101
appBuild: resolveApp('build'),
100102
appPublic: resolveApp('public'),
@@ -124,6 +126,7 @@ if (
124126
__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
125127
) {
126128
module.exports = {
129+
dotenv: resolveOwn('template/.env'),
127130
appPath: resolveApp('.'),
128131
appBuild: resolveOwn('../../build'),
129132
appPublic: resolveOwn('template/public'),

fixtures/kitchensink/.env

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
REACT_APP_FILE_ENV_MESSAGE=fromtheenvfile
1+
REACT_APP_X = x-from-original-env
2+
REACT_APP_ORIGINAL_1 = from-original-env-1
3+
REACT_APP_ORIGINAL_2 = from-original-env-2

fixtures/kitchensink/.env.development

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
REACT_APP_X = x-from-development-env
2+
REACT_APP_DEVELOPMENT = development

fixtures/kitchensink/.env.local

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
REACT_APP_X = x-from-original-local-env
2+
REACT_APP_ORIGINAL_2 = override-from-original-local-env-2

fixtures/kitchensink/.env.production

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
REACT_APP_X = x-from-production-env
2+
REACT_APP_PRODUCTION = production

fixtures/kitchensink/integration/env.test.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,27 @@ describe('Integration', () => {
1616
const doc = await initDOM('file-env-variables');
1717

1818
expect(
19-
doc.getElementById('feature-file-env-variables').textContent
20-
).to.equal('fromtheenvfile.');
19+
doc.getElementById('feature-file-env-original-1').textContent
20+
).to.equal('from-original-env-1');
21+
expect(
22+
doc.getElementById('feature-file-env-original-2').textContent
23+
).to.equal('override-from-original-local-env-2');
24+
25+
if (process.env.NODE_ENV === 'production') {
26+
expect(doc.getElementById('feature-file-env').textContent).to.equal(
27+
'production'
28+
);
29+
expect(doc.getElementById('feature-file-env-x').textContent).to.equal(
30+
'x-from-production-env'
31+
);
32+
} else {
33+
expect(doc.getElementById('feature-file-env').textContent).to.equal(
34+
'development'
35+
);
36+
expect(doc.getElementById('feature-file-env-x').textContent).to.equal(
37+
'x-from-development-env'
38+
);
39+
}
2140
});
2241

2342
it('NODE_PATH', async () => {

fixtures/kitchensink/src/features/env/FileEnvVariables.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010
import React from 'react';
1111

1212
export default () => (
13-
<span id="feature-file-env-variables">
14-
{process.env.REACT_APP_FILE_ENV_MESSAGE}.
13+
<span>
14+
<span id="feature-file-env-original-1">
15+
{process.env.REACT_APP_ORIGINAL_1}
16+
</span>
17+
<span id="feature-file-env-original-2">
18+
{process.env.REACT_APP_ORIGINAL_2}
19+
</span>
20+
<span id="feature-file-env">
21+
{process.env.REACT_APP_DEVELOPMENT}{process.env.REACT_APP_PRODUCTION}
22+
</span>
23+
<span id="feature-file-env-x">{process.env.REACT_APP_X}</span>
1524
</span>
1625
);

scripts/build.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ process.on('unhandledRejection', err => {
2020
throw err;
2121
});
2222

23-
// Load environment variables from .env file. Suppress warnings using silent
24-
// if this file is missing. dotenv will never modify any environment variables
25-
// that have already been set.
26-
// https://github.com/motdotla/dotenv
27-
require('dotenv').config({ silent: true });
23+
// Ensure environment variables are read.
24+
require('../config/env');
2825

2926
const chalk = require('chalk');
3027
const fs = require('fs-extra');

scripts/start.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ process.on('unhandledRejection', err => {
1919

2020
process.env.NODE_ENV = 'development';
2121

22-
// Load environment variables from .env file. Suppress warnings using silent
23-
// if this file is missing. dotenv will never modify any environment variables
24-
// that have already been set.
25-
// https://github.com/motdotla/dotenv
26-
require('dotenv').config({ silent: true });
22+
// Ensure environment variables are read.
23+
require('../config/env');
2724

2825
const fs = require('fs');
2926
const chalk = require('chalk');

scripts/test.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ process.on('unhandledRejection', err => {
2020
throw err;
2121
});
2222

23-
// Load environment variables from .env file. Suppress warnings using silent
24-
// if this file is missing. dotenv will never modify any environment variables
25-
// that have already been set.
26-
// https://github.com/motdotla/dotenv
27-
require('dotenv').config({ silent: true });
23+
// Ensure environment variables are read.
24+
require('../config/env');
2825

2926
const jest = require('jest');
3027
const argv = process.argv.slice(2);

template/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,24 @@ To define permanent environment variables, create a file called `.env` in the ro
739739
REACT_APP_SECRET_CODE=abcdef
740740
```
741741

742+
<!--
743+
TODO: uncomment (and tweak) the doc for 0.10
744+
What .env* files are used?
745+
746+
* `.env` - Default
747+
* `.env.development`, `.env.test`, `.env.production` - Environment-specific settings.
748+
* `.env.local` - Local overrides. This file is loaded for all environments except test.
749+
* `.env.development.local`, `.env.test.local`, `.env.production.local` - Local overrides of environment-specific settings.
750+
751+
Files priority (file is skipped if does not exist):
752+
753+
* npm test - `.env.test.local`, `env.test`, `.env.local`, `.env`
754+
* npm run build - `.env.production.local`, `env.production`, `.env.local`, `.env`
755+
* npm start - `.env.development.local`, `env.development`, `.env.local`, `.env`
756+
757+
Priority from left to right.
758+
-->
759+
742760
These variables will act as the defaults if the machine does not explicitly set them.<br>
743761
Please refer to the [dotenv documentation](https://github.com/motdotla/dotenv) for more details.
744762

template/gitignore

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
# misc
1313
.DS_Store
14-
.env
14+
.env.local
15+
.env.development.local
16+
.env.test.local
17+
.env.production.local
18+
1519
npm-debug.log*
1620
yarn-debug.log*
1721
yarn-error.log*
18-

0 commit comments

Comments
 (0)