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): allow user custom .env files directory #2123

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 7 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ export default ({ 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`

Environment files directory. Can be an absolute path, or a path relative from the location of the config file itself.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably worth adding a link to the .env guide:

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
File renamed without changes.
11 changes: 11 additions & 0 deletions packages/playground/env/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const path = require('path')

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
envDir: path.resolve(__dirname, 'env'),
build: {
minify: false
}
}
15 changes: 12 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 @@ -268,7 +274,10 @@ export async function resolveConfig(
}

// load .env files
const userEnv = loadEnv(mode, resolvedRoot)
const envDir = config.envDir
? normalizePath(path.resolve(config.envDir))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
? normalizePath(path.resolve(config.envDir))
? normalizePath(path.resolve(resolvedRoot, config.envDir))

: resolvedRoot
const userEnv = 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 @@ -788,7 +797,7 @@ async function loadConfigFromBundledFile(
return config
}

export function loadEnv(mode: string, root: string, prefix = 'VITE_') {
export function loadEnv(mode: string, envDir: string, prefix = 'VITE_') {
if (mode === 'local') {
throw new Error(
`"local" cannot be used as a mode name because it conflicts with ` +
Expand All @@ -813,7 +822,7 @@ export function loadEnv(mode: string, root: string, prefix = 'VITE_') {
}

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