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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@computerwwwizards/vite-define-plugin

v1.0.6

Published

Flatten a JSON or plain JavaScript config object into Vite's define map under `import.meta.env.*` so you can read structured client-side config at build time without stringifying everything yourself.

Readme

Codewwwizards Vite Define Plugin

Flatten a JSON or plain JavaScript config object into Vite's define map under import.meta.env.* so you can read structured client-side config at build time without stringifying everything yourself.

Given a JSON like:

{
  "myCustomConfig": {
    "envName": "value"
  }
}

You can access it as:

// prints "value"
console.log(import.meta.env.myCustomConfig.envName)

Important: this plugin uses Vite's define replacement. It's a compile-time text substitution, so destructuring or reading whole objects at runtime won't work:

const { myCustomConfig } = import.meta.env
// prints undefined
console.log(myCustomConfig?.envName)

// also prints undefined
console.log(import.meta.env.myCustomConfig)

Why? Because these aren't real reads; Vite replaces occurrences with hardcoded values during build. This is intended for client-side configuration, separate from .env files.

Motivation

.env files are great, but everything becomes strings. Many apps need arrays, booleans, numbers, and nested shapes for client configuration. A JSON (or JS object) is a natural fit. This plugin lets you keep using the familiar import.meta.env.* access pattern while sourcing values from a structured config.

Quick start

vite.config.ts

import { defineConfig } from 'vite'
import addConfigToEnv from '@computerwwwizards/vite-define-plugin'

export default defineConfig({
  plugins: [addConfigToEnv()]
})

By default, the plugin looks for:

  • client-config.dev.json in development mode
  • client-config.json in other modes

…relative to Vite's root (or process.cwd() if root isn't set).

Example client-config.dev.json:

{
  "someConfig": [5]
}

Usage in source code:

// will print [5]
console.log(import.meta.env.someConfig)

Vite will replace that with:

console.log([5])

Options API

The plugin accepts the following options. Types are shown for reference.

| Option | Type | Default | Description | |---------------------|------------------------------------|-------------------------|-------------| | initialValuesFile | string | auto-detected file path | Absolute or relative path to a JSON file with initial values. Used when initialValues is not provided. | | initialValuesByMode | Record<string, string> | {} | Select a different JSON file per Vite mode (e.g., { development: 'client-config.dev.json', production: 'client-config.json' }). Overrides initialValuesFile when the key matches the active mode. | | initialValues | object | read from file | Provide a plain object to use directly. When set, the plugin does not read any file. | | keyPrefix | string | 'import.meta.env' | Prefix used for generated keys. Change if you need a different root than import.meta.env. | | separator | string | '.' | Key separator used when flattening nested objects. | | isPlainObject | (obj: unknown) => boolean | internal default | Custom plain-object check, if you need to support different prototypes. |

File resolution, when initialValues is not provided:

  1. initialValuesByMode[env.mode] if present
  2. Else initialValuesFile if provided
  3. Else ${root || process.cwd()}/client-config.dev.json in development, or ${root || process.cwd()}/client-config.json otherwise

Examples

1) Custom config path (single file)

import { defineConfig } from 'vite'
import addConfigToEnv from '@computerwwwizards/vite-define-plugin'

export default defineConfig({
  plugins: [
    addConfigToEnv({
      initialValuesFile: 'config/app-config.json'
    })
  ]
})

2) Custom path by mode

import { defineConfig } from 'vite'
import addConfigToEnv from '@computerwwwizards/vite-define-plugin'

export default defineConfig({
  plugins: [
    addConfigToEnv({
      initialValuesByMode: {
        development: 'config/app-config.dev.json',
        staging: 'config/app-config.staging.json',
        production: 'config/app-config.prod.json'
      }
    })
  ]
})

3) In-memory plain JS object (from a custom file)

You can provide values directly using initialValues. When set, no file is read.

config/appConfig.ts

export const appConfig = {
  featureFlags: {
    newNav: true
  },
  api: {
    baseUrl: 'https://api.example.com'
  }
}

vite.config.ts

import { defineConfig } from 'vite'
import addConfigToEnv from '@computerwwwizards/vite-define-plugin'
import { appConfig } from './config/appConfig'

export default defineConfig({
  plugins: [
    addConfigToEnv({
      initialValues: appConfig
    })
  ]
})

Or inline:

addConfigToEnv({
  initialValues: {
    someConfig: [5],
    flags: { a: true }
  }
})

Notes

  • Values are embedded at build time via define. Avoid placing secrets here.
  • Non-string types (numbers, booleans, arrays, objects) are supported and preserved.
  • To change where keys are generated, set keyPrefix (e.g., keyPrefix: 'process.env').

Roadmap

  • [ ] Add type generation
  • [ ] Add possibility to merge config with harcoded values