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: support loading customize environment variables from a .env file #223

Merged
merged 6 commits into from
May 22, 2020

Conversation

daychongyang
Copy link
Contributor

No description provided.

@daychongyang
Copy link
Contributor Author

#213

playground/App.vue Outdated Show resolved Hide resolved
src/node/build/index.ts Outdated Show resolved Hide resolved
src/node/build/index.ts Outdated Show resolved Hide resolved
@yyx990803 yyx990803 merged commit 89fe0a9 into vitejs:master May 22, 2020
@yyx990803
Copy link
Member

Good job!

@yyx990803
Copy link
Member

FYI: inlining the entire env object like this leads to bloated bundle size and also cause the minifier to not be able to drop if (process.env.NODE_ENV !== 'production') blocks, so I am changing it instead replace each key individually.

This will also remove support for usage like process.env['NODE_ENV'] or Object.keys(process.env).

@yyx990803
Copy link
Member

See 1be6121

@quantuminformation
Copy link

Anyone got a walkthrough doc of how to use this? I am trying to use console.log(import.meta.env) but I only see
image

vs what I have in .env

I am using svite:

https://github.com/quantuminformation/mentorcv/blob/master/src/index.js#L2

@damianstasik
Copy link
Contributor

@quantuminformation try prefixing your environment variables with VITE_.

@quantuminformation
Copy link

wow that was quick, yes that works thx

@stefnotch
Copy link
Contributor

https://github.com/vitejs/vite#modes-and-environment-variables

@daychongyang
Copy link
Contributor Author

daychongyang commented Jul 8, 2020

Only variables prefixed with VITE_ are exposed to your code. e.g. VITE_SOME_KEY=123 will be exposed as import.meta.env.VITE_SOME_KEY, but SOME_KEY=123 will not. This is because the .env files may be used by some users for server-side or build scripts and may contain sensitive information that should not be exposed in code shipped to browsers.

// src/node/config.ts     line:455

function loadEnv(mode: string, root: string): Record<string, string> {
  if (mode === 'local') {
    throw new Error(
      `"local" cannot be used as a mode name because it conflicts with ` +
        `the .local postfix for .env files.`
    )
  }

  debug(`env mode: ${mode}`)
  const envFiles = [
    /** mode local file */ `.env.${mode}.local`,
    /** mode file */ `.env.${mode}`,
    /** local file */ `.env.local`,
    /** default file */ `.env`
  ]

  const env: Record<string, string> = {}
  for (const file of envFiles) {
    const path = lookupFile(root, [file], true)
    if (path) {
      const result = dotenv.config({
        debug: !!process.env.DEBUG || undefined,
        path
      })
      if (result.error) {
        throw result.error
      }
      dotenvExpand(result)
      for (const key in result.parsed) {
        // only keys that start with VITE_ are exposed.
        if (key.startsWith(`VITE_`)) {
          env[key] = result.parsed![key]
        }
      }
    }
  }

@quantuminformation
Copy link

Nice work guys, i'll promote this build tool on my YouTube channel later today.

@quantuminformation
Copy link

btw if any member/contributor wants to come on my podcast to talk about vite, please ping me.

@quantuminformation
Copy link

For some reason, I only get one environment var coming through

image
https://github.com/quantuminformation/mentorcv/blob/master/src/firebaseBackend.js#L12

@daychongyang
Copy link
Contributor Author

For some reason, I only get one environment var coming through

image
https://github.com/quantuminformation/mentorcv/blob/master/src/firebaseBackend.js#L12

There seems to be no problem...
image

@quantuminformation
Copy link

Reinstalled nod mods, went away! thx

@quantuminformation
Copy link

quantuminformation commented Jul 10, 2020

I tried passing in from command line

➜ mentorcv git:(master) ✗ VITE_EMULATION="true" ndv

however console.log(import.meta.env.VITE_EMULATION) prints undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants