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-detect-prepare

v1.0.7

Published

A Vite plugin that detects git branch and sets custom environment variables

Readme

vite-plugin-detect-prepare

一个检测 git 分支并设置自定义环境变量的 Vite 插件。

✨ 特性

  • 🔍 检测当前 git 分支
  • 🎯 根据分支设置环境变量
  • 🚀 支持字符串和正则表达式匹配
  • ⚡ 轻量级,零依赖
  • 🛠️ TypeScript 支持

📦 安装

# npm
npm install vite-plugin-detect-prepare -D

# yarn  
yarn add vite-plugin-detect-prepare -D

# pnpm
pnpm add vite-plugin-detect-prepare -D

🚀 使用方法

基础用法

import { defineConfig } from 'vite'
import detectPrepare from 'vite-plugin-detect-prepare'

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

自定义配置

import { defineConfig } from 'vite'
import detectPrepare from 'vite-plugin-detect-prepare'

export default defineConfig({
  plugins: [
    detectPrepare({
      targetBranch: 'pre-release',
      envVarName: 'VITE_IS_PRERELEASE'
    })
  ]
})

使用正则表达式

import { defineConfig } from 'vite'
import detectPrepare from 'vite-plugin-detect-prepare'

export default defineConfig({
  plugins: [
    detectPrepare({
      targetBranch: /-pre-/,
      envVarName: 'VITE_PRE_ENVIRONMENT'
    })
  ]
})

⚙️ 配置项

| 选项 | 类型 | 默认值 | 说明 | | ------------ | ----------------- | -------------------- | ------------------------------ | | targetBranch | string\|RegExp | 'prepare' | 需要检测的分支名或正则表达式 | | envVarName | string | 'VITE_PRE_RELEASE' | 设置的环境变量名 |

📖 示例

检测预发布分支

// vite.config.js
export default defineConfig({
  plugins: [
    detectPrepare({
      targetBranch: /-(pre|beta|alpha)-/,
      envVarName: 'VITE_IS_PRERELEASE'
    })
  ]
})

在你的应用中使用:

// main.js
if (import.meta.env.VITE_IS_PRERELEASE === 'true') {
  console.log('当前为预发布环境')
  // 启用调试功能
  window.__DEBUG__ = true
}

Vue 项目中的使用

<template>
  <div>
    <div v-if="isPreRelease" class="pre-release-banner">
      ⚠️ 预发布版本
    </div>
    <!-- 其他内容 -->
  </div>
</template>

<script setup>
const isPreRelease = import.meta.env.VITE_PRE_RELEASE === 'true'
</script>

React 项目中的使用

function App() {
  const isPreRelease = import.meta.env.VITE_PRE_RELEASE === 'true'
  
  return (
    <div>
      {isPreRelease && (
        <div className="pre-release-banner">
          ⚠️ 预发布版本
        </div>
      )}
      {/* 其他内容 */}
    </div>
  )
}

🔧 工作原理

  1. 插件在构建时读取当前 git 分支信息
  2. 根据配置的 targetBranch 进行匹配
  3. 如果匹配成功,设置对应的环境变量为 'true'
  4. 如果不匹配,设置环境变量为 'false'

📝 注意事项

  • 环境变量只在构建时确定,运行时不会动态变化
  • 确保项目在 git 仓库中,否则无法检测分支信息
  • 环境变量名必须以 VITE_ 开头才能在客户端代码中访问

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📄 许可证

MIT © [Your Name]

🔗 相关链接


如果这个插件对你有帮助,请给个 ⭐ 支持一下!