Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add envDir option #3407

Merged
merged 5 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -912,7 +921,7 @@ async function loadConfigFromBundledFile(

export function loadEnv(
mode: string,
root: string,
envDir: string,
prefix = 'VITE_'
): Record<string, string> {
if (mode === 'local') {
Expand All @@ -939,7 +948,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