Skip to content

Incorrect define replacement in build #8663

Description

@bluwy

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions