Skip to content

Commit

Permalink
fix: use upward search for env files
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 25, 2020
1 parent 5589fa3 commit 4fceaea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 10 additions & 9 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Transform } from './transform'
import { DepOptimizationOptions } from './depOptimizer'
import { IKoaProxiesOptions } from 'koa-proxies'
import { ServerOptions } from 'https'
import { lookupFile } from './utils'

export { Resolver, Transform }

Expand Down Expand Up @@ -345,7 +346,7 @@ export async function resolveConfig(
}

// load environment variables
const env = loadEnv(mode, cwd)
const env = loadEnv(mode, config.root || cwd)
debug(`env: %O`, env)
config.env = env

Expand Down Expand Up @@ -414,22 +415,22 @@ function resolvePlugin(config: UserConfig, plugin: Plugin): UserConfig {
}
}

function loadEnv(mode: string, cwd: string): Record<string, string> {
function loadEnv(mode: string, root: string): Record<string, string> {
debug(`env mode: ${mode}`)
const { resolve } = path
const envFiles = [
/** default file */ resolve(cwd, '.env'),
/** local file */ resolve(cwd, `.env.local`),
/** mode file */ resolve(cwd, `.env.${mode}`),
/** mode local file */ resolve(cwd, `.env.${mode}.local`)
/** default file */ `.env`,
/** local file */ `.env.local`,
/** mode file */ `.env.${mode}`,
/** mode local file */ `.env.${mode}.local`
]

const env: Record<string, string> = {}
for (const file of envFiles) {
if (fs.existsSync(file) && fs.statSync(file).isFile()) {
const path = lookupFile(root, [file], true)
if (path) {
const result = dotenv.config({
debug: !!process.env.DEBUG,
path: file
path
})
if (result.error) {
throw result.error
Expand Down
2 changes: 1 addition & 1 deletion src/node/utils/fsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function lookupFile(
): string | undefined {
for (const format of formats) {
const fullPath = path.join(dir, format)
if (fs.existsSync(fullPath)) {
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) {
return pathOnly ? fullPath : fs.readFileSync(fullPath, 'utf-8')
}
}
Expand Down

0 comments on commit 4fceaea

Please sign in to comment.