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

iasei-cond-compile

v1.0.0

Published

Babel condition compile plugin, remove code at compile time, support TS/ES6/Vue

Readme

iasei-cond-compile

编译期条件编译 Babel 插件。通过 #ifdef / #endif 注释标记代码块,在构建时按配置保留或删除,支持 TypeScript、ES6+ 与 Vue SFC。

特性

  • 编译期移除未命中条件的代码,不进入最终产物
  • 支持 #ifdef KEY=VALUE 条件语法(大小写不敏感)
  • 支持 .ts / .js / .tsx / .jsx 及 Vue 单文件组件
  • 提供 Vite 插件,在 @vitejs/plugin-vue 处理 SFC 之前介入,避免条件注释被提前剥离

安装

pnpm add -D iasei-cond-compile
# 或
npm install -D iasei-cond-compile

对等依赖

| 包 | 是否必需 | 说明 | |---|---|---| | @babel/core | 是 | Babel 核心 | | vite | 否 | 使用 Vite 插件时需要 | | @vue/compiler-sfc | 否 | 处理 .vue 文件时需要 |

条件语法

使用行注释标记条件块。注释需写在被控制语句的前一行(leading comment):

// #ifdef ENV=DEV
const devLog = '开发环境代码';
console.log(devLog);
// #endif

// #ifdef ENV=PROD
const prodLog = '生产环境代码';
console.log(prodLog);
// #endif

// 普通代码,始终保留
const normal = '正常代码';
  • #ifdef KEY=VALUE:当插件选项中 KEY 的值等于 VALUE 时保留该块内代码,否则删除
  • #endif:结束当前条件块
  • 选项值会转为字符串后比较(String(condition[key]) !== val

Babel 用法

// babel.config.js
module.exports = {
  plugins: [
    ['iasei-cond-compile', { ENV: 'DEV' }],
  ],
};
import { transform } from '@babel/core';
import condCompilePlugin from 'iasei-cond-compile';

const result = transform(sourceCode, {
  plugins: [[condCompilePlugin, { ENV: 'DEV' }]],
});

Vite 用法

condCompilePreVue 放在 @vitejs/plugin-vue 之前,并设置 enforce: 'pre'(插件内部已配置):

// vite.config.ts
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { condCompilePreVue } from 'iasei-cond-compile/vite';

export default defineConfig({
  plugins: [
    condCompilePreVue({ ENV: 'DEV' }),
    vue(),
  ],
});

Vite 插件会处理:

  • .vue:替换 <script> / <script setup> 中的条件代码(template 不受影响)
  • .ts / .js / .tsx / .jsx:直接对源码做条件编译

自动跳过 node_modules.d.ts 文件。

Vue SFC 示例

<script setup lang="ts">
// #ifdef ENV=DEV
const devLog = '开发环境';
console.log(devLog);
// #endif

// #ifdef ENV=PROD
const prodLog = '生产环境';
console.log(prodLog);
// #endif

const normal = '普通代码';
</script>

<template>
  <div>{{ normal }}</div>
</template>

构建时传入 { ENV: 'DEV' } 会保留开发分支、移除生产分支;template 区块保持不变。

配置选项

type CondCompileOptions = Record<string, string | number | boolean>;

示例:

{ ENV: 'DEV' }
{ PLATFORM: 'WEB', DEBUG: true }

使用.env变量

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "");

  return {
    plugins: [
      condCompilePreVue({ ENV_DEV: env.ENV_DEV }),
      vue(),
    ],
  };
});

注意事项

  • 条件注释必须作为下一行语句的前导注释,插件在 ProgramBlockStatement 层级处理
  • 同一文件中可存在多个独立的 #ifdef#endif
  • 未匹配条件的代码块会在编译阶段被完全移除,不会出现在输出中
  • Vue 外部 script(src 属性引用)不会被内联处理

开发

pnpm install
pnpm run build    # 构建 dist
pnpm run dev      # 监听构建
pnpm test         # 运行测试

License

MIT