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

Issue with Vite v5 #15

Open
SebastienGllmt opened this issue Dec 8, 2023 · 3 comments
Open

Issue with Vite v5 #15

SebastienGllmt opened this issue Dec 8, 2023 · 3 comments

Comments

@SebastienGllmt
Copy link

SebastienGllmt commented Dec 8, 2023

I found that this plugin no longer works in Vite v5. Notably, according to their docs about how they changed define, esbuild is now used to manage environment variables during build this.

Notably, this seams to mean

export default defineConfig({
  plugins: [EnvironmentPlugin('all')],

No longer works, and instead I had to add to esbuildOptions instead

optimizeDeps: {
    esbuildOptions: {
      define: {
        // add envs here (can't use vite-plugin-environment for this)
      },
   }
}
@ElMassimo
Copy link
Owner

ElMassimo commented Dec 8, 2023

define is still supported in Vite 5.

I've updated the test setup to run with Vite 5, and tests continue to pass.

Please provide a minimal example that used to work in Vite 4 and no longer works in Vite 5.

@ElMassimo
Copy link
Owner

ElMassimo commented Dec 8, 2023

Sorry, accidentally hit close with comment.

@ElMassimo ElMassimo reopened this Dec 8, 2023
@SebastienGllmt
Copy link
Author

SebastienGllmt commented Jan 22, 2024

I think I figured out what is happening with this. Vite handles dependencies and code you wrote separately. Notably, Vite uses dependency pre-bundling to bundle any package you have separately from the code in your own package.

vite-plugin-environment works as expected for any code in the package you author yourself. However, it does not set process.env during the pre-bundling stage. Conversely, the solution I proposed in the original post of this issue has the opposite behavior - it only adds the process.env variables during the pre-bundling stage (hence the optimizeDeps key)

Therefore, to have your process.env properly setup you need both this package (for your own code) and setting the esbuildOptions (for dependencies)

Of note, this only matters for development builds because dep-prebundling is only done using esbuild for dev builds (@rollup/plugin-commonjs is used instead for production builds)

Here is code I wrote for the esbuild define in case it's useful to anyone

// 1) Setup process.env so it's visible from dependencies during the Vite pre-build step
import { config } from 'dotenv';
config({ path: `${process.cwd()}/../../.env` });

const envVarsToInclude = [
    // put the ENV vars you want to expose here
    'FOO'
];
// you'll need to update these map entries for any default or optional values
const esbuildEnvs = Object.fromEntries(envVarsToInclude .map(key => [`process.env.${key}`, JSON.stringify(process.env[key])]));

// 2) Setup process.env so it's visible from files in your own project
// you'll need to update these map entries for any default or optional values
const viteEnvMap: EnvVarDefaults = Object.fromEntries(
  envVarsToInclude.map(entry => [entry, undefined])
);

// https://vitejs.dev/config/
export default defineConfig({
  // in combination with the EnvironmentPlugin makes process.env available and loads everything from .env
  // additionally loads .env.foo (or other name) based on the --mode argument to vite
  envDir: '../..',
  plugins: [EnvironmentPlugin(viteEnvMap)],
  optimizeDeps: {
    esbuildOptions: {
      define: {
        ...esbuildEnvs,
      },
  },
});

It could be good for the docs for this project to mention this, or to add functionality to handle both these cases for you

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

No branches or pull requests

2 participants