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

vite-plugin-env-inject

v0.2.1

Published

Vite plugin for runtime environment variable injection in browser builds and Docker deployments

Readme

vite-plugin-env-inject

npm version License: MIT Release TypeScript Vite

English | 简体中文

为浏览器构建注入运行时环境变量的 Vite 插件。

动机

Vite 默认在构建时将 import.meta.env.VITE_* 静态内联进产物,适合固定配置部署,但不适合「同一 Docker 镜像在不同环境使用不同配置」的场景。

本插件在不改动业务代码写法的前提下,将环境变量访问切换为运行时配置脚本。构建阶段不会读取环境变量,只在容器启动或 vite dev 时才注入实际的 VITE_* 值。

特性

  • 自动将 import.meta.env.VITE_* 和 process.env.VITE_* 转为运行时全局变量访问
  • 自动向 index.html 注入配置脚本
  • vite dev 时通过 dev server 提供实时配置
  • 构建时输出运行时配置模板(__vite_env_config__.js.template)

安装

npm install -D vite-plugin-env-inject

用法

// vite.config.ts
import { defineConfig } from 'vite'
import { envInject } from 'vite-plugin-env-inject'

export default defineConfig({
  plugins: [envInject()],
})

源码中照常使用环境变量:

const apiUrl = import.meta.env.VITE_API_URL
const token = process.env.VITE_API_TOKEN

构建时插件只做代码转换并输出配置模板;运行时再由配置脚本填入真实值,并在应用 bundle 加载前注入。

| 阶段 | 行为 | |------|------| | vite build | 转换 env 访问、注入 script 标签、输出空占位文件 + .template | | 容器启动 | 由 Dockerfile 中的 envsubst 将 .template 渲染为 __vite_env_config__.js | | vite dev | dev server 从当前环境动态提供配置 |

配置项

envInject({
  prefix: 'VITE_',
  globalName: '__VITE_INJECT_ENV__',
  configFile: '__vite_env_config__.js',
  include: ['VITE_EXTRA_KEY'],
  debug: true,
})

开发

npm install
npm run build
npm test
npm run dev

License

MIT