{"id":19034,"library":"babel-plugin-conditional-compile","title":"babel-plugin-conditional-compile","description":"A Babel plugin that performs conditional compilation by evaluating if statements with predefined constants and removing dead code. Version 0.0.5 is the latest stable release, updated infrequently (last release in 2016). It allows developers to define compile-time constants (e.g., IS_DEV, CODE_FOR_IE) and automatically eliminate unreachable branches during bundling, reducing bundle size. Unlike webpack's DefinePlugin or similar tools, this operates at the AST level within Babel transformation pipeline. Supports dropping debugger statements and arbitrary constant replacement. Lacks TypeScript definitions and ES module support; designed for legacy Babel 6.x.","status":"maintenance","version":"0.0.5","language":"javascript","source_language":"en","source_url":"https://github.com/brianZeng/babel-plugin-conditional-compile","tags":["javascript","babel","compile","bebel-plugin"],"install":[{"cmd":"npm install babel-plugin-conditional-compile","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-conditional-compile","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-conditional-compile","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Requires Babel 6.x runtime to function as a plugin.","package":"babel-core","optional":false}],"imports":[{"note":"This is a Babel plugin, not a module to import directly. Add it to plugins array in .babelrc or babel.transform options.","wrong":"import conditionalCompile from 'babel-plugin-conditional-compile'","symbol":"default","correct":"// No import needed - plugin is specified in Babel config"},{"note":"This plugin targets Babel 6.x, where the package is 'babel-core' (not '@babel/core'). Use require('babel-core') for compatibility.","wrong":"import { transform } from 'babel/core'; // wrong because babel-core was the v6 package","symbol":"babel-core","correct":"const babel = require('babel-core');"},{"note":"Without passing an options object, no defines will be set and the plugin becomes a no-op.","wrong":"plugins: ['conditional-compile'] // no options object","symbol":"PluginConfig","correct":"// Options are passed as second element in plugin array:\nplugins: [['conditional-compile', { define: { IS_DEV: true } }]]"}],"quickstart":{"code":"const babel = require('babel-core');\n\nconst code = `\nif (IS_DEV) {\n  console.log('debug info');\n}\nvar foo;\nif (CODE_FOR_IE) {\n  foo = 1;\n} else if (CODE_FOR_CHROME) {\n  foo = 2;\n}\n`;\n\nconst result = babel.transform(code, {\n  plugins: [['babel-plugin-conditional-compile', {\n    define: {\n      IS_DEV: false,\n      CODE_FOR_CHROME: true\n    },\n    dropDebugger: false\n  }]]\n});\n\nconsole.log(result.code);\n// Output:\n// var foo;\n// foo = 2;","lang":"javascript","description":"Demonstrates using Babel 6 with the plugin to remove dead code based on compile-time constants. Sets IS_DEV to false and CODE_FOR_CHROME to true, removing the IE branch and debug log."},"warnings":[{"fix":"Use @babel/plugin-transform-conditional-compile or other modern alternatives.","message":"Package is unmaintained since 2016 and only supports Babel 6.x (babel-core), not Babel 7+ (@babel/core).","severity":"deprecated","affected_versions":">=0.0.1"},{"fix":"Ensure your Babel preset is set to transform ES modules to CommonJS, or use a different plugin that supports ESM.","message":"The plugin does not support ES module syntax (import/export); works only with CommonJS require.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Do not attempt to require the plugin yourself. Instead, specify it in .babelrc or babel.transform plugins array as a string.","message":"Incorrect use of 'require('babel-plugin-conditional-compile')' at runtime will fail because the plugin is not designed to be called directly.","severity":"breaking","affected_versions":">=0.0.0"},{"fix":"Always pass an object with define property, even if empty: plugins: [['conditional-compile', {}]]","message":"Options object must be provided as second array element; omitting it causes all defines to be undefined and no code removal occurs.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Install 'babel-core' (legacy): npm install babel-core@6 --save-dev, or migrate to a plugin compatible with @babel/core.","cause":"Attempting to use the plugin with Babel 7+ where '@babel/core' is the package name, not 'babel-core'.","error":"Error: Cannot find module 'babel-core'"},{"fix":"Ensure the plugin is listed in .babelrc. Example: { plugins: [['conditional-compile', { define: { IS_DEV: true } }]] }","cause":"The plugin is not configured or is not transforming the code because the define option is missing or misspelled.","error":"ReferenceError: IS_DEV is not defined"},{"fix":"Reference the plugin by its package name string in the plugins array: ['conditional-compile', options]","cause":"Plugin is not properly installed or is being used as a function instead of a string in Babel config.","error":"TypeError: Cannot read property 'visitor' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}