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

fix(define): should not stringify vite internal env #12120

Merged
merged 4 commits into from
Feb 21, 2023

Conversation

sun0day
Copy link
Member

@sun0day sun0day commented Feb 20, 2023

Description

Some Vite internal env values(e.g plugin-legacy __VITE_IS_LEGACY__) will be stringified, which may cause the value type to be incorrect in import.meta.env object

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@bluwy
Copy link
Member

bluwy commented Feb 21, 2023

Ah I didn't anticipate this bug, if you have define import.meta.env.SOMETHING: "rawIdentifier". import.meta.env should be:

{
  SOMETHING: rawIdentifer
}

instead of a string. I think we need to do this generically for all define that defines a raw identifier, not just for plugin-legacy.

@bluwy bluwy added the p4-important Violate documented behavior or significantly improves performance (priority) label Feb 21, 2023
Comment on lines 66 to 68
for (const key in env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split off env and userDefineEnv as otherwise the define envs get mixed into this for loop, which is harmless in practice, but would be nice to avoid.

packages/vite/src/node/plugins/define.ts Show resolved Hide resolved
// set here to allow override with config.define
importMetaKeys['import.meta.hot'] = `undefined`
for (const key in env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
}
Object.assign(importMetaFallbackKeys, {
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify(env),
'import.meta.env': JSON.stringify({ ...env, ...userDefineEnv })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice that import.meta.env.SSR will be replaced by user define values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that might be fine for now as in dev (import analysis), it's allowed to overwrite import.meta.env.SSR too 🤔

if (hasEnv) {
// inject import.meta.env
let env = `import.meta.env = ${JSON.stringify({
...config.env,
SSR: !!ssr,
})};`
// account for user env defines
for (const key in config.define) {
if (key.startsWith(`import.meta.env.`)) {
const val = config.define[key]
env += `${key} = ${
typeof val === 'string' ? val : JSON.stringify(val)
};`
}
}
str().prepend(env)
}

this code injects the import.meta.env code for each file. Also I just realized we could probably cache this code.

@sun0day
Copy link
Member Author

sun0day commented Feb 21, 2023

LGTM

@bluwy
Copy link
Member

bluwy commented Feb 21, 2023

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Feb 21, 2023

📝 Ran ecosystem CI: Open

suite result
astro ✅ success
histoire ✅ success
iles ✅ success
ladle ✅ success
laravel ✅ success
marko ✅ success
nuxt-framework ✅ success
previewjs ✅ success
qwik ⏹️ cancelled
rakkas ✅ success
sveltekit ✅ success
vite-plugin-ssr ✅ success
vite-plugin-react ✅ success
vite-plugin-react-pages ❌ failure
vite-plugin-react-swc ✅ success
vite-plugin-svelte ✅ success
vite-plugin-vue ✅ success
vite-setup-catalogue ✅ success
vitepress ✅ success
vitest ✅ success
windicss ✅ success

@patak-dev patak-dev merged commit 73c3999 into vitejs:main Feb 21, 2023
futurGH pushed a commit to futurGH/vite that referenced this pull request Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants