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

feat(config): allow user custom .env files directory #2123

wants to merge 3 commits into from

Conversation

CHOYSEN
Copy link
Contributor

@CHOYSEN CHOYSEN commented Feb 20, 2021

Close #2113

@Shinigami92 Shinigami92 added p2-nice-to-have Not breaking anything but nice to have (priority) 🔍 review needed labels Mar 23, 2021
@@ -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))

- **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.

@GavinRay97
Copy link
Contributor

+1, very useful when you have a multi-project workspace and want to keep a top-level .env file with config values for all of the projects.

Would be great to have this merged, thank you =)

@aleclarson
Copy link
Member

@GavinRay97 Agreed. Do you want to fork this PR and add the suggested changes so we can merge?

@GavinRay97
Copy link
Contributor

@GavinRay97 Agreed. Do you want to fork this PR and add the suggested changes so we can merge?

Sure np, can do it today after work 👍

@GavinRay97
Copy link
Contributor

Btw, this is how I am working around this now.
This works, but it's ugly:

// vite.config.ts
import reactRefresh from "@vitejs/plugin-react-refresh"
import * as dotenv from "dotenv"
import { defineConfig } from "vite"

const result = dotenv.config({
  debug: true,
  path: require("path").join(__dirname, "../.env"),
})

if (result.error) throw result.error

const defines = {}
for (let key in result.parsed) {
  if (!key.startsWith("VITE_")) continue
  defines["import.meta.env." + key] = JSON.stringify(result.parsed[key])
}

export default defineConfig({
  plugins: [reactRefresh()],
  define: defines,
})

@GavinRay97
Copy link
Contributor

GavinRay97 commented May 13, 2021

#3407

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Configuration .env file read path not found in document
4 participants