-
Notifications
You must be signed in to change notification settings - Fork 11
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
syntax caused runtime error
#1
Comments
Hi @ColorfulCow, Could you please test if this works? const ENV_NAME = getConfigFileName({
VITE_GLOB_APP_SHORT_NAME: import.meta.env.VITE_GLOB_APP_SHORT_NAME
}); I've got a theory that the way we identify which variables replace relies on us knowing which variable you're accessing, but as you're passing it into the function, we lose the link. The main reason it was built this way instead of just replacing
Given I assume your code looks something like: function getConfigFileName(env) {
return env.VITE_GLOB_APP_SHORT_NAME;
}
// ...
const ENV_NAME = getConfigFileName(import.meta.env); I wonder if you're in violation of the second point as I don't know how Vite could statically replace that reference to I also wonder what the expected thing to happen here is. We could replace it with an empty object instead of To fix the issue we would need to replace it with an object containing the built in variables and any |
Ok, so after a quick test I can confirm that their docs are a bit misleading and the usage in the issue does work, as does the irregular access ( console.log("normal usage", import.meta.env.VITE_TEST_VALUE)
console.log("irregular usage", import.meta.env["VITE_TEST_VALUE"])
const key = "TEST_VALUE"
console.log("irregular usage 2", import.meta.env[`VITE_${key}`])
function printTestValue(env: Record<string, unknown>) {
console.log("normal function:", env.VITE_TEST_VALUE)
}
printTestValue(import.meta.env)
function printSomeValue(env: Record<string, unknown>, key: string) {
console.log("irregular function:", env[key])
}
printSomeValue(import.meta.env, "VITE_TEST_VALUE") gets output as (with minification disabled): console.log("normal usage", "yes");
console.log("irregular usage", {VITE_TEST_VALUE: "yes", BASE_URL: "/", MODE: "production", DEV: false, PROD: true}["VITE_TEST_VALUE"]);
const key = "TEST_VALUE";
console.log("irregular usage 2", {VITE_TEST_VALUE: "yes", BASE_URL: "/", MODE: "production", DEV: false, PROD: true}[`VITE_${key}`]);
function printTestValue(env) {
console.log("normal function:", env.VITE_TEST_VALUE);
}
printTestValue({VITE_TEST_VALUE: "yes", BASE_URL: "/", MODE: "production", DEV: false, PROD: true});
function printSomeValue(env, key2) {
console.log("irregular function:", env[key2]);
}
printSomeValue({VITE_TEST_VALUE: "yes", BASE_URL: "/", MODE: "production", DEV: false, PROD: true}, "VITE_TEST_VALUE"); I'll update the plugin to support this in the same way (i.e. single value replacement where we can, full object replacement where we can't). |
fix: replace unknown import.meta.env lookups with full env object
🎉 This issue has been resolved in version 1.0.4 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version 1.0.3 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version 1.0.0 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version 1.1.0 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
dependence
babel-preset-vite
1.0.3:@babel/core
7.13.14:Relevant code or config:
What you did:
npm run serve
What happened:
when i changed code like this, project start without error
The text was updated successfully, but these errors were encountered: