1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 const fs = require('fs');
8 const path = require('path');
9 const paths = require('./paths');
11 // Make sure that including paths.js after env.js will read .env variables.
12 delete require.cache[require.resolve('./paths')];
14 const NODE_ENV = process.env.NODE_ENV;
17 'The NODE_ENV environment variable is required but was not specified.'
21 // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
23 `${paths.dotenv}.${NODE_ENV}.local`,
24 // Don't include `.env.local` for `test` environment
25 // since normally you expect tests to produce the same
26 // results for everyone
27 NODE_ENV !== 'test' && `${paths.dotenv}.local`,
28 `${paths.dotenv}.${NODE_ENV}`,
32 // Load environment variables from .env* files. Suppress warnings using silent
33 // if this file is missing. dotenv will never modify any environment variables
34 // that have already been set. Variable expansion is supported in .env files.
35 // https://github.com/motdotla/dotenv
36 // https://github.com/motdotla/dotenv-expand
37 dotenvFiles.forEach(dotenvFile => {
38 if (fs.existsSync(dotenvFile)) {
39 require('dotenv-expand')(
40 require('dotenv').config({
47 // We support resolving modules according to `NODE_PATH`.
48 // This lets you use absolute paths in imports inside large monorepos:
49 // https://github.com/facebook/create-react-app/issues/253.
50 // It works similar to `NODE_PATH` in Node itself:
51 // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
52 // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
53 // Otherwise, we risk importing Node.js core modules into an app instead of webpack shims.
54 // https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
55 // We also resolve them to make sure all tools using them work consistently.
56 const appDirectory = fs.realpathSync(process.cwd());
57 process.env.NODE_PATH = (process.env.NODE_PATH || '')
58 .split(path.delimiter)
59 .filter(folder => folder && !path.isAbsolute(folder))
60 .map(folder => path.resolve(appDirectory, folder))
61 .join(path.delimiter);
63 // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
64 // injected into the application via DefinePlugin in webpack configuration.
65 const REACT_APP = /^REACT_APP_/i;
67 function getClientEnvironment(publicUrl) {
68 const raw = Object.keys(process.env)
69 .filter(key => REACT_APP.test(key))
72 env[key] = process.env[key];
76 // Useful for determining whether we’re running in production mode.
77 // Most importantly, it switches React into the correct mode.
78 NODE_ENV: process.env.NODE_ENV || 'development',
79 // Useful for resolving the correct path to static assets in `public`.
80 // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
81 // This should only be used as an escape hatch. Normally you would put
82 // images into the `src` and `import` them in code to get their paths.
83 PUBLIC_URL: publicUrl,
84 // We support configuring the sockjs pathname during development.
85 // These settings let a developer run multiple simultaneous projects.
86 // They are used as the connection `hostname`, `pathname` and `port`
87 // in webpackHotDevClient. They are used as the `sockHost`, `sockPath`
88 // and `sockPort` options in webpack-dev-server.
89 WDS_SOCKET_HOST: process.env.WDS_SOCKET_HOST,
90 WDS_SOCKET_PATH: process.env.WDS_SOCKET_PATH,
91 WDS_SOCKET_PORT: process.env.WDS_SOCKET_PORT,
92 // Whether or not react-refresh is enabled.
93 // It is defined here so it is available in the webpackHotDevClient.
94 FAST_REFRESH: process.env.FAST_REFRESH !== 'false',
97 // Stringify all values so we can feed into webpack DefinePlugin
99 'process.env': Object.keys(raw).reduce((env, key) => {
100 env[key] = JSON.stringify(raw[key]);
105 return { raw, stringified };
108 module.exports = getClientEnvironment;