UNPKG

1.69 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
13// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
14// injected into the application via DefinePlugin in Webpack configuration.
15const REACT_APP = /^REACT_APP_/i;
16
17function getClientEnvironment(publicUrl) {
18 const raw = Object.keys(process.env)
19 .filter(key => REACT_APP.test(key))
20 .reduce(
21 (env, key) => {
22 env[key] = process.env[key];
23 return env;
24 },
25 {
26 // Useful for determining whether we’re running in production mode.
27 // Most importantly, it switches React into the correct mode.
28 NODE_ENV: process.env.NODE_ENV || 'development',
29 // Useful for resolving the correct path to static assets in `public`.
30 // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
31 // This should only be used as an escape hatch. Normally you would put
32 // images into the `src` and `import` them in code to get their paths.
33 PUBLIC_URL: publicUrl,
34 }
35 );
36 // Stringify all values so we can feed into Webpack DefinePlugin
37 const stringified = {
38 'process.env': Object.keys(raw).reduce(
39 (env, key) => {
40 env[key] = JSON.stringify(raw[key]);
41 return env;
42 },
43 {}
44 ),
45 };
46
47 return { raw, stringified };
48}
49
50module.exports = getClientEnvironment;