diff --git a/packages/kit/src/config/env.ts b/packages/kit/src/config/env.ts index 0230433064..530d598f6f 100644 --- a/packages/kit/src/config/env.ts +++ b/packages/kit/src/config/env.ts @@ -1,6 +1,7 @@ import { existsSync, promises as fsp } from 'fs' import { resolve } from 'upath' import dotenv from 'dotenv' +import { LoadNuxtConfigOptions } from './load' export interface LoadDotEnvOptions { /** 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 * to the current working directory). For example, `.env`. */ - dotenvFile: string + dotenvFile: string | false /** * 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). */ -export async function loadEnv (rootDir: string) { +export async function loadEnv (rootDir: string, options: LoadNuxtConfigOptions['envConfig'] = {}) { + const targetEnv = options.env ?? process.env + // Load env const env = await loadDotenv({ rootDir, - dotenvFile: '.env', - env: process.env, - expand: true + dotenvFile: options.dotenv ?? '.env', + env: targetEnv, + expand: options.expand ?? true }) // Fill process.env so it is accessible in nuxt.config for (const key in env) { - if (!key.startsWith('_') && process.env[key] === undefined) { - process.env[key] = env[key] + if (!key.startsWith('_') && targetEnv[key] === undefined) { + targetEnv[key] = env[key] } } } /** Load environment variables into an object. */ export async function loadDotenv (opts: LoadDotEnvOptions) { + if (!opts.dotenvFile) { + return + } + const env = Object.create(null) const dotenvFile = resolve(opts.rootDir, opts.dotenvFile) diff --git a/packages/kit/src/config/load.ts b/packages/kit/src/config/load.ts index fce5024ff6..bd0bb054a9 100644 --- a/packages/kit/src/config/load.ts +++ b/packages/kit/src/config/load.ts @@ -6,6 +6,7 @@ import * as rc from 'rc9' import { tryResolveModule, requireModule, scanRequireTree } from '../utils/cjs' import { NuxtOptions } from '../types/config' import nuxtConfigSchema from './schema' +import { loadEnv } from './env' export interface LoadNuxtConfigOptions { /** Your project root directory (either absolute or relative to the current working directory). */ @@ -14,6 +15,11 @@ export interface LoadNuxtConfigOptions { configFile?: string /** Any overrides to your Nuxt configuration. */ config?: Record + envConfig?: { + dotenv?: string | false + env?: Record + expand?: boolean + } } export function loadNuxtConfig (opts: LoadNuxtConfigOptions): NuxtOptions { @@ -44,6 +50,8 @@ export function loadNuxtConfig (opts: LoadNuxtConfigOptions): NuxtOptions { nuxtConfig.rootDir = rootDir } + loadEnv(rootDir, opts.envConfig) + // Resolve and apply defaults return applyDefaults(nuxtConfigSchema, nuxtConfig) as NuxtOptions } diff --git a/packages/kit/src/nuxt.ts b/packages/kit/src/nuxt.ts index ca4781b3e8..1079d11d47 100644 --- a/packages/kit/src/nuxt.ts +++ b/packages/kit/src/nuxt.ts @@ -62,7 +62,8 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { rootDir: opts.rootDir, for: opts.dev ? 'dev' : 'build', configOverrides: opts.config, - ready: opts.ready + ready: opts.ready, + envConfig: opts.envConfig }) return nuxt as Nuxt }