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

import.meta.env is undefined when used in ternary operator following a spread #11803

Closed
7 tasks done
ycmjason opened this issue Jan 24, 2023 · 4 comments
Closed
7 tasks done
Labels
bug: upstream Bug in a dependency of Vite p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@ycmjason
Copy link
Contributor

Describe the bug

when import.meta.env is used in a ternary operator, following an array spread, vite can't seem to pick up import.meta.env is being used, and it becomes undefined.

E.g.

const v = [...(import.meta.env ? [true] : [false])]

will result in error:

Uncaught TypeError: Cannot read properties of undefined (reading 'DEV')

However without the ... spread seems to work ok.

const v = import.meta.env ? [true] : [false]

Please see the reproduction link.

Reproduction

https://stackblitz.com/edit/vitejs-vite-7mr8pa

Steps to reproduce

No response

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    vite: 4.0.4 => 4.0.4

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

This seems to be a bug in es-module-lexer: guybedford/es-module-lexer#145

const v = [...(import.meta.env ? [true] : [false])]

is transformed to

const v = [...import.meta.env ? [true] : [false]]

by esbuild (this transformation is valid).
Then, it faces that bug.

For a workaround, split it into two variables. This will avoid ...import.meta to be there.

const d = import.meta.env.DEV ? [true] : [false];
const s = [...d];

@sapphi-red sapphi-red added bug: upstream Bug in a dependency of Vite p2-edge-case Bug, but has workaround or limited in scope (priority) labels Jan 24, 2023
@sapphi-red sapphi-red linked a pull request Feb 13, 2023 that will close this issue
1 task
@bluwy bluwy removed a link to a pull request Feb 23, 2023
1 task
@bluwy
Copy link
Member

bluwy commented Feb 23, 2023

The bug is fixed in es-module-lexer now, but it introduces a regression at guybedford/es-module-lexer#148 so we can't bump it yet.

@sapphi-red
Copy link
Member

Closing as #12230 is merged.

@ycmjason
Copy link
Contributor Author

🙌🙌 cheers to everyone who worked on this

@github-actions github-actions bot locked and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

No branches or pull requests

3 participants