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

meta-env-typed

v2.0.3

Published

A vite plugin to generate import_meta.d.ts file for env typed.

Readme

Meta Env Typed

English Documentation

简介

meta-env-typed 是一个支持多种构建工具的插件,可以自动生成 import_meta.d.ts 文件,为你的环境变量提供完整的 TypeScript 类型支持。目前支持 Vite 和 Rsbuild,后续将支持 Rspack。

特性

  • 🚀 自动生成环境变量类型定义
  • 💡 完整的 TypeScript 支持
  • ⚡️ 零配置,开箱即用
  • 🔒 类型安全的环境变量访问
  • 🔄 环境文件变化时自动更新类型定义
  • 🛠️ 支持多种构建工具(Vite、Rsbuild,即将支持 Rspack)

安装

# 使用 npm
npm install meta-env-typed -D

# 使用 yarn
yarn add meta-env-typed -D

# 使用 pnpm
pnpm add meta-env-typed -D

使用方法

Vite

在你的 vite.config.ts 中配置插件:

import { defineConfig } from 'vite'
import envTyped from 'meta-env-typed/vite'

export default defineConfig({
  plugins: [
    envTyped({
      // 可选:指定环境变量前缀,默认为 'VITE_'
      envPrefix: ['VITE_', 'CUSTOM_'],
      // 可选:指定类型定义文件输出路径,默认为 'src/import_meta.d.ts'
      filePath: './src/types/import_meta.d.ts',
      // 可选:是否在类型中包含实际值,默认为 false
      valueInType: true,
      // 可选:是否在类型定义中使用分号,默认为 true
      semi: false
    })
  ]
})

Rsbuild

在你的 rsbuild.config.ts 中配置插件:

import { defineConfig } from '@rsbuild/core'
import envTyped from 'meta-env-typed/rsbuild'

export default defineConfig({
  plugins: [
    envTyped({
      // 可选:指定环境变量前缀,默认为 ''(空字符串,匹配所有变量)
      envPrefix: ['CUSTOM_', 'APP_'],
      // 可选:指定类型定义文件输出路径,默认为 'src/import_meta.d.ts'
      filePath: './src/types/import_meta.d.ts',
      // 可选:是否在类型中包含实际值,默认为 false
      valueInType: true
    })
  ]
})

在代码中使用

// 现在你可以获得完整的类型提示
console.log(import.meta.env.VITE_API_URL)
console.log(import.meta.env.CUSTOM_SECRET_KEY)

配置选项

插件支持以下配置选项:

interface EnvTypedOptions {
  /**
   * 环境文件目录
   * @default process.cwd()
   */
  envDir?: string

  /**
   * 环境变量前缀
   * Vite: @default 'VITE_'
   * Rsbuild: @default ''
   */
  envPrefix?: string | string[]

  /**
   * 类型定义文件输出路径
   * @default 'src/import_meta.d.ts'
   */
  filePath?: string

  /**
   * 是否在类型中包含实际值
   * @default false
   * @example
   * // 当 valueInType 为 false 时
   * VITE_API_URL: string
   * // 当 valueInType 为 true 时
   * VITE_API_URL: 'https://api.example.com'
   */
  valueInType?: boolean

  /**
   * 是否在类型定义中使用分号
   * @default true
   */
  semi?: boolean
}

示例

基础配置

// vite.config.ts
import { defineConfig } from 'vite'
import envTyped from 'meta-env-typed/vite'

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

自定义环境变量前缀

// vite.config.ts
export default defineConfig({
  plugins: [
    envTyped({
      envPrefix: ['VITE_', 'CUSTOM_']
    })
  ]
})

包含实际值

// vite.config.ts
export default defineConfig({
  plugins: [
    envTyped({
      valueInType: true
    })
  ]
})

Rsbuild 示例

// rsbuild.config.ts
import { defineConfig } from '@rsbuild/core'
import envTyped from 'meta-env-typed/rsbuild'

export default defineConfig({
  plugins: [
    envTyped({
      envPrefix: ['APP_', 'PUBLIC_'],
      filePath: './types/env.d.ts'
    })
  ]
})

支持的构建工具

  • Vite (v6.0.0+)
  • Rsbuild (v1.3.22+)
  • 🚧 Rspack (即将支持)

📝 贡献指南

欢迎提交 issuespull requests 来帮助改进 meta-env-typed

📄 许可证

MIT

联系我们