Babel plugin to remove invariant arguments not needed during production builds.
When using invariant, the actual detailed messages do not get minified away by default as they should. That happens because the minifier doesn't know you won't use the messages anymore.
This plugin solves that by removing all non-essential arguments from invariant calls.
Install the latest version of babel-plugin-strip-invariant:
npm install babel-plugin-strip-invariant --save-dev
Inside your .babelrc, configure this plugin to run only when building for production:
{
"env": {
"production": {
"plugins": ["babel-plugin-strip-invariant"]
}
}
}
That's it! No more invariant messages cluttering production code.
Since invariant implementation is pretty small, maybe you wish to implement it yourself instead of relying on one more dependency. For that use case, you can customize how this plugin works:
You can use a function name other than invariant
.
{
"env": {
"production": {
"plugins": [
["babel-plugin-strip-invariant", { "pragma": "myFunc" }]
]
}
}
}
Than, when babel runs:
// source
myFunc(essentialArg, minifyAway, thisToo);
// after transpilation
myFunc(essentialArg);
You can specify the number of arguments to keep.
{
"env": {
"production": {
"plugins": [
["babel-plugin-strip-invariant", { "argCount": 2 }]
]
}
}
}
Than, when babel runs:
// source
invariant(essentialArg, anotherEssentialArg, minifyAway, thisToo);
// after transpilation
invariant(essentialArg, anotherEssentialArg);
You can mix both options too!
That's it! If you've liked this, consider giving it a star ⭐!
Copyright (c) 2018 Marcel de Oliveira Coelho under the MIT License. Go Crazy. 🚀