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

[Help] How to use env var like import.meta.env.VITE_KEY in main/preload ? #209

Closed
dongivan opened this issue Jul 24, 2022 · 4 comments
Closed
Labels
help wanted Extra attention is needed

Comments

@dongivan
Copy link

dongivan commented Jul 24, 2022

Describe the problem you confuse

Vite supports env vars prefixed with "VITE_" if they are placed in the file .env.
However in electron-vite-vue template, env vars like import.meta.env.VITE_KEY can only be used in renderer process.

How to reproduce:

  1. npm create electron-vite & npm i
  2. Add .env file at app root with the content:
VITE_TEST="test string"
  1. Modify electron/electron-env.d.ts:
/// <reference types="vite-plugin-electron/electron-env" />

interface ImportMetaEnv {
  readonly VITE_TEST: string
}

interface ImportMeta {
  readonly env: ImportMetaEnv
}
  1. Add this line into electron/main/index.ts:
console.log("in main process", import.meta.env.VITE_TEST)
  1. Add this line into electron/preload/index.ts:
console.log("in preload", import.meta.env.VITE_TEST)
  1. Add this line into src/main.ts:
console.log("in renderer process", import.meta.env.VITE_TEST)
  1. npm run dev
  2. Result:

main

renderer

@dongivan dongivan added the help wanted Extra attention is needed label Jul 24, 2022
@dongivan
Copy link
Author

BTW, currently I use dotenv to load env vars of .env file into process.env.

@caoxiemeihao
Copy link
Member

caoxiemeihao commented Jul 24, 2022

It's probable a conservative desing, not sure if it's right.
However, usually in Node.js environments, process.env is always avaizble.

  const conf: InlineConfig = {
    // 🚧 Avoid recursive build caused by load config file
    configFile: false,
    envFile: false, // 👈👈👈👈👈👈👈
    publicDir: false,

    build: {
      emptyOutDir: false,
      minify: process.env./* from mode option */NODE_ENV === 'production',
    },
  }

https://github.com/electron-vite/vite-plugin-electron/blob/2df429e643192a0a9a0702b1c3b36544460950b0/packages/electron/src/config.ts#L32


Perhaps, this feature is avaiable in v0.8.2, what do you think about it. My bro 😄 @PAXANDDOS

@dongivan
Copy link
Author

Thank you for replying!

@PAXANDDOS
Copy link
Member

PAXANDDOS commented Jul 24, 2022

Perhaps, this feature is available in v0.8.2, what do you think about it. My bro 😄 @PAXANDDOS

Honestly, I thought the env file was exposed everywhere 😅

I was thinking that we don't need .env files here at all, we're building desktop applications with Electron anyways. But then I remembered some cases where you need to have, for example, a hash string for modules, and since all application logic is stored in the main process, exposing environment variables to it would be a great idea

@caoxiemeihao So I want to say that yes, it's better to enable the envFile option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants