# vite-plugin-conditional-compiler

Latest version **0.4.0** (published 2025-11-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install vite-plugin-conditional-compiler
pnpm add vite-plugin-conditional-compiler
yarn add vite-plugin-conditional-compiler
bun add vite-plugin-conditional-compiler
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2025-11-05 |
| First published | 2022-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 84 |
| Maintainers | kejunmao |
| Keywords | vite-plugin |

## Links

- npm: https://www.npmjs.com/package/vite-plugin-conditional-compiler
- Repository: https://github.com/KeJunMao/vite-plugin-conditional-compile
- Issues: https://github.com/KeJunMao/vite-plugin-conditional-compile/issues
- npm.io page: https://npm.io/package/vite-plugin-conditional-compiler

## Dependencies (2)

- [@ampproject/remapping](https://npm.io/package/@ampproject/remapping.md) ^2.2.1
- [unplugin-preprocessor-directives](https://npm.io/package/unplugin-preprocessor-directives.md) ^1.2.0

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 0.4.0 (latest) — 2025-11-05
- 0.3.1 — 2024-01-25
- 0.3.0 — 2024-01-21
- 0.2.1 — 2023-10-24
- 0.2.0 — 2023-09-28
- 0.1.1 — 2023-03-03
- 0.0.6 — 2022-12-22
- 0.0.5 — 2022-12-10
- 0.0.4 — 2022-11-07
- 0.0.3 — 2022-11-05
- 0.0.2 — 2022-10-11
- 0.0.1 — 2022-10-10

## README

<p align="center">
  <img width="300" src="./assets/logo.svg" alt="logo of vite-plugin-conditional-compiler repository">
</p>

<h2 align='center'>vite-plugin-conditional-compiler</h2>

<p align="center">Conditional compilation syntax based on environment variables</p>

English | [简体中文](./README.zh-CN.md)

> **Important**
> From v0.2.0, this plugin is essentially an wrapper of [unplugin-preprocessor-directives](https://github.com/KeJunMao/unplugin-preprocessor-directives).

## Installation

```
pnpm i -D vite-plugin-conditional-compiler
```

## Usage

```ts
// vite.config.ts
import { defineConfig } from "vite";
import ConditionalCompile from "vite-plugin-conditional-compiler";

export default defineConfig({
  plugins: [ConditionalCompile()],
});
```

## Syntax

Start with `#v-ifdef`, then append `%ENV%`, end with `#v-endif`, you can also use `#v-elif` and `#v-else`.

- `#v-ifdef`: if defined
- `%ENV%` Vite environment variables

> **Warning**
> The `#v-ifndef` is deprecated in the v0.2.0 version

## Configuration

```ts
export interface Options {
  /**
   * @default ["**/*"]
   */
  include: FilterPattern;
  /**
   * @default []
   */
  exclude: FilterPattern;
}
```

## Examples

```js
// Compile in production environment only
// #v-ifdef PROD
value = 1;
// #v-endif
```

```js
// Condition or, not supported &&
// Compile in production or development
// #v-ifdef PROD||DEV
value = 1;
// #v-endif
```

```js
// Allow custom environment variables
// Compile only when 'VITE_MY_ENV' exists and is not false
// #v-ifdef VITE_MY_ENV
value = 1;
// #v-endif
```

```js
// Allow specified values
// Compile only when 'VITE_MY_ENV' exists and is not equal to hi
// #v-ifdef VITE_MY_ENV!='hi'
value = 1;
// #v-endif
```

## Other

With the better-comments plugin in VsCode, syntax can be highlighted

```json
{
  "tag": "#v",
  "color": "#fff",
  "strikethrough": false,
  "underline": false,
  "backgroundColor": "#0000ff",
  "bold": true,
  "italic": false
}
```

---
_Source: https://npm.io/package/vite-plugin-conditional-compiler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
