A conditional compilation plugin for Rollup and Vite, allowing you to include or exclude code blocks based on build-time flags or environment variables. Supports JavaScript, TypeScript, CSS, SCSS, Vue, and React files. v1.1.1 is the current stable version, released in 2023 and last updated in 2023. It uses a comment-based syntax (e.g., @ifdef and @ifndef) similar to C preprocessor directives, making it easy to toggle debug code, platform-specific features, or feature flags without modifying the source. Unlike alternatives that rely on environment variable replacement or dead-code elimination, this plugin performs actual code removal at build time, ensuring zero runtime overhead.
npm install rollup-plugin-ifdefVerified import paths — ran on the pinned version, not inferred.
Shows Vite setup with environment variable DEBUG; conditionally compile code blocks based on NODE_ENV.
Always wrap boolean flags in quotes: 'true' vs 'false' (or use 'defined' check).
Place conditional code inside /* @ifdef */ comments, not inside JavaScript strings or expressions.
Update comments from //#ifdef to /* @ifdef */ or use the new syntax. Refer to migration guide.
Avoid nesting; use logical operators in the condition if needed (e.g., DEBUG && FEATURE_X).
Run npm install rollup-plugin-ifdef --save-dev
Use default import: import ifdef from 'rollup-plugin-ifdef'
Ensure plugin is only included once in plugins array. If using with other plugins, set 'order' or 'enforce' to 'pre'.
Use correct syntax: /* @ifdef FLAG_NAME */ or /* @ifndef FLAG_NAME */