babel-plugin-strip-invariant
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript
Babel plugin to remove non-essential arguments from invariant() calls in production builds. Current stable version is 1.0.0, first release. It solves a common footgun where minifiers don't remove verbose invariant messages. Alternative to manual optimization; offers pragma and argCount options for custom invariant implementations. Lightweight, no runtime dependencies.
Common errors
error SyntaxError: unknown: identifier 'invariant' is not defined ↓
cause The invariant function is not imported or available in scope after stripping arguments.
fix
Install and import the 'invariant' package: npm install invariant and add import invariant from 'invariant';
error Error: .babelrc env.production.plugins[0] must be a string or array ↓
cause Using require() or a non-serializable object in plugins array.
fix
Use string plugin name: 'babel-plugin-strip-invariant'
error Babel 7 compatibility: Plugin/Preset files are not allowed to export objects, only functions ↓
cause Plugin not updated for Babel 7 API.
fix
Use Babel 6 or check if fork exists for Babel 7.
error Module not found: Can't resolve 'babel-plugin-strip-invariant' ↓
cause Package not installed.
fix
Run npm install --save-dev babel-plugin-strip-invariant
Warnings
gotcha Plugin only runs in production environment as defined in .babelrc; development builds are unaffected. ↓
fix Ensure the plugin is placed under the 'production' env block in .babelrc or use BABEL_ENV=production.
gotcha If using a custom pragma, the function must be imported/defined; plugin does not inject anything. ↓
fix Manually import or define the custom function in your code.
gotcha argCount removes trailing arguments; ensure essential arguments are first. ↓
fix Reorder invocation so that arguments to keep are at the beginning.
deprecated Plugin has zero open issues and no updates since 2018; consider active alternatives like babel-plugin-transform-define. ↓
fix Evaluate if plugin still works with your Babel version; may need fork.
Install
npm install babel-plugin-strip-invariant yarn add babel-plugin-strip-invariant pnpm add babel-plugin-strip-invariant Imports
- default wrong
import stripInvariant from 'babel-plugin-strip-invariant'correctmodule.exports = { plugins: ['babel-plugin-strip-invariant'] } - pragma wrong
plugins: [[require('babel-plugin-strip-invariant'), { pragma: 'myFunc' }]]correct['babel-plugin-strip-invariant', { pragma: 'myFunc' }] - argCount wrong
['babel-plugin-strip-invariant', { args: 2 }]correct['babel-plugin-strip-invariant', { argCount: 2 }]
Quickstart
// .babelrc
{
"env": {
"production": {
"plugins": ["babel-plugin-strip-invariant"]
}
}
}
// or babel.config.js
module.exports = {
env: {
production: {
plugins: ['babel-plugin-strip-invariant']
}
}
};
// input
invariant(condition, 'This is a verbose message that will be removed in production');
// output (production build)
invariant(condition);