rollup-plugin-baked-env

Software
Cover image

image GitHub - victornpb/rollup-plugin-baked-env: A Rollup Plugin for baking a process.env module inside your code\ A Rollup Plugin for baking a process.env module inside your code - GitHub - victornpb/rollup-plugin-baked-env: A Rollup Plugin for baking a process.env module inside your code\ https://github.com/victornpb/rollup-plugin-baked-env

This plugin allow you to use environment variables inside your code by importing process.env as a faux module. The environment variable will be baked in your code at compile time. Only the variables being used are included.

Installation

NPM

npm install --save-dev rollup-plugin-baked-env

Yarn

yarn add --dev rollup-plugin-baked-env

Adding to your project

rollup.config.js

import bakedEnv from 'rollup-plugin-baked-env';

export default {
    // ...
    plugins: [
        bakedEnv(),
    ],
};

Usage Examples

Inside your code you can do something like this:

Basic usage

import { NODE_ENV } from 'process.env';

if (NODE_ENV === 'production') {
    // code
}

Import variable, aliasing it to another name

import { ROLLUP_WATCH as isDev } from 'process.env';

if (isDev) {
    // code
}
else {
    // code
}

Import multiple variables

import { NODE_ENV, FOO, BAR } from 'process.env';

console.log(FOO, BAR);
if (NODE_ENV === 'production') {
    // code
}

Import all variables

import * as env from 'process.env'; // āœ‹ use with caution!

if (env.production !== 'production') {
    console.log('My home directory is ' + env.HOME);
}

NOTE:Ā If you doĀ import * as env from 'process.env'Ā never use theĀ envĀ variable directly! This will cause rollup to embed ALL environment variables inside your bundle.

import * as env from 'process.env';

// DON'T DO THIS!
console.log(env); // BAD!

Object.keys(env).filter(/* ... */) // BAD! (if you access the env at runtime, it will force rollup to embed everything)

Validation by default

Your code will fail to compile if the code imports variables that are not set!

Options

License

The code is available under theĀ MITĀ license.

Contributing

We are open to contributions, seeĀ CONTRIBUTING.mdĀ for more info.