1.0.3 • Published 3 years ago

esbuild-env-parsing v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Esbuild environment parsing

I want something that will throw an error immediately before any environment variable with undefined value can squeeze into my program

So if you have a lot of optional environment variables, this is not for you

I like to use esbuild, although it's possible to do as the following according to this issue

const { build } = require('esbuild');
const define = {};

for (const k in process.env) {
    define[`process.env.${k}`] = JSON.stringify(process.env[k]);
}

const options = {
    stdio: 'inherit',
    entryPoints: ['./src/main.ts'],
    outfile: './dist/main.js',
    bundle: true,
    define,
};

build(options).catch(() => process.exit(1));

I wanna be explicit about the environment variable(s) I passed into esbuild without mutation

Since I need to be explicit, I must ensure that each environment variable I passed is defined

Hence, I built this package

How do I use it?

// CommonJS
const { build } = require('esbuild');
require('dotenv').config({});
const { parseAsEnvs } = require('esbuild-env-parsing');

// ESModule
import { build } from 'esbuild';
import dotenv from 'dotenv';
const { parseAsEnvs } = require('esbuild-env-parsing');

dotenv.config({});

// this is how you use it for esbuild
const options = {
    stdio: 'inherit',
    entryPoints: ['./src/main.ts'],
    outfile: './dist/main.js',
    bundle: true,
    // explicit about the variable passed in
    define: parseAsEnvs(['NODE_ENV', 'WHAT_NOT', 'API']),
};

build(options).catch(() => process.exit(1));

Can I use it without esbuild

Sort of can, but you will not be able to use parseAsEnvs, instead you will be using parseAsStringEnv, parseAsNumEnv or parseAsBooleanEnv

Here's an example

import { parseAsStringEnv } from 'esbuild-env-parsing';

// if it has to be boolean or number, use `parseAsBooleanEnv` and `parseAsNumEnv` respectively
const apiKey = parseAsStringEnv({
    env: process.env.API_KEY,
    name: 'API_KEY',
});

Now you may be wondering why do you need to pass name? It's something that can help you to identify which environment variable is missing, because I will throw an error when an environment variable is undefined

Can I raise an issue?

Why not? Feel free to raise an issue if you have a question, an enhancement, or a bug report.

Can I contribute

I am glad that you are willing to contribute However, before that, I'd like to have a few words about contribution.

  • Use TypeScript
  • Write Test Code

Changes

Kindly refer to CHANGELOG

How to install

yarn add esbuild-env-parsing

OR

npm i esbuild-env-parsing

OR

pnpm add esbuild-env-parsing
1.0.2

3 years ago

1.0.1

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago