npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rollup-plugin-baked-env

v1.0.1

Published

Import process.env as a module for baking environment variables inside your bundle at build time.

Downloads

256

Readme

rollup-plugin-baked-env

LICENSE Node CodeFactor

Coverage Status Travis

Version Downloads

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

Usage Examples

Inside your code you can do something like this:

Basic usage

import { FOO_BAR } from 'process.env';

// use the variable
if (FOO_BAR === 'foo') {
    // do stuff
}

Import variable, aliasing it to another name

import { FOO_BAR as myFooBar } from 'process.env';

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

Import multiple variables

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

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

Readability and consistency

If you used to plugins like EnvironmentPlugin or DefinePlugin they use find/replace to replace the constants in your source codoe, but reading the code it's unclear where a variable is coming from, also ESLint and prettier thinks those variables are globals, since there's no reference about then anywhere. This fixes the, whithout having to make a whitelist.

Validation by default

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

Caveats importing all variables

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.

Works fine, but not recommended:

import * as env from 'process.env'; // ✋ AVOID!

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

Because if you do this, bad things happen:

import * as env from 'process.env'; // ✋ BAD!

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

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

Installation

NPM

npm install --D 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(),
    ],
};

Options

bakedEnv(variables, options)

| Parameters | Description | Type | Default Value | |-----------------------|-----------------------------------------------------------------------------------|---------|-----------------| | variables | The variables to be exposed. | object | process.env | | options.moduleName | The name of the faux module. You can choose another name like 'environment_vars'| string |'process.env'| |options.preferConst| Embed variables in the code as const instead of var. | boolean |true | |options.compact | Generate code without line breaks. | boolean |false | |options.indent | The indentation to use in the generated code. | string |'\t'` |

Options example

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

const myVariables = {
  MY_VAR_1: 'value1',
  MY_VAR_2: 'value2',
};

export default {
  // ...
  plugins: [
    bakedEnv(myVariables, {
      moduleName: 'enviroment_vars', // expose variables as a module called 'enviroment_vars' example: `import { FOO } from 'enviroment_vars';`
      preferConst: true,
      compact: false,
      indent: '  ',
    }),
  ],
};

License

The code is available under the MIT license.

Contributing

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