A Babel plugin for compile-time replacement of identifiers, member expressions, and typeof statements, similar to Webpack's DefinePlugin. The current stable version is 2.1.4 (released 2024-12-12), with a slow, maintenance-focused release cadence. Unlike Webpack's DefinePlugin which operates at the bundler level, this plugin works at the Babel transpilation stage, making it useful for library authors who want to define compile-time constants without a bundler. Key differentiator: supports replacing typeof expressions (e.g., "typeof window": "object") and member expressions (e.g., "process.env.NODE_ENV": "production"). Notable changes: v2.1.3 avoids replacing object keys to prevent bugs; v2.1.4 also prevents object property replacement. Maintenance status is stable, with no active feature development but continued bug/security fixes.
npm install babel-plugin-transform-defineVerified import paths — ran on the pinned version, not inferred.
Shows basic usage with member expression and identifier replacement, and dynamic config via .babelrc.js.
Upgrade to >=2.1.3. In older versions, avoid defining keys that match identifier replacements.
Upgrade to >=2.1.4. In older versions, object property replacements may occur; wrap replacement values in quotes or use member expression replacements explicitly.
Consider alternatives like @babel/plugin-transform-literals or custom babel macros if you need active feature development.
Only use string, number, or boolean literal values. For complex replacements, use a different approach (e.g., babel macros).
Run: npm install --save-dev babel-plugin-transform-define
Ensure the second argument to the plugin is an object: ['transform-define', { 'KEY': 'value' }]Reorder plugins or avoid defining overlapping replacements. Usually place transform-define early in the plugin list.
Always use strings for keys: { 'process.env.NODE_ENV': 'production' }No dependency data recorded yet.