UNPKG

@oriflame/config-webpack

Version:
56 lines (41 loc) 1.89 kB
'use strict'; const dotenv = require('dotenv'); const dotenvExpand = require('dotenv-expand'); const fs = require('fs'); const path = require('path'); const _interopDefault = e => e && e.__esModule ? e : { default: e }; const dotenv__default = /*#__PURE__*/_interopDefault(dotenv); const fs__default = /*#__PURE__*/_interopDefault(fs); const path__default = /*#__PURE__*/_interopDefault(path); const rootEnvFile = path__default.default.join(process.cwd(), '.env'); // Make sure that including paths.js after env.js will read .env variables. delete require.cache[require.resolve('./constants')]; // Grab NODE_ENV and REACT_* environment variables and prepare them to be // injected into the application via DefinePlugin in Webpack configuration. const REACT_APP = /^react_app_/i; function getClientEnvironment(mode) { const NODE_ENV = mode; const dotenvFiles = [`${rootEnvFile}.${NODE_ENV}.local`, `${rootEnvFile}.${NODE_ENV}`, rootEnvFile]; dotenvFiles.forEach(dotenvFile => { if (fs__default.default.existsSync(dotenvFile)) { dotenvExpand.expand(dotenv__default.default.config({ path: dotenvFile })); } }); const raw = Object.keys(process.env).filter(key => REACT_APP.test(key)).reduce((env, key) => { // eslint-disable-next-line no-param-reassign -- we need to mutate the new object env[key] = process.env[key]; return env; }, { // Useful for determining whether we’re running in production mode. // Most importantly, it switches React into the correct mode. NODE_ENV: process.env.NODE_ENV ?? 'development' }); // Stringify all values so we can feed into Webpack DefinePlugin const stringified = { 'process.env': Object.fromEntries(Object.keys(raw).map(key => [key, JSON.stringify(raw[key])])) }; return stringified; } module.exports = getClientEnvironment; //# sourceMappingURL=env2.js.map