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

Incorrect define replacement in build #8663

Closed
7 tasks done
bluwy opened this issue Jun 19, 2022 · 1 comment · Fixed by #10958 or #11151
Closed
7 tasks done

Incorrect define replacement in build #8663

bluwy opened this issue Jun 19, 2022 · 1 comment · Fixed by #10958 or #11151

Comments

@bluwy
Copy link
Member

bluwy commented Jun 19, 2022

Describe the bug

When building a project that uses import.meta.env.UNKNOWN, it is being replaced as MODE:"production",DEV:!1,PROD:!0}.UNKNOWN instead of ({}).UNKNOWN.

Ref #8090 (Additional context)

Bug introduced in #5972 in Vite 3. Line 50 is never being used, and skips to line 51 instead.

const env: Record<string, any> = {
...config.env,
SSR: !!config.build.ssr
}
for (const key in env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
}
Object.assign(importMetaKeys, {
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify(config.env),
'import.meta.hot': `false`
})

This not only happens for import.meta.env., but process.env. to for ssr builds. And other similar usage.

Reproduction

Vite 3: https://stackblitz.com/edit/vitejs-vite-b1md2x?file=main.js&terminal=build,preview
Vite 2.9: https://stackblitz.com/edit/vitejs-vite-ouwh65?file=main.js&terminal=build,preview

System Info

Vite 3

Used Package Manager

pnpm

Logs

No response

Validations

@julienv3
Copy link
Contributor

julienv3 commented Nov 16, 2022

@tony19 it does not seem like #9791 fixes the issue.
The problem lies in the negative lookahead in the regex used for replacements, it will never match "import.meta.env." (or any other .-ending replacement) because it rejects the match when it's followed by a letter or a number.
Sent a PR addressing this issue specifically in #10958 with tests.

@bluwy bluwy removed their assignment Nov 17, 2022
patak-dev pushed a commit that referenced this issue Nov 28, 2022
bluwy pushed a commit that referenced this issue Dec 5, 2022
patak-dev pushed a commit that referenced this issue Dec 5, 2022
* fix: glob import parsing (#10949) (#11056)

closes #10949
closes #11051

* fix: import.meta.env and process.env undefined variable replacement (fix #8663) (#10958)

Co-authored-by: bluwy <bjornlu.dev@gmail.com>
fix #8663

* fix(esbuild): handle inline sourcemap option (#11120)

* fix(importGlob): preserve line count for sourcemap (#11122)

* fix: Dev SSR dep optimization + respect optimizeDeps.include (#11123)

* fix: reset global regex before match (#11132)

* chore: fix test

Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com>
Co-authored-by: julienv3 <julienv3@gmail.com>
Co-authored-by: 翠 / green <green@sapphi.red>
@github-actions github-actions bot locked and limited conversation to collaborators Dec 13, 2022
futurGH pushed a commit to futurGH/vite that referenced this issue Feb 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants