babel-plugin-strip-invariant

Babel plugin to remove invariant arguments not needed during production builds.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
babel-plugin-strip-invariant
501.0.05 years ago5 years agoMinified + gzip package size for babel-plugin-strip-invariant in KB

Readme

babel-plugin-strip-invariant
Babel plugin to remove invariant arguments not needed during production builds.
Build Status Coverage Status npm version npm downloads per month

Why is this necessary?

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.

Installation and Setup

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.

Customization

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:

Pragma

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);

ArgCount

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 :star:!

Copyright & License

Copyright (c) 2018 Marcel de Oliveira Coelho under the MIT License. Go Crazy. :rocket: