babel-plugin-strip-invariant

    1.0.0 • Public • Published

    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 ⭐️!

    Copyright & License

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

    Install

    npm i babel-plugin-strip-invariant

    DownloadsWeekly Downloads

    402

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    41.6 kB

    Total Files

    12

    Last publish

    Collaborators

    • husscode