UNPKG

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