UNPKG

1.64 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.
15
16var REACT_APP = /^REACT_APP_/i;
17
18function getClientEnvironment(publicUrl) {
19 var raw = Object
20 .keys(process.env)
21 .filter(key => REACT_APP.test(key))
22 .reduce((env, key) => {
23 env[key] = process.env[key];
24 return env;
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 // Stringify all values so we can feed into Webpack DefinePlugin
36 var stringified = {
37 'process.env': Object
38 .keys(raw)
39 .reduce((env, key) => {
40 env[key] = JSON.stringify(raw[key]);
41 return env;
42 }, {})
43 };
44
45 return { raw, stringified };
46}
47
48module.exports = getClientEnvironment;