-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
load.js
189 lines (158 loc) · 4.77 KB
/
load.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import path from 'path'
import fs from 'fs'
import defu from 'defu'
import consola from 'consola'
import dotenv from 'dotenv'
import { clearRequireCache, createRequire, scanRequireTree } from '@nuxt/utils'
import destr from 'destr'
import * as rc from 'rc9'
import { defaultNuxtConfigFile } from './config'
export async function loadNuxtConfig ({
rootDir = '.',
envConfig = {},
configFile = defaultNuxtConfigFile,
configContext = {},
configOverrides = {}
} = {}) {
rootDir = path.resolve(rootDir)
const _require = createRequire(rootDir, true)
let options = {}
try {
configFile = _require.resolve(path.resolve(rootDir, configFile))
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw (e)
} else if (configFile !== defaultNuxtConfigFile) {
consola.fatal('Config file not found: ' + configFile)
}
// Skip configFile if cannot resolve
configFile = undefined
}
// Load env
envConfig = {
dotenv: '.env',
env: process.env,
expand: true,
...envConfig
}
const env = loadEnv(envConfig, rootDir)
// Fill process.env so it is accessible in nuxt.config
for (const key in env) {
if (!key.startsWith('_') && envConfig.env[key] === undefined) {
envConfig.env[key] = env[key]
}
}
if (configFile) {
// Clear cache
clearRequireCache(configFile)
options = _require(configFile) || {}
if (options.default) {
options = options.default
}
if (typeof options === 'function') {
try {
options = await options(configContext)
if (options.default) {
options = options.default
}
} catch (error) {
consola.error(error)
consola.fatal('Error while fetching async configuration')
}
}
// Don't mutate options export
options = { ...options }
// Keep _nuxtConfigFile for watching
options._nuxtConfigFile = configFile
// Keep all related files for watching
options._nuxtConfigFiles = Array.from(scanRequireTree(configFile))
if (!options._nuxtConfigFiles.includes(configFile)) {
options._nuxtConfigFiles.unshift(configFile)
}
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}
// Load Combine configs
// Priority: configOverrides > nuxtConfig > .nuxtrc > .nuxtrc (global)
options = defu(
configOverrides,
options,
rc.read({ name: '.nuxtrc', dir: options.rootDir }),
rc.readUser('.nuxtrc')
)
// Load env to options._env
options._env = env
options._envConfig = envConfig
if (configContext) { configContext.env = env }
// Expand and interpolate runtimeConfig from _env
if (envConfig.expand) {
for (const c of ['publicRuntimeConfig', 'privateRuntimeConfig']) {
if (options[c]) {
if (typeof options[c] === 'function') {
options[c] = options[c](env)
}
expand(options[c], env, destr)
}
}
}
return options
}
function loadEnv (envConfig, rootDir = process.cwd()) {
const env = Object.create(null)
// Read dotenv
if (envConfig.dotenv) {
envConfig.dotenv = path.resolve(rootDir, envConfig.dotenv)
if (fs.existsSync(envConfig.dotenv)) {
const parsed = dotenv.parse(fs.readFileSync(envConfig.dotenv, 'utf-8'))
Object.assign(env, parsed)
}
}
// Apply process.env
if (!envConfig.env._applied) {
Object.assign(env, envConfig.env)
envConfig.env._applied = true
}
// Interpolate env
if (envConfig.expand) {
expand(env)
}
return env
}
// Based on https://github.com/motdotla/dotenv-expand
function expand (target, source = {}, parse = v => v) {
function getValue (key) {
// Source value 'wins' over target value
return source[key] !== undefined ? source[key] : target[key]
}
function interpolate (value, parents = []) {
if (typeof value !== 'string') {
return value
}
const matches = value.match(/(.?\${?(?:[a-zA-Z0-9_:]+)?}?)/g) || []
return parse(matches.reduce((newValue, match) => {
const parts = /(.?)\${?([a-zA-Z0-9_:]+)?}?/g.exec(match)
const prefix = parts[1]
let value, replacePart
if (prefix === '\\') {
replacePart = parts[0]
value = replacePart.replace('\\$', '$')
} else {
const key = parts[2]
replacePart = parts[0].substring(prefix.length)
// Avoid recursion
if (parents.includes(key)) {
consola.warn(`Please avoid recursive environment variables ( loop: ${parents.join(' > ')} > ${key} )`)
return ''
}
value = getValue(key)
// Resolve recursive interpolations
value = interpolate(value, [...parents, key])
}
return value !== undefined ? newValue.replace(replacePart, value) : newValue
}, value))
}
for (const key in target) {
target[key] = interpolate(getValue(key))
}
}