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

@lifebyspot/ts-transform-inject-environment-variables

v1.0.3

Published

Injects hardcodede environment variables at compile time

Downloads

890

Readme

Transformer Plugin: Inject Environment Variables

This ttsc compliant transformer allows you to inject environment variables on your code at compile time.

This is done by replacing the initializers of variable declarations. You can learn how to do that, too!

Setup

Run npm install to save the package in your project.

npm install -D @lifebyspot/ts-transform-inject-environment-variables 

Usage

Basically, there's two main scenarios you can use to inject variables using this plugin.

First, the variables defined using process.env as initializer.

export const API_BASE_URL = process.env.API_BASE_URL;
export let ASDASDASDASD = process.env.ASDASDASDASD;

or

export const API_BASE_URL = process.env['API_BASE_URL'];

Will be converted into:

export const API_BASE_URL = 'https://api.example.com';
export let ASDASDASDASD;

Second, the variables defined using a destructuring syntax.

export const { API_BASE_URL } = process.env;

Will be converted into:

export const { API_BASE_URL = 'https://api.example.com' } = {};

Bonus: If no environment variable is found, the variable will be uninitialized. However, it's possible to use the nullish coalescing operator or object destructuring default values to define a default value to assign when this happens.

const { HOME = '/root' } = process.env;
const PATH = process.env.PATH ?? '/bin:/usr/bin';

Will be converted to:

const { HOME = '/root' } = {};
const PATH = '/bin:/usr/bin';

Note: Do not define variables that might be undefined as const.

export const ASDASDASDASD = process.env.ASDASDASDASD;

This is because, when running the compiled version, would end up with an uninitialized const variable, which is basically a violation of Javascript's rules.

export const ASDASDASDASD; // Not valid

Prerequisites

Configuration

Add the plugin to the plugins list on tsconfig.json.

{
    // ...
    "compilerOptions": {
        "plugins": [
            {
                "transform": "@lifebyspot/ts-transform-inject-environment-variables",
                "type": "config"
            }
        ]
    }
    // ...
}

Running

Just run ttsc against your project working directory. That it.

Contributing

Just send an issue or a PR! ;)