From 472ba5d7198e5db631ddafc1fd2adf78ce26003e Mon Sep 17 00:00:00 2001 From: GavinRay97 Date: Fri, 28 May 2021 13:42:22 -0400 Subject: [PATCH] feat(config): add `envDir` option (#3407) Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com> --- docs/config/index.md | 9 +++++++++ docs/guide/env-and-mode.md | 2 +- packages/vite/src/node/config.ts | 15 ++++++++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 6e188c7e61bdab..ba391fe2de1042 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -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 diff --git a/docs/guide/env-and-mode.md b/docs/guide/env-and-mode.md index d58063554cb973..4d92ee03b36a6d 100644 --- a/docs/guide/env-and-mode.md +++ b/docs/guide/env-and-mode.md @@ -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 diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 849b7372e50132..5ef64be1f33454 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -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 @@ -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 @@ -893,7 +902,7 @@ async function loadConfigFromBundledFile( export function loadEnv( mode: string, - root: string, + envDir: string, prefix = 'VITE_' ): Record { if (mode === 'local') { @@ -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