The plugin exposes env variables on the special import.meta.env object.
const env = require("rollup-plugin-import-meta-env");
env({
...process.env,
PROD: true,
DEV: false
})
console.log(import.meta.env.DEV);
console.log(false);
To prevent accidentally leaking env variables to the client, it is therefore recommend to always reference them using the full static string. For example, dynamic key access will not recommend.
DB_PASSWORD=foobar
VITE_SOME_KEY=123
// GOOD
console.log(import.meta.env.VITE_SOME_KEY);
// GOOD
console.log("123");
// BAD
var env=import.meta.env;
console.log(env.VITE_SOME_KEY);
// BAD
var env={
DB_PASSWORD: "foobar",
VITE_SOME_KEY: "123"
};
console.log(env.VITE_SOME_KEY);
To prevent accidentally leaking env variables to the client, config function to filter exposed variables. Only variables prefixed with VITE_ are exposed by e.g. the following config.
env({
PROD: true,
DEV:false
},{
filter(key){
if(key.startsWith("VITE_")){
return true;
}
return false;
}
})
- include
- exclude
- sourcemap
env({
PROD: true,
DEV:false
},{
filter(key){
if(key.startsWith("VITE_")){
return true;
}
return false;
},
sourcemap: true,
exclude: "node_modules/**"
})