electron-vite
electron-vite copied to clipboard
Defaults for define in main are causing esbuild error due to process.env not being a valid value
Describe the bug
I'm getting error
> error: Invalid define value (must be valid JSON syntax or a single identifier): process.env
> error: Invalid define value (must be valid JSON syntax or a single identifier): global.process.env
> error: Invalid define value (must be valid JSON syntax or a single identifier): globalThis.process.env
which can be bypassed by adding define
, that replaces those keys with something else like single identifier or {}
. However for main process I do actually want to use process.env
, which appears to be not possible to do now (I suspect newest esbuild introduced some stricter checks that the value is single identifier or valid JSON, but process.env
is neither, see https://github.com/evanw/esbuild/issues/2292).
I think for main
section we shouldn't actually use the define at all, since we are replacing process.env
with process.env
, which doesn't make a lot of sense and those references could be left as they are in the emitted code.
Electron-Vite Version
1.0.3
Electron Version
18
Vite Version
v3.0.0-alpha.8
Validations
- [X] Follow the Code of Conduct.
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
@kskalski
I can't reproduce this bug (esbuild
ver: 0.14.42
). Can you provide more detailed info on this?
In Vite, the string replacement is done by itself, the define
config should not be used in esbuild
. see vite define plugin
Ok, seems like this error happens only when I remove
external: ['@electron-toolkit/utils']
line in the config. I was building without this external declaration, because I wasn't sure for its need, but possibly it shouldn't be ever removed.
Well, adding toolkit/utils to my larger project didn't fix the issue, but it does make a difference in electron-vite-boilerplate
project. I created reproduction in https://github.com/kskalski/electron-vite-boilerplate
Note that it uses the alpha version of vite (which currently is conflictinhg with electron-vite
's deps, thus the override flag in npm install
).
I was able to workaround the problem in my project by commenting out contents of https://github.com/alex8088/electron-vite/blob/c71d5aa6610002667bd04d5fd8a7e67f2f6af1fd/src/plugin.ts#L33 so the real question is if those defines are some kind of obsolete feature, do we still need it in the plugin.
@kskalski Thanks for taking the time to reproduce the issue. I will try it too. Whether this feature is still needed, we should really think about it :thinking: .
use vite@3 now