UNPKG

3.73 kBJavaScriptView Raw
1// @remove-on-eject-begin
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 * All rights reserved.
5 *
6 * This source code is licensed under the BSD-style license found in the
7 * LICENSE file in the root directory of this source tree. An additional grant
8 * of patent rights can be found in the PATENTS file in the same directory.
9 */
10// @remove-on-eject-end
11'use strict';
12
13const fs = require('fs');
14const path = require('path');
15const paths = require('./paths');
16
17// Make sure that including paths.js after env.js will read .env variables.
18delete require.cache[require.resolve('./paths')];
19
20const NODE_ENV = process.env.NODE_ENV;
21if (!NODE_ENV) {
22 throw new Error(
23 'The NODE_ENV environment variable is required but was not specified.'
24 );
25}
26
27// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
28var dotenvFiles = [
29 `${paths.dotenv}.${NODE_ENV}.local`,
30 `${paths.dotenv}.${NODE_ENV}`,
31 // Don't include `.env.local` for `test` environment
32 // since normally you expect tests to produce the same
33 // results for everyone
34 NODE_ENV !== 'test' && `${paths.dotenv}.local`,
35 paths.dotenv,
36].filter(Boolean);
37
38// Load environment variables from .env* files. Suppress warnings using silent
39// if this file is missing. dotenv will never modify any environment variables
40// that have already been set.
41// https://github.com/motdotla/dotenv
42dotenvFiles.forEach(dotenvFile => {
43 if (fs.existsSync(dotenvFile)) {
44 require('dotenv').config({
45 path: dotenvFile,
46 });
47 }
48});
49
50// We support resolving modules according to `NODE_PATH`.
51// This lets you use absolute paths in imports inside large monorepos:
52// https://github.com/facebookincubator/create-react-app/issues/253.
53// It works similar to `NODE_PATH` in Node itself:
54// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
55// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
56// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
57// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
58// We also resolve them to make sure all tools using them work consistently.
59const appDirectory = fs.realpathSync(process.cwd());
60process.env.NODE_PATH = (process.env.NODE_PATH || '')
61 .split(path.delimiter)
62 .filter(folder => folder && !path.isAbsolute(folder))
63 .map(folder => path.resolve(appDirectory, folder))
64 .join(path.delimiter);
65
66// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
67// injected into the application via DefinePlugin in Webpack configuration.
68const REACT_APP = /^REACT_APP_/i;
69
70function getClientEnvironment(publicUrl) {
71 const raw = Object.keys(process.env)
72 .filter(key => REACT_APP.test(key))
73 .reduce(
74 (env, key) => {
75 env[key] = process.env[key];
76 return env;
77 },
78 {
79 // Useful for determining whether we’re running in production mode.
80 // Most importantly, it switches React into the correct mode.
81 NODE_ENV: process.env.NODE_ENV || 'development',
82 // Useful for resolving the correct path to static assets in `public`.
83 // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
84 // This should only be used as an escape hatch. Normally you would put
85 // images into the `src` and `import` them in code to get their paths.
86 PUBLIC_URL: publicUrl,
87 }
88 );
89 // Stringify all values so we can feed into Webpack DefinePlugin
90 const stringified = {
91 'process.env': Object.keys(raw).reduce((env, key) => {
92 env[key] = JSON.stringify(raw[key]);
93 return env;
94 }, {}),
95 };
96
97 return { raw, stringified };
98}
99
100module.exports = getClientEnvironment;