env.js 3.65 KB
Newer Older
1
2
3
4
// @remove-on-eject-begin
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
Sophie Alpert's avatar
Sophie Alpert committed
5
6
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
7
8
 */
// @remove-on-eject-end
9
'use strict';
10

Danil Shashkov's avatar
Danil Shashkov committed
11
const fs = require('fs');
12
const path = require('path');
Danil Shashkov's avatar
Danil Shashkov committed
13
const paths = require('./paths');
14

15
16
17
// Make sure that including paths.js after env.js will read .env variables.
delete require.cache[require.resolve('./paths')];

Danil Shashkov's avatar
Danil Shashkov committed
18
const NODE_ENV = process.env.NODE_ENV;
19
20
21
22
23
24
25
26
if (!NODE_ENV) {
  throw new Error(
    'The NODE_ENV environment variable is required but was not specified.'
  );
}

// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
var dotenvFiles = [
Danil Shashkov's avatar
Danil Shashkov committed
27
28
  `${paths.dotenv}.${NODE_ENV}.local`,
  `${paths.dotenv}.${NODE_ENV}`,
29
30
31
32
  // Don't include `.env.local` for `test` environment
  // since normally you expect tests to produce the same
  // results for everyone
  NODE_ENV !== 'test' && `${paths.dotenv}.local`,
33
  paths.dotenv,
34
35
].filter(Boolean);

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

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// We support resolving modules according to `NODE_PATH`.
// This lets you use absolute paths in imports inside large monorepos:
// https://github.com/facebookincubator/create-react-app/issues/253.
// It works similar to `NODE_PATH` in Node itself:
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
// We also resolve them to make sure all tools using them work consistently.
const appDirectory = fs.realpathSync(process.cwd());
process.env.NODE_PATH = (process.env.NODE_PATH || '')
  .split(path.delimiter)
  .filter(folder => folder && !path.isAbsolute(folder))
  .map(folder => path.resolve(appDirectory, folder))
  .join(path.delimiter);

67
68
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
// injected into the application via DefinePlugin in Webpack configuration.
69
const REACT_APP = /^REACT_APP_/i;
70
71

function getClientEnvironment(publicUrl) {
72
  const raw = Object.keys(process.env)
73
    .filter(key => REACT_APP.test(key))
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    .reduce(
      (env, key) => {
        env[key] = process.env[key];
        return env;
      },
      {
        // Useful for determining whether we’re running in production mode.
        // Most importantly, it switches React into the correct mode.
        NODE_ENV: process.env.NODE_ENV || 'development',
        // Useful for resolving the correct path to static assets in `public`.
        // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
        // This should only be used as an escape hatch. Normally you would put
        // images into the `src` and `import` them in code to get their paths.
        PUBLIC_URL: publicUrl,
      }
    );
90
  // Stringify all values so we can feed into Webpack DefinePlugin
91
  const stringified = {
92
93
94
95
    'process.env': Object.keys(raw).reduce((env, key) => {
      env[key] = JSON.stringify(raw[key]);
      return env;
    }, {}),
96
97
98
  };

  return { raw, stringified };
99
100
101
}

module.exports = getClientEnvironment;