esbuild plugin for conditional compilation using `//#ifdef` comments in JavaScript and TypeScript files. Version 1.0.1 is the latest stable release. It allows developers to include or exclude code blocks based on environment variables before esbuild's tree-shaking phase. Unlike traditional `if (true)` patterns that break function scoping, this plugin removes dead code at the comment level, preserving correct scoping. It filters `process.env` prefixes automatically and supports negation with `!`. The plugin excludes `node_modules` by default for performance and security. Alternative to `esbuild-plugin-conditional` or manual define-based checks, but with a simpler preprocessor-style comment syntax.
npm install esbuild-plugin-ifdefNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to use esbuild-plugin-ifdef with esbuild to conditionally include code based on environment variables.
Ensure `//#ifdef` comments are in top-level or unreachable code only. Do not nest them inside JavaScript conditional statements that esbuild might evaluate.
Use the same keys as your `define` object but without 'process.env.' prefix. For example, if define = { 'process.env.FOO': true }, use `//#ifdef FOO`.Use `//#ifdef !MY_FLAG` instead of `#ifndef MY_FLAG`.
Set `baseDir` to the directory containing your entry points or adjust `exclude` to include the necessary paths.
Always include 'node_modules' in your exclude list if using custom exclude array.
Use dynamic import: `const ifdef = (await import('esbuild-plugin-ifdef')).default;` or switch to ESM `import`.Add `'process.env.MY_FLAG': true` to the define object or use a key that exists.
Set baseDir to an existing directory, e.g., process.cwd() or the project root.
No dependency data recorded yet.