mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 17:07:22 +00:00
parent
c8f8691496
commit
c5979d9fb5
@ -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)
|
||||
|
@ -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<string, any>
|
||||
envConfig?: {
|
||||
dotenv?: string | false
|
||||
env?: Record<string, string | undefined>
|
||||
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
|
||||
}
|
||||
|
@ -62,7 +62,8 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
||||
rootDir: opts.rootDir,
|
||||
for: opts.dev ? 'dev' : 'build',
|
||||
configOverrides: opts.config,
|
||||
ready: opts.ready
|
||||
ready: opts.ready,
|
||||
envConfig: opts.envConfig
|
||||
})
|
||||
return nuxt as Nuxt
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user