Registry / devops / vite-plugin-conditional-compile

vite-plugin-conditional-compile

JSON →
library1.4.5jsnpmunverified

A Vite plugin that implements conditional compilation using preprocessor-like directives (#if, #elif, #else, #endif), similar to C/C++ #ifdef. It allows selective inclusion/exclusion of code blocks based on environment variables, custom variables, and logical expressions. Current stable version is 1.4.5, released on an irregular cadence. Key differentiators: supports JSX, Vue templates, and arbitrary file types; integrates with Vite's loadEnv; enables flexible condition syntax with logical operators and nested expressions. Requires Vite ^4.5.0 as a peer dependency. Ships TypeScript types.

npm install vite-plugin-conditional-compile
INSTALL
IMPORT
SIG · VITE-PLUGIN-CONDIT
V
vite-plugin-conditional-compile
devopsjavascriptv1.4.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import vitePluginConditionalCompile from 'vite-plugin-conditional-compile'
const vitePluginConditionalCompile = require('vite-plugin-conditional-compile')
ESM default export. CommonJS require also works, but the default export is preferred in ESM contexts.
vitePluginConditionalCompile
import vitePluginConditionalCompile from 'vite-plugin-conditional-compile'
import { vitePluginConditionalCompile } from 'vite-plugin-conditional-compile'
Not a named export; this is the default export with that name.
ViteConditionalCompileOptions
import type { ViteConditionalCompileOptions } from 'vite-plugin-conditional-compile'
TypeScript type export for the plugin options. Use with 'import type' for correct TypeScript usage.

Configures vite-plugin-conditional-compile with custom env variables and shows how to use directives in JSX.

// vite.config.ts import { defineConfig } from 'vite'; import vitePluginConditionalCompile from 'vite-plugin-conditional-compile'; export default defineConfig({ plugins: [ vitePluginConditionalCompile({ env: { DEBUG: true, FEATURE_A: 'enabled' } }) ] }); // src/main.jsx // #if [DEBUG] console.log('Debug mode'); // #endif // #if [FEATURE_A=enabled] console.log('Feature A is enabled'); // #endif
Debug
Known issues
breakingThe transform function, previously synchronous, is now async for source map support.
fix
If you were relying on synchronous behavior, update your checks to handle async. No code changes needed unless you are extending the plugin.
affects: >=1.4.5
gotchaPlugin must be placed before other plugins (especially JSX compilers) in the plugins array, otherwise conditions may not be parsed.
fix
Ensure vitePluginConditionalCompile is the first plugin in the array.
affects: >=1.0.0
gotchaCondition expressions must be enclosed in square brackets [] and cannot use angle brackets or other delimiters.
fix
Always wrap conditions in [] (e.g., #if [DEV] not #if DEV).
affects: >=1.0.0
deprecatedThe 1.3.x version had a different API but is still referenced in README. The current 1.4.x API uses env and include/exclude options.
fix
Upgrade to 1.4.5 and use the new configuration format as shown in the README.
affects: 1.3.x
gotchaThe plugin does not support nested #if directives inside other #if blocks; only one level of condition is allowed.
fix
Flatten your conditions or use logical operators (e.g., #if [A && B]).
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token '#' while parsing JSX
The plugin is placed after JSX transformer (e.g., @vitejs/plugin-react) and cannot parse the # syntax before JSX is transformed.
fix
Move vitePluginConditionalCompile to the first position in the plugins array.
Cannot find module '...' or its corresponding type declarations.
Using CommonJS require() instead of ESM import – the package exports a default export, not a CommonJS module.exports.
fix
Use import vitePluginConditionalCompile from 'vite-plugin-conditional-compile'.
TypeError: vitePluginConditionalCompile is not a function
Using named import { vitePluginConditionalCompile } instead of default import.
fix
Use import vitePluginConditionalCompile from 'vite-plugin-conditional-compile'.
ENOENT: no such file or directory, open '.../node_modules/vite-plugin-conditional-compile/package.json'
The package was not installed because Vite peer dependency was missing or version mismatch.
fix
Run 'npm install vite@^4.5.0' and then 'npm install vite-plugin-conditional-compile'.
Unset env variable: DEV is not defined
The condition references an env variable that is not set in either plugin env or Vite's loadEnv.
fix
Define the variable in the plugin's env option or ensure it is present in Vite's environment variables.
Upgrade
Version history
1.4.5latest on npm
Audit
Dependencies
viterequiredpeer dependency: requires Vite >=4.5.0 to function as a Vite plugin
Agent activity
4 hits · last 30 days
node
4
Resources
vite-plugin-conditional-compile — npm install vite-plugin-conditional-compile · libregistry