nx-plus icon indicating copy to clipboard operation
nx-plus copied to clipboard

Nx and Vite handling conflict of .env files

Open chf007 opened this issue 3 years ago • 4 comments

the ideal priority

/apps/[app]/.env.[mode].local /apps/[app]/.env.[mode] /apps/[app]/.env.local /apps/[app]/.env /.env.[mode].local /.env.[mode] /.env.local /.env

but if there is an environment variable in both above file, the one in [mode] will not take effect

the reason is that Nx will first process /apps/[app]/.env.local /apps/[app]/.env /.env.local /.env,and Nx does not support mode mode,[mode] files will not be processed

when the Vite executor is executed, Vite will process all .env files, but at this time the environment variable Vite pre-processed by Nx will not overwrite

chf007 avatar Apr 27 '22 08:04 chf007

https://github.com/chf007/vite-plugin-nx-dotenv

I wrote a plugin for Vite to temporarily solve this problem.

chf007 avatar Apr 28 '22 13:04 chf007

Cool fix @chf007! If you want, you can open a PR and add a snippet in the @nx-plus/vite README about this package

ZachJW34 avatar Apr 28 '22 19:04 ZachJW34

There are still some problems with the plugin: because the priority of the plugin cannot be higher than the Vite built-in define plugin, the ideal priority cannot be achieved in the build mode.

At present, it may only be solved by giving pr to Vite.

chf007 avatar May 06 '22 14:05 chf007

There are still some problems with the plugin: because the priority of the plugin cannot be higher than the Vite built-in define plugin, the ideal priority cannot be achieved in the build mode.

At present, it may only be solved by giving pr to Vite.

This problem has been solved in vite-plugin-nx-dotenv version 1.0.1, regardless of serve mode or build mode, we can correctly get the desired environment variable value.

chf007 avatar May 11 '22 10:05 chf007