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

Handling dev and build config.define inconsistency #12866

Closed
1 of 4 tasks
Eazymov opened this issue Apr 14, 2023 · 5 comments · Fixed by #13003
Closed
1 of 4 tasks

Handling dev and build config.define inconsistency #12866

Eazymov opened this issue Apr 14, 2023 · 5 comments · Fixed by #13003
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@Eazymov
Copy link

Eazymov commented Apr 14, 2023

Documentation is

  • Missing
  • Outdated
  • Confusing
  • Not sure?

Explain in Detail

Since dev and build implement define differently when i use this config

import { defineConfig } from 'vite'

export default defineConfig({
  define: {
    'import.meta.env.STRING': '123',
    'import.meta.env.STRINGIFIED_STRING': JSON.stringify('123'),
  },
})

I get STRING: 123, STRINGIFIED_STRING: "123" during development and STRING: "123", STRINGIFIED_STRING: "\"123\"" during production.
It is noted in the docs but how is it supposed to work with that? What should i do if both ways of defining a variable give me different results? Check for mode and pass different values to define? Or am i just misunderstanding something?
Seems like vite should provide consistency by itself

Your Suggestion for Changes

Make consistent behavior between dev and build or add examples to the docs on how to deal with inconsistency

Reproduction

dev https://stackblitz.com/edit/vitejs-vite-ibspf5?file=src%2Fmain.js&terminal=dev
build preview https://stackblitz.com/edit/vitejs-vite-ibspf5?file=src%2Fmain.js&terminal=preview

Steps to reproduce

No response

@Eazymov Eazymov added the documentation Improvements or additions to documentation label Apr 14, 2023
@github-actions
Copy link

Hello @Eazymov. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@stackblitz
Copy link

stackblitz bot commented Apr 14, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) and removed documentation Improvements or additions to documentation needs reproduction labels Apr 17, 2023
@sapphi-red
Copy link
Member

sapphi-red commented Apr 17, 2023

This bug seems to happen from 4.1.2 (build). (In dev, it happens from pre-2.2)
Adding import.meta.env.* to define should not affect the value of import.meta.env.

@Eazymov
Copy link
Author

Eazymov commented Apr 17, 2023

Adding import.meta.env.* to define should not affect the value of import.meta.env.

image
https://vitejs.dev/config/shared-options.html#envprefix

Without this feature there will be no way to use env without prefix?

@sapphi-red
Copy link
Member

If you add import.meta.env.ENV_VARIABLE to define, import.meta.env.ENV_VARIABLE will be replaced with that value. But import.meta.env won't include ENV_VARIABLE property.

If you want import.meta.env to include ENV_VARIABLE property, you have to add import.meta.env to define.

@github-actions github-actions bot locked and limited conversation to collaborators May 15, 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

Successfully merging a pull request may close this issue.

2 participants