| 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 |
|
|---|
| 11 | const fs = require('fs');
|
|---|
| 12 | const path = require('path');
|
|---|
| 13 | const paths = require('./paths');
|
|---|
| 14 |
|
|---|
| 15 | // Make sure that including paths.js after env.js will read .env variables.
|
|---|
| 16 | delete require.cache[require.resolve('./paths')];
|
|---|
| 17 |
|
|---|
| 18 | const NODE_ENV = process.env.NODE_ENV;
|
|---|
| 19 | if (!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
|
|---|
| 26 | const dotenvFiles = [
|
|---|
| 27 | `${paths.dotenv}.${NODE_ENV}.local`,
|
|---|
| 28 | // Don't include `.env.local` for `test` environment
|
|---|
| 29 | // since normally you expect tests to produce the same
|
|---|
| 30 | // results for everyone
|
|---|
| 31 | NODE_ENV !== 'test' && `${paths.dotenv}.local`,
|
|---|
| 32 | `${paths.dotenv}.${NODE_ENV}`,
|
|---|
| 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. Variable expansion is supported in .env files.
|
|---|
| 39 | // https://github.com/motdotla/dotenv
|
|---|
| 40 | // https://github.com/motdotla/dotenv-expand
|
|---|
| 41 | dotenvFiles.forEach(dotenvFile => {
|
|---|
| 42 | if (fs.existsSync(dotenvFile)) {
|
|---|
| 43 | require('dotenv-expand')(
|
|---|
| 44 | require('dotenv').config({
|
|---|
| 45 | path: dotenvFile,
|
|---|
| 46 | })
|
|---|
| 47 | );
|
|---|
| 48 | }
|
|---|
| 49 | });
|
|---|
| 50 |
|
|---|
| 51 | // We support resolving modules according to `NODE_PATH`.
|
|---|
| 52 | // This lets you use absolute paths in imports inside large monorepos:
|
|---|
| 53 | // https://github.com/facebook/create-react-app/issues/253.
|
|---|
| 54 | // It works similar to `NODE_PATH` in Node itself:
|
|---|
| 55 | // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
|
|---|
| 56 | // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
|
|---|
| 57 | // Otherwise, we risk importing Node.js core modules into an app instead of webpack shims.
|
|---|
| 58 | // https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
|
|---|
| 59 | // We also resolve them to make sure all tools using them work consistently.
|
|---|
| 60 | const appDirectory = fs.realpathSync(process.cwd());
|
|---|
| 61 | process.env.NODE_PATH = (process.env.NODE_PATH || '')
|
|---|
| 62 | .split(path.delimiter)
|
|---|
| 63 | .filter(folder => folder && !path.isAbsolute(folder))
|
|---|
| 64 | .map(folder => path.resolve(appDirectory, folder))
|
|---|
| 65 | .join(path.delimiter);
|
|---|
| 66 |
|
|---|
| 67 | // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
|
|---|
| 68 | // 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 | .reduce(
|
|---|
| 75 | (env, key) => {
|
|---|
| 76 | env[key] = process.env[key];
|
|---|
| 77 | return env;
|
|---|
| 78 | },
|
|---|
| 79 | {
|
|---|
| 80 | // Useful for determining whether we’re running in production mode.
|
|---|
| 81 | // Most importantly, it switches React into the correct mode.
|
|---|
| 82 | NODE_ENV: process.env.NODE_ENV || 'development',
|
|---|
| 83 | // Useful for resolving the correct path to static assets in `public`.
|
|---|
| 84 | // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
|
|---|
| 85 | // This should only be used as an escape hatch. Normally you would put
|
|---|
| 86 | // images into the `src` and `import` them in code to get their paths.
|
|---|
| 87 | PUBLIC_URL: publicUrl,
|
|---|
| 88 | // We support configuring the sockjs pathname during development.
|
|---|
| 89 | // These settings let a developer run multiple simultaneous projects.
|
|---|
| 90 | // They are used as the connection `hostname`, `pathname` and `port`
|
|---|
| 91 | // in webpackHotDevClient. They are used as the `sockHost`, `sockPath`
|
|---|
| 92 | // and `sockPort` options in webpack-dev-server.
|
|---|
| 93 | WDS_SOCKET_HOST: process.env.WDS_SOCKET_HOST,
|
|---|
| 94 | WDS_SOCKET_PATH: process.env.WDS_SOCKET_PATH,
|
|---|
| 95 | WDS_SOCKET_PORT: process.env.WDS_SOCKET_PORT,
|
|---|
| 96 | // Whether or not react-refresh is enabled.
|
|---|
| 97 | // It is defined here so it is available in the webpackHotDevClient.
|
|---|
| 98 | FAST_REFRESH: process.env.FAST_REFRESH !== 'false',
|
|---|
| 99 | }
|
|---|
| 100 | );
|
|---|
| 101 | // Stringify all values so we can feed into webpack DefinePlugin
|
|---|
| 102 | const stringified = {
|
|---|
| 103 | 'process.env': Object.keys(raw).reduce((env, key) => {
|
|---|
| 104 | env[key] = JSON.stringify(raw[key]);
|
|---|
| 105 | return env;
|
|---|
| 106 | }, {}),
|
|---|
| 107 | };
|
|---|
| 108 |
|
|---|
| 109 | return { raw, stringified };
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | module.exports = getClientEnvironment;
|
|---|