unplugin-flag
TypeScript icon, indicating that this package has built-in type declarations

0.0.0 • Public • Published

unplugin-flag

A powerful plugin for safely loading environment variables with schema validation. Simple to use with virtual modules, type-safe with TypeScript support, and designed for a better developer experience. 🔥 🚀 👷

npm package npm package downloads

Supports:

Next.js Vite Esbuild Webpack Astro Rollup Rolldown Rspack Farm

Table of Contents

Features

  • 🔐 Secure: Load environment variables safely with schema validation to avoid errors and prevent exposing sensitive information.
  • ✍️ Type-Safe: Automatically inferred types based on your schema configuration.
  • Developer-Friendly: Lightweight and simple API for managing environment variables with seamless TypeScript integration.

Installation

Install via your preferred package manager:

npm install unplugin-flag       # npm

yarn add unplugin-flag          # yarn

bun add unplugin-flag           # bun

pnpm add unplugin-flag          # pnpm

Basic Usage

Configuration

Next.js
// next.config.mjs
import Environment from 'unplugin-flag/webpack'

const nextConfig = {
    webpack(config){
        config.plugins.push(Environment('PREFIX_APP'))
        return config
    },
}

export default nextConfig


Vite
// vite.config.ts
import Environment from 'unplugin-flag/vite'

export default defineConfig({
  plugins: [
    Environment('PREFIX_APP'),
  ],
})


Farm
// farm.config.ts
import Environment from 'unplugin-flag/farm'

export default defineconfig({
  plugins: [
    Environment('PREFIX_APP'),
  ],
})


Rspack
// rspack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-flag/rspack')('PREFIX_APP')
  ]
}


Rollup
// rollup.config.js
import Environment from 'unplugin-flag/rollup'

export default {
  plugins: [
    Environment('PREFIX_APP'),
  ],
}


Rolldown
// rolldown.config.js
import Environment from 'unplugin-flag/rolldown'

export default {
  plugins: [
    Environment('PREFIX_APP'),
  ],
}


Webpack
// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-flag/webpack')("PREFIX_APP")
  ]
}


Esbuild
// esbuild.config.js
import { build } from 'esbuild'
import Environment from 'unplugin-flag/esbuild'

build({
  plugins: [Environment('PREFIX_APP')],
})


Astro
// astro.config.mjs
import { defineConfig } from 'astro/config'
import Environment from 'unplugin-flag/astro'

build({
  plugins: [Environment('PREFIX_APP')],
})


Schema Validation

Use the schema option with zod for validating environment variables. This automatically creates a virtual module with types.

Environment({
    match: 'PREFIX_', // or ['PREFIX_', 'PREFIX2_']
    schema: {
        PREFIX_APP_NAME: z.string().min(1).default('My App'),
        PREFIX_APP_PORT: z.coerce.number().min(1).default(3000),
    },
})

Intellisense with TypeScript

To enable Intellisense for environment variables, add the following to your tsconfig.json:

{
    "compilerOptions": {
        "types": ["unplugin-flag/client"]
    }
}

Accessing Environment Variables

You can access environment variables from the virtual module @env:

import { env } from '@env'

console.log(env.PREFIX_APP_NAME)

If you want to customize the module name, use the moduleEnvName option:

// in plugin configuration
Environment({
    match: 'PREFIX_', // or ['PREFIX_', 'PREFIX2_']
    schema: ...,
    moduleEnvName: 'MYENV',
})


// you can access it from `MYENV` module
import { env } from 'MYENV'

console.log(env.PREFIX_APP_NAME)

Client/Server Environment

To handle environment variables separately for client and server, use the client and server options. This allows for precise control over which variables are accessible in different environments.

[!NOTE] When using the client and server options, you cannot access environment variables through the @env module. Instead, use @env/client for client-side variables and @env/server for server-side variables by default.

Example configuration:

Environment({
    client: {
        match: 'CLIENT_',
        schema: {
            CLIENT_APP_NAME: z.string().min(1).default('My App'),
        },
    },
    server: {
        match: 'SERVER_',
        schema: {
            SERVER_APP_DB_URL: z.string().min(1).default('postgres://localhost:5432/mydb'),
        }
    },
})

If you'd like to change the default module names @env/client and @env/server, you can use the optional moduleEnvName key to define a custom module name for accessing the environment variables.

[!CAUTION] When customizing moduleEnvName for client and server, ensure the module names are different. Using the same name for both client and server can cause conflicts and unpredictable behavior.

Environment({
    client: {
        match: 'CLIENT_',
        schema: {
            CLIENT_APP_NAME: z.string().min(1).default('My App'),
        },
        moduleEnvName: '@myenv/client', // Optional: Customize the client module name
    },
    server: {
        match: 'SERVER_',
        schema: {
            SERVER_APP_DB_URL: z.string().min(1).default('postgres://localhost:5432/mydb'),
        },
        moduleEnvName: '@myenv/server', // Optional: Customize the server module name
    },
})

Accessing Client/Server Environment

// client environment
import { env } from '@env/client'

env.CLIENT_APP_NAME // typed with string

// server environment
import { env } from '@env/server'

env.SERVER_APP_DB_URL // typed with string

Acknowledgements


Package Sidebar

Install

npm i unplugin-flag

Weekly Downloads

0

Version

0.0.0

License

MIT

Unpacked Size

28.9 kB

Total Files

46

Last publish

Collaborators

  • ri7nz