babel-plugin-dev-expression
raw JSON → 0.2.3 verified Sat Apr 25 auth: no javascript
A Babel plugin that mirrors Facebook's dev-expression transform, reducing or eliminating development checks (invariant, warning, __DEV__) from production code. Stable version 0.2.3, low release cadence. It replaces __DEV__ with process.env.NODE_ENV !== 'production', optimizes invariant and warning calls by wrapping them with environment checks. Differentiates from manual conditional code by automating transforms and handling edge cases like stringification.
Common errors
error Cannot find module 'babel-plugin-dev-expression' ↓
cause Not installed or misconfigured in pnpm / yarn PnP.
fix
Run npm install --save-dev babel-plugin-dev-expression or add to package.json.
error Error: .plugins[0][0] must be a string, object, function ↓
cause Using import or ES module syntax for the plugin in a CommonJS config file.
fix
Use require('babel-plugin-dev-expression') or string shorthand 'dev-expression'.
error Invariant failed: Minified React error #31 ↓
cause invariant was stripped incorrectly because the plugin didn't run (NODE_ENV=test or misconfiguration).
fix
Ensure babel-plugin-dev-expression is in the plugins list and NODE_ENV is set to 'production'.
Warnings
gotcha The transform does NOT run when NODE_ENV is 'test'. You must define __DEV__ as a global in your test environment. ↓
fix In Jest: setupFiles or jest.globals; in Mocha: global.__DEV__ = true;
breaking v0.2.0 dropped Babel 5 support and required Babel 6+. If you are on Babel 5, pin to v0.1.0. ↓
fix Upgrade to Babel 6/7 or stay on babel-plugin-dev-expression@0.1.x.
deprecated This package is a mirror of Facebook's plugin and may not receive updates. Consider using @babel/plugin-transform-prod-expressions or manual optimization. ↓
fix Evaluate alternatives; the plugin is stable but unmaintained.
gotcha The plugin replaces __DEV__ even in non-production builds if NODE_ENV is set. If you rely on __DEV__ being a global truthy constant, the transform will break it. ↓
fix Ensure NODE_ENV is properly set to 'production' for builds, or disable the plugin in development.
Install
npm install babel-plugin-dev-expression yarn add babel-plugin-dev-expression pnpm add babel-plugin-dev-expression Imports
- default export wrong
import plugin from 'babel-plugin-dev-expression'correctmodule.exports = require('babel-plugin-dev-expression') - devExpression wrong
import { devExpression } from 'babel-plugin-dev-expression'correctconst devExpression = require('babel-plugin-dev-expression') - Babel config usage wrong
plugins: [['babel-plugin-dev-expression', { /* options */ }]]correctplugins: ['dev-expression']
Quickstart
// babel.config.js
module.exports = {
plugins: [
'babel-plugin-dev-expression'
]
};
// Before (source.js):
const __DEV__ = true;
invariant(condition, 'msg %s', arg);
warning(condition, 'msg %s', arg);
// After (production build):
process.env.NODE_ENV !== 'production';
if (!condition) {
if ("production" !== process.env.NODE_ENV) {
invariant(false, 'msg %s', arg);
} else {
invariant(false);
}
}
if ("production" !== process.env.NODE_ENV) {
warning(condition, 'msg %s', arg);
}