|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import chalk from 'chalk' |
| 4 | +import dotenvExpand from 'next/dist/compiled/dotenv-expand' |
| 5 | +import dotenv, { DotenvConfigOutput } from 'next/dist/compiled/dotenv' |
| 6 | +import findUp from 'find-up' |
| 7 | + |
| 8 | +export type Env = { [key: string]: string } |
| 9 | + |
| 10 | +export function loadEnvConfig(dir: string, dev?: boolean): Env | false { |
| 11 | + const packageJson = findUp.sync('package.json', { cwd: dir }) |
| 12 | + |
| 13 | + // only do new env loading if dotenv isn't installed since we |
| 14 | + // can't check for an experimental flag in next.config.js |
| 15 | + // since we want to load the env before loading next.config.js |
| 16 | + if (packageJson) { |
| 17 | + const { dependencies, devDependencies } = require(packageJson) |
| 18 | + const allPackages = Object.keys({ |
| 19 | + ...dependencies, |
| 20 | + ...devDependencies, |
| 21 | + }) |
| 22 | + |
| 23 | + if (allPackages.some(pkg => pkg === 'dotenv')) { |
| 24 | + return false |
| 25 | + } |
| 26 | + } else { |
| 27 | + // we should always have a package.json but disable in case we don't |
| 28 | + return false |
| 29 | + } |
| 30 | + |
| 31 | + const isTest = process.env.NODE_ENV === 'test' |
| 32 | + const mode = isTest ? 'test' : dev ? 'development' : 'production' |
| 33 | + const dotenvFiles = [ |
| 34 | + `.env.${mode}.local`, |
| 35 | + `.env.${mode}`, |
| 36 | + // Don't include `.env.local` for `test` environment |
| 37 | + // since normally you expect tests to produce the same |
| 38 | + // results for everyone |
| 39 | + mode !== 'test' && `.env.local`, |
| 40 | + '.env', |
| 41 | + ].filter(Boolean) as string[] |
| 42 | + |
| 43 | + const combinedEnv: Env = { |
| 44 | + ...(process.env as any), |
| 45 | + } |
| 46 | + |
| 47 | + for (const envFile of dotenvFiles) { |
| 48 | + // only load .env if the user provided has an env config file |
| 49 | + const dotEnvPath = path.join(dir, envFile) |
| 50 | + |
| 51 | + try { |
| 52 | + const contents = fs.readFileSync(dotEnvPath, 'utf8') |
| 53 | + let result: DotenvConfigOutput = {} |
| 54 | + result.parsed = dotenv.parse(contents) |
| 55 | + |
| 56 | + result = dotenvExpand(result) |
| 57 | + |
| 58 | + if (result.parsed) { |
| 59 | + console.log(`> ${chalk.cyan.bold('Info:')} Loaded env from ${envFile}`) |
| 60 | + } |
| 61 | + |
| 62 | + Object.assign(combinedEnv, result.parsed) |
| 63 | + } catch (err) { |
| 64 | + if (err.code !== 'ENOENT') { |
| 65 | + console.log( |
| 66 | + `> ${chalk.cyan.bold('Error: ')} Failed to load env from ${envFile}`, |
| 67 | + err |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // load global env values prefixed with `NEXT_APP_` to process.env |
| 74 | + for (const key of Object.keys(combinedEnv)) { |
| 75 | + if ( |
| 76 | + key.startsWith('NEXT_APP_') && |
| 77 | + typeof process.env[key] === 'undefined' |
| 78 | + ) { |
| 79 | + process.env[key] = combinedEnv[key] |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return combinedEnv |
| 84 | +} |
0 commit comments