Skip to content
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

Closed
7 tasks done
lsdsjy opened this issue Nov 9, 2022 · 3 comments
Closed
7 tasks done

Build error when using json objects in define #10848

lsdsjy opened this issue Nov 9, 2022 · 3 comments
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@lsdsjy
Copy link
Contributor

lsdsjy commented Nov 9, 2022

Describe the bug

I wrote a logical expression to call a function when some pre-defined constant is truthy:

// main.js
__CONST_OBJ__.SOME_FLAG && doSomething()
// vite.config.js
export default {
  define: {
    __CONST_OBJ__: {
      SOME_FLAG: false,  // the value may be derived from environment variables
    },
  },
};

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 and yarn build

System Info

System:
    StackBlitz
  npmPackages:
    vite: ^3.2.3 => 3.2.3

Used Package Manager

npm

Logs

No response

Validations

@luo3house
Copy link
Contributor

Vite docs says config define is only a replacement, so you should define the whole expression to replace instead.

// vite.config.js
export default {
  define: {
    '__CONST_OBJ__.SOME_FLAG': false,
  }
}

@lsdsjy
Copy link
Contributor Author

lsdsjy commented Nov 15, 2022

Vite docs says config define is only a replacement, so you should define the whole expression to replace instead.

According to the docs, the replacement expression can be JSON object. I don't think it's an invalid use case.

@bluwy bluwy added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Apr 1, 2023
@sapphi-red
Copy link
Member

This is fixed by #11151 in 5.0.0-beta.13.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

4 participants