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-compileVerified import paths — ran on the pinned version, not inferred.
Configures vite-plugin-conditional-compile with custom env variables and shows how to use directives in JSX.
If you were relying on synchronous behavior, update your checks to handle async. No code changes needed unless you are extending the plugin.
Ensure vitePluginConditionalCompile is the first plugin in the array.
Always wrap conditions in [] (e.g., #if [DEV] not #if DEV).
Upgrade to 1.4.5 and use the new configuration format as shown in the README.
Flatten your conditions or use logical operators (e.g., #if [A && B]).
Move vitePluginConditionalCompile to the first position in the plugins array.
Use import vitePluginConditionalCompile from 'vite-plugin-conditional-compile'.
Use import vitePluginConditionalCompile from 'vite-plugin-conditional-compile'.
Run 'npm install vite@^4.5.0' and then 'npm install vite-plugin-conditional-compile'.
Define the variable in the plugin's env option or ensure it is present in Vite's environment variables.