feat: support auto load for dotenv (#580)

close #355
This commit is contained in:
Anthony Fu 2021-09-25 16:18:01 +08:00 committed by GitHub
parent c8f8691496
commit c5979d9fb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import { existsSync, promises as fsp } from 'fs' import { existsSync, promises as fsp } from 'fs'
import { resolve } from 'upath' import { resolve } from 'upath'
import dotenv from 'dotenv' import dotenv from 'dotenv'
import { LoadNuxtConfigOptions } from './load'
export interface LoadDotEnvOptions { export interface LoadDotEnvOptions {
/** The project root directory (either absolute or relative to the current working directory). */ /** The project root directory (either absolute or relative to the current working directory). */
@ -9,7 +10,7 @@ export interface LoadDotEnvOptions {
* What file to look in for environment variables (either absolute or relative * What file to look in for environment variables (either absolute or relative
* to the current working directory). For example, `.env`. * to the current working directory). For example, `.env`.
*/ */
dotenvFile: string dotenvFile: string | false
/** /**
* Whether to interpolate variables within .env. * Whether to interpolate variables within .env.
* *
@ -31,25 +32,31 @@ export interface LoadDotEnvOptions {
* *
* @param rootDir - The project root directory (either absolute or relative to the current working directory). * @param rootDir - The project root directory (either absolute or relative to the current working directory).
*/ */
export async function loadEnv (rootDir: string) { export async function loadEnv (rootDir: string, options: LoadNuxtConfigOptions['envConfig'] = {}) {
const targetEnv = options.env ?? process.env
// Load env // Load env
const env = await loadDotenv({ const env = await loadDotenv({
rootDir, rootDir,
dotenvFile: '.env', dotenvFile: options.dotenv ?? '.env',
env: process.env, env: targetEnv,
expand: true expand: options.expand ?? true
}) })
// Fill process.env so it is accessible in nuxt.config // Fill process.env so it is accessible in nuxt.config
for (const key in env) { for (const key in env) {
if (!key.startsWith('_') && process.env[key] === undefined) { if (!key.startsWith('_') && targetEnv[key] === undefined) {
process.env[key] = env[key] targetEnv[key] = env[key]
} }
} }
} }
/** Load environment variables into an object. */ /** Load environment variables into an object. */
export async function loadDotenv (opts: LoadDotEnvOptions) { export async function loadDotenv (opts: LoadDotEnvOptions) {
if (!opts.dotenvFile) {
return
}
const env = Object.create(null) const env = Object.create(null)
const dotenvFile = resolve(opts.rootDir, opts.dotenvFile) const dotenvFile = resolve(opts.rootDir, opts.dotenvFile)

View File

@ -6,6 +6,7 @@ import * as rc from 'rc9'
import { tryResolveModule, requireModule, scanRequireTree } from '../utils/cjs' import { tryResolveModule, requireModule, scanRequireTree } from '../utils/cjs'
import { NuxtOptions } from '../types/config' import { NuxtOptions } from '../types/config'
import nuxtConfigSchema from './schema' import nuxtConfigSchema from './schema'
import { loadEnv } from './env'
export interface LoadNuxtConfigOptions { export interface LoadNuxtConfigOptions {
/** Your project root directory (either absolute or relative to the current working directory). */ /** Your project root directory (either absolute or relative to the current working directory). */
@ -14,6 +15,11 @@ export interface LoadNuxtConfigOptions {
configFile?: string configFile?: string
/** Any overrides to your Nuxt configuration. */ /** Any overrides to your Nuxt configuration. */
config?: Record<string, any> config?: Record<string, any>
envConfig?: {
dotenv?: string | false
env?: Record<string, string | undefined>
expand?: boolean
}
} }
export function loadNuxtConfig (opts: LoadNuxtConfigOptions): NuxtOptions { export function loadNuxtConfig (opts: LoadNuxtConfigOptions): NuxtOptions {
@ -44,6 +50,8 @@ export function loadNuxtConfig (opts: LoadNuxtConfigOptions): NuxtOptions {
nuxtConfig.rootDir = rootDir nuxtConfig.rootDir = rootDir
} }
loadEnv(rootDir, opts.envConfig)
// Resolve and apply defaults // Resolve and apply defaults
return applyDefaults(nuxtConfigSchema, nuxtConfig) as NuxtOptions return applyDefaults(nuxtConfigSchema, nuxtConfig) as NuxtOptions
} }

View File

@ -62,7 +62,8 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
rootDir: opts.rootDir, rootDir: opts.rootDir,
for: opts.dev ? 'dev' : 'build', for: opts.dev ? 'dev' : 'build',
configOverrides: opts.config, configOverrides: opts.config,
ready: opts.ready ready: opts.ready,
envConfig: opts.envConfig
}) })
return nuxt as Nuxt return nuxt as Nuxt
} }