-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
Build error when using json objects in define
#10848
Labels
p3-minor-bug
An edge case that only affects very specific usage (priority)
Comments
Vite docs says config // vite.config.js
export default {
define: {
'__CONST_OBJ__.SOME_FLAG': false,
}
} |
According to the docs, the replacement expression can be JSON object. I don't think it's an invalid use case. |
bluwy
added
p3-minor-bug
An edge case that only affects very specific usage (priority)
and removed
pending triage
labels
Apr 1, 2023
7 tasks
This is fixed by #11151 in 5.0.0-beta.13. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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: