aklinker1 / vite-plugin-web-extension

Vite plugin for developing Chrome/Web Extensions
https://vite-plugin-web-extension.aklinker1.io/
MIT License
574 stars 48 forks source link

Environement variables .env #29

Closed flo-pereira closed 2 years ago

flo-pereira commented 2 years ago

Hi,

Is there a way to get env variables from .env in content_script ?

.env

VITE_CUSTOM = test

vite.config.ts

export default defineConfig((command, mode) => {
  console.log(command, mode);
  // { mode: 'development', command: 'serve' }

  console.log(loadEnv(mode, __dirname));
  // { VITE_CUSTOM: 'test' }
  ...

content_script.ts

   console.log(import.meta.env)
//   {"BASE_URL":"/","MODE":"production","DEV":false,"PROD":true}

Thanks

aklinker1 commented 2 years ago

I believe .env files are loaded from the root that is specified in the vite.config.ts file. In all the examples, I set that to src/. Check if your config specifies a root, and try putting the .env files there.

Oh sorry, I misread your issue. It's a problem with content scripts, not with loading .env files. I'm busy right now, I'll get back to you in a bit

flo-pereira commented 2 years ago

You were right, that was just a problem with my root dir, sorry for that ! :pray:

flo-pereira commented 2 years ago

however, there is an issue that the .env file have to be in the root dir, seems that it doesn't take envDir path.

flo-pereira commented 2 years ago

got it

libModeViteConfig: {
  envDir: __dirname,
}