Skip to content

Commit

Permalink
support different env configs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tuchk4 committed Mar 15, 2017
1 parent 9512d97 commit fd86bdd
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 23 deletions.
53 changes: 53 additions & 0 deletions packages/react-scripts/config/loadEnv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// @remove-on-eject-begin
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
// @remove-on-eject-end
'use strict';

// Load environment variables from .env* files. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set.
// https://github.com/motdotla/dotenv
// Used .env* files - https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
var fs = require('fs');
var paths = require('./paths');

var sequence = {
'development': [
paths.dotenvDevelopmentLocal,
paths.dotenvDevelopment,
paths.dotenvLocal,
paths.dotenv
],
'test': [
paths.dotenvTestLocal,
paths.dotenvTest,
paths.dotenvLocal,
paths.dotenv
],
'production': [
paths.dotenvProductionLocal,
paths.dotenvProduction,
paths.dotenvLocal,
paths.dotenv
]
};

var envConfigs = sequence[process.env.NODE_ENV];

if (envConfigs) {
envConfigs.forEach(envConfig => {
if (fs.existsSync(envConfig)) {
require('dotenv').config({
silent: true,
path: envConfig
});
}
});
}
24 changes: 24 additions & 0 deletions packages/react-scripts/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ function getServedPath(appPackageJson) {

// config after eject: we're in ./config/
module.exports = {
dotenv: resolveApp('.env'),
dotenvLocal: resolveApp('.env.local'),
dotenvDevelopment: resolveApp('.env.development'),
dotenvDevelopmentLocal: resolveApp('.env.development.local'),
dotenvTest: resolveApp('.env.test'),
dotenvTestLocal: resolveApp('.env.test.local'),
dotenvProduction: resolveApp('.env.production'),
dotenvProductionLocal: resolveApp('.env.production.local'),
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
Expand All @@ -96,6 +104,14 @@ function resolveOwn(relativePath) {
// config before eject: we're in ./node_modules/react-scripts/config/
module.exports = {
appPath: resolveApp('.'),
dotenv: resolveApp('.env'),
dotenvLocal: resolveApp('.env.local'),
dotenvDevelopment: resolveApp('.env.development'),
dotenvDevelopmentLocal: resolveApp('.env.development.local'),
dotenvTest: resolveApp('.env.test'),
dotenvTestLocal: resolveApp('.env.test.local'),
dotenvProduction: resolveApp('.env.production'),
dotenvProductionLocal: resolveApp('.env.production.local'),
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
Expand Down Expand Up @@ -125,6 +141,14 @@ if (
) {
module.exports = {
appPath: resolveApp('.'),
dotenv: resolveOwn('template/.env'),
dotenvLocal: resolveOwn('template/.env.local'),
dotenvDevelopment: resolveOwn('template/.env.development'),
dotenvDevelopmentLocal: resolveOwn('template/.env.development.local'),
dotenvTest: resolveOwn('template/.env.test'),
dotenvTestLocal: resolveOwn('template/.env.test.local'),
dotenvProduction: resolveOwn('template/.env.production'),
dotenvProductionLocal: resolveOwn('template/.env.production.local'),
appBuild: resolveOwn('../../build'),
appPublic: resolveOwn('template/public'),
appHtml: resolveOwn('template/public/index.html'),
Expand Down
4 changes: 3 additions & 1 deletion packages/react-scripts/fixtures/kitchensink/.env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
REACT_APP_FILE_ENV_MESSAGE=fromtheenvfile
REACT_APP_X = x-from-original-env
REACT_APP_ORIGINAL_1 = from-original-env-1
REACT_APP_ORIGINAL_2 = from-original-env-2
2 changes: 2 additions & 0 deletions packages/react-scripts/fixtures/kitchensink/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_X = x-from-development-env
REACT_APP_DEVELOPMENT = development
2 changes: 2 additions & 0 deletions packages/react-scripts/fixtures/kitchensink/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_X = x-from-original-local-env
REACT_APP_ORIGINAL_2 = override-from-original-local-env-2
2 changes: 2 additions & 0 deletions packages/react-scripts/fixtures/kitchensink/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_X = x-from-production-env
REACT_APP_PRODUCTION = production
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ describe('Integration', () => {
it('file env variables', async () => {
const doc = await initDOM('file-env-variables');

expect(
doc.getElementById('feature-file-env-variables').textContent
).to.equal('fromtheenvfile.');
expect(doc.getElementById('feature-file-env-original-1').textContent).to.equal('from-original-env-1');
expect(doc.getElementById('feature-file-env-original-2').textContent).to.equal('override-from-original-local-env-2');

if (process.env.NODE_ENV === 'production') {
expect(doc.getElementById('feature-file-env').textContent).to.equal('production')
expect(doc.getElementById('feature-file-env-x').textContent).to.equal('x-from-production-env')
} else {
expect(doc.getElementById('feature-file-env').textContent).to.equal('development')
expect(doc.getElementById('feature-file-env-x').textContent).to.equal('x-from-development-env')
}
});

it('NODE_PATH', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import React from 'react';

export default () => (
<span id="feature-file-env-variables">
{process.env.REACT_APP_FILE_ENV_MESSAGE}.
<span>
<span id="feature-file-env-original-1">{process.env.REACT_APP_ORIGINAL_1}</span>
<span id="feature-file-env-original-2">{process.env.REACT_APP_ORIGINAL_2}</span>
<span id="feature-file-env">{process.env.REACT_APP_DEVELOPMENT}{process.env.REACT_APP_PRODUCTION}</span>
<span id="feature-file-env-x">{process.env.REACT_APP_X}</span>
</span>
);
6 changes: 1 addition & 5 deletions packages/react-scripts/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ process.on('unhandledRejection', err => {
throw err;
});

// Load environment variables from .env file. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set.
// https://github.com/motdotla/dotenv
require('dotenv').config({ silent: true });
require('../config/loadEnv');

const chalk = require('chalk');
const fs = require('fs-extra');
Expand Down
6 changes: 1 addition & 5 deletions packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ process.on('unhandledRejection', err => {

process.env.NODE_ENV = 'development';

// Load environment variables from .env file. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set.
// https://github.com/motdotla/dotenv
require('dotenv').config({ silent: true });
require('../config/loadEnv');

const fs = require('fs');
const chalk = require('chalk');
Expand Down
6 changes: 1 addition & 5 deletions packages/react-scripts/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ process.on('unhandledRejection', err => {
throw err;
});

// Load environment variables from .env file. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set.
// https://github.com/motdotla/dotenv
require('dotenv').config({ silent: true });
require('../config/loadEnv');

const jest = require('jest');
const argv = process.argv.slice(2);
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/template/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_X = 1
15 changes: 15 additions & 0 deletions packages/react-scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,21 @@ To define permanent environment variables, create a file called `.env` in the ro
REACT_APP_SECRET_CODE=abcdef
```

What .env* files are used?

* `.env` - Default
* `.env.development`, `.env.test`, `.env.production` - Environment-specific settings.
* `.env.local` - Local overrides. This file is loaded for all environments except test.
* `.env.development.local`, `.env.test.local`, `.env.production.local` - Local overrides of environment-specific settings.

Files priority (file is skipped if does not exist):

* npm test - `.env.test.local`, `env.test`, `.env.local`, `.env`
* npm run build - `.env.production.local`, `env.production`, `.env.local`, `.env`
* npm start - `.env.development.local`, `env.development`, `.env.local`, `.env`

Priority from left to right.

These variables will act as the defaults if the machine does not explicitly set them.<br>
Please refer to the [dotenv documentation](https://github.com/motdotla/dotenv) for more details.

Expand Down
7 changes: 5 additions & 2 deletions packages/react-scripts/template/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

0 comments on commit fd86bdd

Please sign in to comment.