electron-vite icon indicating copy to clipboard operation
electron-vite copied to clipboard

Defaults for define in main are causing esbuild error due to process.env not being a valid value

Open kskalski opened this issue 2 years ago • 4 comments

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

kskalski avatar Jun 04 '22 09:06 kskalski

@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

alex8088 avatar Jun 05 '22 06:06 alex8088

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.

kskalski avatar Jun 06 '22 15:06 kskalski

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 avatar Jun 08 '22 16:06 kskalski

@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: .

alex8088 avatar Jun 09 '22 08:06 alex8088

use vite@3 now

alex8088 avatar Aug 25 '22 12:08 alex8088