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

bun-plugin-env-types

v1.5.0

Published

A Bun plugin for generating typescript types for environment variables

Downloads

17

Readme

Bun Plugin Env Types

Get autocomplete for your environment variables without manual configuration.

Overview

I just wanted type-safe environment variables availabe on process.env and Bun.env without having to manually add themm. So, I figure why not use the Bun plugin system to make a plugin that does this on the fly.

Can be used in 3 ways:

  1. Runtime Plugins - generate the env.d.ts file whenever you use a bun command. i.e. bun run, bun build, bun ./script.ts, etc.
  2. Build Plugins - generate the file whenever you run your build script (like an esbuild plugin).
  3. [Npx/Bunx] - just run the plugin directly with bunx bun-plugin-env-types and it will generate the file for you.

Installation

bun add -d bun-plugin-env-types

Usage

You have 2 options:

  1. Use as a runtime plugin in bunfig.toml
  2. Use as a Bun.build plugin

Runtime Plugin

Runtime Plugins are cool bc they allow you to run files when other bun processes run. In particular, it is most handy to use the preload functionality so it runs before any other process runs. You just add the file to the preload array in the bunfig.toml file.

Example

  1. create a file to preload like preload.ts.
  2. import and call the plugin:
// ./preload.ts
import envPlugin from 'bun-plugin-env-types'

envPlugin()

3.add the file to the bunfig.toml file in the preload array:

preload = [
  "./preload.ts"
]

Build Plugin

Build Plugins run alongside the bundler, whenever your build script runs. To use it:

// ./build.ts
import envPlugin from 'bun-plugin-env-types'

Bun.build({
    entrypoints: ['src/index.ts'],
    plugins: [envPlugin()]
})

Thats pretty much it. You will end up with an env.d.ts file in your project that looks like this

# ./.env
SECRET_KEY="sshhhhh"
DB_URL="https://long-production-url.com"
// ./env.d.ts
declare namespace NodeJS {
    export interface ProcessEnv {
        SECRET_KEY: string
        DB_URL: string
    }
}

Notes

  • You can modify the generated d.ts file and your changes will not be overwritten.
  • You can access your environment variables via process.env, Bun.env, or import.meta.env with autocomplete.