Skip to content

Commit

Permalink
feat(config): add envDir option (#3407)
Browse files Browse the repository at this point in the history
Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
  • Loading branch information
GavinRay97 and aleclarson committed May 28, 2021
1 parent 5745a2e commit 472ba5d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
9 changes: 9 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ export default async ({ command, mode }) => {

Set to `false` to prevent Vite from clearing the terminal screen when logging certain messages. Via command line, use `--clearScreen false`.

### envDir

- **Type:** `string`
- **Default:** `root`

The directory from which `.env` files are loaded. Can be an absolute path, or a path relative to the project root.

See [here](/guide/env-and-mode#env-files) for more about environment files.

## Server Options

### server.host
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/env-and-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ It will also replace these strings appearing in JavaScript strings and Vue templ

## `.env` Files

Vite uses [dotenv](https://github.com/motdotla/dotenv) to load additional environment variables from the following files in your project root:
Vite uses [dotenv](https://github.com/motdotla/dotenv) to load additional environment variables from the following files in your [environment directory](/config/#envDir):

```
.env # loaded in all cases
Expand Down
15 changes: 12 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ export interface UserConfig {
* Default: true
*/
clearScreen?: boolean
/**
* Environment files directory. Can be an absolute path, or a path relative from
* the location of the config file itself.
* @default root
*/
envDir?: string
/**
* Import aliases
* @deprecated use `resolve.alias` instead
Expand Down Expand Up @@ -304,7 +310,10 @@ export async function resolveConfig(
}

// load .env files
const userEnv = inlineConfig.envFile !== false && loadEnv(mode, resolvedRoot)
const envDir = config.envDir
? normalizePath(path.resolve(resolvedRoot, config.envDir))
: resolvedRoot
const userEnv = inlineConfig.envFile !== false && loadEnv(mode, envDir)

// Note it is possible for user to have a custom mode, e.g. `staging` where
// production-like behavior is expected. This is indicated by NODE_ENV=production
Expand Down Expand Up @@ -893,7 +902,7 @@ async function loadConfigFromBundledFile(

export function loadEnv(
mode: string,
root: string,
envDir: string,
prefix = 'VITE_'
): Record<string, string> {
if (mode === 'local') {
Expand All @@ -920,7 +929,7 @@ export function loadEnv(
}

for (const file of envFiles) {
const path = lookupFile(root, [file], true)
const path = lookupFile(envDir, [file], true)
if (path) {
const parsed = dotenv.parse(fs.readFileSync(path), {
debug: !!process.env.DEBUG || undefined
Expand Down

0 comments on commit 472ba5d

Please sign in to comment.