{"id":22618,"library":"vite-plugin-conditional-compiler","title":"vite-plugin-conditional-compiler","description":"A Vite plugin for conditional compilation using comments based on environment variables. Current stable version is 0.4.0. It allows developers to include or exclude code blocks at build time by wrapping them in `#v-ifdef`, `#v-elif`, `#v-else`, and `#v-endif` directives that reference Vite environment variables (e.g., `PROD`, `DEV`, custom env vars). Supports logical OR, custom values, and `!=` comparisons. Since v0.2.0, it wraps `unplugin-preprocessor-directives`. Release cadence is moderate with occasional bug fixes and feature releases. Key differentiator: lightweight, no runtime overhead, and works in any file type (JS, CSS, HTML) via comments.","status":"active","version":"0.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/KeJunMao/vite-plugin-conditional-compile","tags":["javascript","vite-plugin","typescript"],"install":[{"cmd":"npm install vite-plugin-conditional-compiler","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-conditional-compiler","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-conditional-compiler","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Underlying library providing the core directive processing (since v0.2.0)","package":"unplugin-preprocessor-directives","optional":false}],"imports":[{"note":"Default import is required; named export is not available.","wrong":"import { ConditionalCompile } from 'vite-plugin-conditional-compiler'","symbol":"default","correct":"import ConditionalCompile from 'vite-plugin-conditional-compiler'"},{"note":"CommonJS require works as the package provides a default export.","wrong":null,"symbol":"ConditionalCompile","correct":"const ConditionalCompile = require('vite-plugin-conditional-compiler')"},{"note":"Type-only import for TypeScript users.","wrong":null,"symbol":"Options","correct":"import type { Options } from 'vite-plugin-conditional-compiler'"}],"quickstart":{"code":"// vite.config.ts\nimport { defineConfig } from 'vite';\nimport ConditionalCompile from 'vite-plugin-conditional-compiler';\n\nexport default defineConfig({\n  plugins: [ConditionalCompile()],\n});\n\n// In your source file (e.g., main.ts):\n// #v-ifdef VITE_MY_ENV\nconst myValue = 'This will only compile if VITE_MY_ENV is set and truthy';\n// #v-endif\n\n// With negation:\n// #v-ifdef !VITE_DEBUG\nconsole.log('This will compile when VITE_DEBUG is not set or falsy');\n// #v-endif\n\n// Using custom values with !=:\n// #v-ifdef VITE_MODE!='development'\nconsole.log('Not in development mode');\n// #v-endif","lang":"typescript","description":"Setup the plugin in vite.config.ts and use conditional compilation directives in source files."},"warnings":[{"fix":"Update to v0.2.0 or later; no changes required in usage syntax.","message":"From v0.2.0, the plugin wraps unplugin-preprocessor-directives; the internal implementation changed significantly.","severity":"breaking","affected_versions":">=0.2.0"},{"fix":"Replace `#v-ifndef ENV` with `#v-ifdef !ENV`.","message":"The `#v-ifndef` directive is deprecated since v0.2.0. Use `#v-ifdef !ENV` instead.","severity":"deprecated","affected_versions":">=0.2.0"},{"fix":"Use multiple `#v-ifdef` blocks or restructure conditionally compiled code to avoid AND logic.","message":"Only logical OR (`||`) is supported; logical AND (`&&`) is not supported and may cause unexpected behavior.","severity":"gotcha","affected_versions":"*"},{"fix":"Explicitly set `include` and `exclude` options in plugin configuration to limit processing to relevant files.","message":"The plugin processes all files by default (`include: ['**/*']`). Large projects may experience slower build times if no exclude patterns are set.","severity":"gotcha","affected_versions":"*"},{"fix":"Upgrade to v0.1.0+ if you need `!=` comparisons.","message":"In v0.1.0, support for `!=` was added. Previously, only equality checks (implicitly) were possible.","severity":"breaking","affected_versions":"<0.1.0"},{"fix":"Ensure the variable is defined in Vite's `define` or `envPrefix` (default `VITE_`).","message":"The environment variable must be a Vite environment variable (e.g., `PROD`, `VITE_*`). Variables not exposed by Vite may not work.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Use `#v-ifdef !ENV` instead of `#v-ifndef ENV`.","cause":"Using `#v-ifndef` directive which is deprecated and may cause parsing issues in some versions.","error":"SyntaxError: Unexpected token '!'"},{"fix":"Run `npm install -D vite-plugin-conditional-compiler` and use correct import (default import).","cause":"Package not installed or import path incorrect.","error":"Cannot find module 'vite-plugin-conditional-compiler'"},{"fix":"Ensure directives are exactly `#v-ifdef`, `#v-elif`, `#v-else`, `#v-endif` and environment variables match Vite's (e.g., `PROD`, `DEV`, `VITE_*`).","cause":"Directive syntax or environment variable name is incorrect.","error":"The plugin does not seem to be working; no compilation happens."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}