All Projects → badcafe → rollup-plugin-inject-process-env

badcafe / rollup-plugin-inject-process-env

Licence: MIT license
Inject environment variables in process.env with Rollup

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to rollup-plugin-inject-process-env

web-config
A Rollup configuration to build modern web applications with sweet features as for example SCSS imports, Service Worker generation with Workbox, Karma testing, live reloading, coping resources, chunking, treeshaking, Typescript, license extraction, filesize visualizer, JSON import, budgets, build progress, minifying and compression with brotli a…
Stars: ✭ 17 (-64.58%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-closure-compiler-js
Rollup plugin for optimizing JavaScript with google-closure-compiler-js.
Stars: ✭ 31 (-35.42%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-external-globals
Transform external imports into global variables like output.globals.
Stars: ✭ 57 (+18.75%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-generate-package-json
Generate package.json file with packages from your bundle using Rollup
Stars: ✭ 29 (-39.58%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-purs
Bundles PureScript modules with Rollup
Stars: ✭ 71 (+47.92%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-lit-css
Moved to https://github.com/bennypowers/lit-css
Stars: ✭ 35 (-27.08%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-collect-sass
Collect Sass, then compile
Stars: ✭ 17 (-64.58%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-html
Import HTML files as strings in rollup build
Stars: ✭ 36 (-25%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-tagged-template
Use plain HTML files as tagged templates.
Stars: ✭ 24 (-50%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-jscc
Conditional compilation and compile-time variable replacement for Rollup
Stars: ✭ 52 (+8.33%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-sizes
Rollup plugin to display bundle contents & size information
Stars: ✭ 77 (+60.42%)
Mutual labels:  rollup, rollup-plugin
postcss-font-grabber
A postcss plugin, it grabs remote font files and update your CSS, just like that.
Stars: ✭ 26 (-45.83%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-markdown
import JavaScript from Markdown code blocks
Stars: ✭ 16 (-66.67%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-generate-html-template
Rollup plugin for automatically injecting a script tag with the final bundle into an html file.
Stars: ✭ 58 (+20.83%)
Mutual labels:  rollup, rollup-plugin
rollup-plugin-userscript-metablock
Transform json file to userscript metablock and append on.
Stars: ✭ 26 (-45.83%)
Mutual labels:  rollup, rollup-plugin
vue-cli-template-library
Template for developing open-source vue.js libraries with Rollup + Jest + Babel + Storybook + TravisCI + SemanticRelease
Stars: ✭ 61 (+27.08%)
Mutual labels:  rollup
11r
America's favorite Eleventy blog template.
Stars: ✭ 135 (+181.25%)
Mutual labels:  rollup
svelte-pwa-now
A PWA ready Svelte v3.0 starter template with Tailwind, Now integration and optional Typescript suppot
Stars: ✭ 138 (+187.5%)
Mutual labels:  rollup
rollup-boilerplate
Simple boilerplate to make components libraries with React.
Stars: ✭ 13 (-72.92%)
Mutual labels:  rollup
scriptable-ts-boilerplate
A boilerplate for creating remote-updatable Scriptable widgets. Includes setup, components, utils and examples to develop in the comfort of TypeScript.
Stars: ✭ 40 (-16.67%)
Mutual labels:  rollup

rollup-plugin-inject-process-env

Inject process.env environment variables in a browser rollup bundle.

Why ?

Because replacing a string typically with rollup-plugin-replace works in one case :

    console.log(process.env.NODE_ENV);

...but not in all other cases :

    console.log(process.env['NODE_ENV']);
    const { NODE_ENV, NODE_PORT } = process.env;
    console.log(NODE_ENV);

Worse : sometimes, such substitution :

    if (process.env.NODE_ENV === 'production') {

...will be expand to :

    if ('production' === 'production') {

...and make some linter complain.

How ?

Installation

npm install --save-dev rollup-plugin-inject-process-env

Usage

Pass any JSON object to the plugin that will be set as the process.env value. This object accept members value of any type.

    function injectProcessEnv(
        env: object,
        options?: {
            include?: string | string[],
            exclude?: string | string[],
            verbose?: boolean
        }
    )

Note: if you use the commonjs plugin injectProcessEnv must be listed after it in your plugins list. Otherwise you will see the error 'import' and 'export' may only appear at the top level.

Example :

import injectProcessEnv from 'rollup-plugin-inject-process-env';

// ... usual rollup stuff

    plugins: [
        typescript(),
        commonjs(),
        injectProcessEnv({ 
            NODE_ENV: 'production',
            SOME_OBJECT: { one: 1, two: [1,2], three: '3' },
            UNUSED: null
        }),
        nodeResolve()
    ],

Example with environment variables passed in the CLI :

        injectProcessEnv({ 
            NODE_ENV: process.env.NODE_ENV,
            SOME_OBJECT: JSON.parse(process.env.SOME_OBJECT),
            UNUSED: null
         }),

Options

  • The verbose option allows to show which file is included in the process and which one is excluded.
  • The include and exclude options allow to explicitely specify with a minimatch pattern the files to accept or reject. By default, all files are targeted and no files are rejected.

Example :

        injectProcessEnv({
            NODE_ENV: 'production',
            SOME_OBJECT: { one: 1, two: [1,2], three: '3' },
            UNUSED: null
        }, {
            exclude: '**/*.css',
            verbose: true
        }),
        postcss({
            inject: true,
            minimize: true,
            plugins: [],
        }),

Output example of the verbose option :

[rollup-plugin-inject-process-env] Include /path/to/src/index.ts
[rollup-plugin-inject-process-env] Exclude rollup-plugin-inject-process-env
[rollup-plugin-inject-process-env] Exclude /path/to/src/style.3.css
[rollup-plugin-inject-process-env] Include /path/to/node_modules/style-inject/dist/style-inject.es.js

Icing on the cake

You might notice that as mentionned in the documentation https://nodejs.org/api/process.html#process_process_env environment variables are always string, number or boolean.

With rollup-plugin-inject-process-env, you may inject safely any JSON object to a process.env property, as shown in the example above.

Troubleshootings

'globalThis' is undefined

This error may occur in target environments where globalThis is undefined. You should use a polyfill to fix it :

npm install @ungap/global-this

And include it in your code, e.g. :

import '@ungap/global-this';

Reports

Code quality reports

Metrics

Files 1
Lines of code 59 (w/o comments)
Comments 4 (+ 1 with code)
Empty lines 4
Total lines 67 (w/o tests)
TODO 0 lines
Tests 455 (w/o comments)

Linter

0 problems

Tests

Tests suites Tests
Failed 0 0
Passed 4 12
Pending 0 0
Error 0
Total 4 12

/test/browser.test.ts 1.727s

Status Suite Test
Browser get NODE_ENV
Browser get SOME_OBJECT
Browser get MISSING

/test/node.3.test.ts 0.173s

Status Suite Test
Filter out CSS get NODE_ENV
Filter out CSS get SOME_OBJECT
Filter out CSS get MISSING

/test/node.test.ts 0.162s

Status Suite Test
Node get NODE_ENV
Node get SOME_OBJECT
Node get MISSING

/test/node.2.test.ts 0.161s

Status Suite Test
Node get NODE_ENV
Node get SOME_OBJECT
Node get MISSING

License

MIT

Who ?

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].