UNPKG

casterly

Version:
78 lines (77 loc) 3.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const paths_1 = __importDefault(require("./paths")); // Make sure that including paths.js after env.js will read .env variables. delete require.cache[require.resolve('./paths')]; if (!process.env.NODE_ENV) { process.env.NODE_ENV = 'production'; } const NODE_ENV = process.env.NODE_ENV; // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use const dotenvFiles = [ `${paths_1.default.dotenv}.${NODE_ENV}.local`, `${paths_1.default.dotenv}.${NODE_ENV}`, // Don't include `.env.local` for `test` environment // since normally you expect tests to produce the same // results for everyone NODE_ENV !== 'test' && `${paths_1.default.dotenv}.local`, paths_1.default.dotenv, ].filter(Boolean); // Load environment variables from .env* files. Suppress warnings using silent // if this file is missing. dotenv will never modify any environment variables // that have already been set. Variable expansion is supported in .env files. // https://github.com/motdotla/dotenv // https://github.com/motdotla/dotenv-expand dotenvFiles.forEach((dotenvFile) => { if (fs_1.default.existsSync(dotenvFile)) { require('dotenv-expand')(require('dotenv').config({ path: dotenvFile, })); } }); // We support resolving modules according to `NODE_PATH`. // This lets you use absolute paths in imports inside large monorepos: // https://github.com/facebook/create-react-app/issues/253. // It works similar to `NODE_PATH` in Node itself: // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored. // Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims. // https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421 // We also resolve them to make sure all tools using them work consistently. const appDirectory = fs_1.default.realpathSync(process.cwd()); process.env.NODE_PATH = (process.env.NODE_PATH || '') .split(path_1.default.delimiter) .filter((folder) => folder && !path_1.default.isAbsolute(folder)) .map((folder) => path_1.default.resolve(appDirectory, folder)) .join(path_1.default.delimiter); // Grab NODE_ENV and CASTERLY_PUBLIC_* environment variables and prepare them to be // injected into the application via DefinePlugin in Webpack configuration. const CASTERLY_PUBLIC = /^CASTERLY_PUBLIC_/i; // eslint-disable-next-line @typescript-eslint/no-unused-vars function getClientEnvironment({ isServer = false, worker = false } = {}) { const baseEnv = { // 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, }; const raw = Object.keys(process.env) .filter((key) => CASTERLY_PUBLIC.test(key)) .reduce((env, key) => { env[key] = process.env[key]; return env; }, baseEnv); // Stringify all values so we can feed into Webpack DefinePlugin const stringified = Object.assign(Object.assign({}, Object.keys(raw).reduce((env, key) => { env['process.env.' + key] = JSON.stringify(raw[key]); return env; }, {})), { 'process.env.ASSET_PATH': '/_casterly/', 'process.browser': JSON.stringify(!isServer), // Allow browser-only and server-only code to be eliminated 'typeof window': JSON.stringify(isServer || worker ? 'undefined' : 'object') }); return { raw, stringified }; } exports.default = getClientEnvironment;