Build error when using json objects in define
#10848
Labels
p3-minor-bug
An edge case that only affects very specific usage (priority)
define
#10848
Describe the bug
I wrote a logical expression to call a function when some pre-defined constant is truthy:
And running
vite build
throws error:error during build: Error: Unexpected token
.I think it's because when JS parsers encounter a
{
at the beginning of a line, it will be treated as the beginning of new block, not an object. See this repl. So the temporary workaround is to make{
not the first character of a line, e.g. wrap it inside parenthses(Boolean(__CONST_OBJ__.SOME_FLAG)
).If I understand correctly, a possible fix is to wrap the object expression in parentheses before replacing. Don't know if there are any other pitfalls.
Reproduction
https://stackblitz.com/edit/vitejs-vite-76jfpr?file=main.js,vite.config.js&terminal=dev
Steps to reproduce
Run
npm install
andyarn build
System Info
System: StackBlitz npmPackages: vite: ^3.2.3 => 3.2.3
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: