rollup-plugin-import-meta-env
v1.1.2
Published
Inject environment variables like vite.
Readme
Env Variables
The plugin exposes env variables on the special import.meta.env object.
Config
const env = require("rollup-plugin-import-meta-env");env({
PROD: true,
DEV: false
})Before
console.log(import.meta.env.DEV);After
console.log(false);Remove unuse variables by tree-shaking.
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=123Before
// GOOD
console.log(import.meta.env.VITE_SOME_KEY);After
// GOOD
console.log("123");Before
// BAD
var env=import.meta.env;
console.log(env.VITE_SOME_KEY);After
// BAD
var env={
DB_PASSWORD: "foobar",
VITE_SOME_KEY: "123"
};
console.log(env.VITE_SOME_KEY);Filter exposed variables
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(Object.fromEntries(
Object.entries(process.env).filter(([key, value]) => {
if(!(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key))) {
return false;
}
return key.startsWith("VITE_");
})
))Other Options
- include
- exclude
- sourcemap
