{"id":22065,"library":"rollup-plugin-ifdef","title":"rollup-plugin-ifdef","description":"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.","status":"active","version":"1.1.1","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","rollup","vite","ifdebug","conditional","compile","typescript"],"install":[{"cmd":"npm install rollup-plugin-ifdef","lang":"bash","label":"npm"},{"cmd":"yarn add rollup-plugin-ifdef","lang":"bash","label":"yarn"},{"cmd":"pnpm add rollup-plugin-ifdef","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core peer dependency; plugin is designed to work as a Rollup plugin.","package":"rollup","optional":false},{"reason":"Used for filtering files by pattern; automatically required at runtime.","package":"@rollup/pluginutils","optional":false},{"reason":"Used for source map manipulation during code transformation.","package":"magic-string","optional":false}],"imports":[{"note":"Package is ESM-only; CommonJS require() will fail with Rollup/Vite. Use import.","wrong":"const ifdef = require('rollup-plugin-ifdef')","symbol":"default","correct":"import ifdef from 'rollup-plugin-ifdef'"},{"note":"The plugin is exported as default, not a named export. Named import will be undefined.","wrong":"import { ifdef } from 'rollup-plugin-ifdef'","symbol":"ifdef","correct":"import ifdef from 'rollup-plugin-ifdef'"},{"note":"No separate Vite plugin; use the same module in vite.config.js under plugins array.","wrong":"import ifdef from 'rollup-plugin-ifdef/vite'","symbol":"ifdef (Vite)","correct":"import ifdef from 'rollup-plugin-ifdef' // in vite.config.js"}],"quickstart":{"code":"// vite.config.js\nimport { defineConfig } from 'vite';\nimport ifdef from 'rollup-plugin-ifdef';\n\nexport default defineConfig({\n  plugins: [\n    ifdef({\n      DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',\n    }),\n  ],\n});\n\n// src/main.js\n// @ifdef DEBUG\nconsole.log('Debug mode');\n// @endif\n\n// @ifndef DEBUG\nconsole.log('Production mode');\n// @endif\n","lang":"javascript","description":"Shows Vite setup with environment variable DEBUG; conditionally compile code blocks based on NODE_ENV."},"warnings":[{"fix":"Always wrap boolean flags in quotes: 'true' vs 'false' (or use 'defined' check).","message":"Conditions are string comparisons; boolean values from process.env are strings, so flags must be set as strings 'true'/'false' not boolean true/false.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Place conditional code inside /* @ifdef */ comments, not inside JavaScript strings or expressions.","message":"The plugin uses comment-based syntax; it will not work with dynamic expressions or template literals. Code inside conditionals must be statically analyzable.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Update comments from //#ifdef to /* @ifdef */ or use the new syntax. Refer to migration guide.","message":"Version 1.x uses a different syntax than 0.x; @ifdef/@ifndef instead of //#ifdef. Existing code from 0.x will not be recognized.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Avoid nesting; use logical operators in the condition if needed (e.g., DEBUG && FEATURE_X).","message":"Nested conditionals are not supported; having @ifdef inside another @ifdef block may lead to incorrect compilation.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Run npm install rollup-plugin-ifdef --save-dev","cause":"Package not installed or not in node_modules.","error":"Cannot find module 'rollup-plugin-ifdef'"},{"fix":"Use default import: import ifdef from 'rollup-plugin-ifdef'","cause":"Named import instead of default import: import { ifdef } from 'rollup-plugin-ifdef'","error":"'ifdef' is not a function"},{"fix":"Ensure plugin is only included once in plugins array. If using with other plugins, set 'order' or 'enforce' to 'pre'.","cause":"Plugin is applied multiple times or incorrectly configured to process its own output.","error":"Potential infinite recursion: found plugin 'ifdef' for file '...'"},{"fix":"Use correct syntax: /* @ifdef FLAG_NAME */ or /* @ifndef FLAG_NAME */","cause":"Syntax error in comment: missing condition or invalid characters (e.g., using == instead of plain string).","error":"Error: @ifdef expected a valid condition"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}