mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 07:32:01 +00:00
parent
c8f8691496
commit
c5979d9fb5
@ -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)
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user