Coder Social home page Coder Social logo

aliceklipper / babel-plugin-groundskeeper-willie Goto Github PK

View Code? Open in Web Editor NEW

This project forked from betaorbust/babel-plugin-groundskeeper-willie

0.0 2.0 0.0 38 KB

The spiritual successor to groundskeeper. Removes console calls, debuggers, and and pragma-wrapped code.

JavaScript 100.00%

babel-plugin-groundskeeper-willie's Introduction

Groundskeeper Willie Babel Plugin

NPM Version Build Status Dependency Status Dev Dependency Status

This project is a Babel plugin encompassing and expanding the functionality of the very useful Groundskeeper utility. Remaking Groundskeeper's features as a Babel plugin gives ongoing support to ES2015+, while greatly reducing runtime for those already using Babel in their build process.

This plugin will:

  • Remove console statements, so your production code doesn't junk up the console, but your development code can be as verbose as you wish.

    • To keep specific console statements, you can disable Groundskeeper Willie by adding a line disable directive, which makes it significantly more flexible compared to babel-plugin-transform-remove-console

    Source:

    console.log(`I'll be removed.`);
    console.log(`I'll be preserved!`); // groundskeeper-willie-disable-line

    Result:

    console.log(`I'll be preserved!`); // groundskeeper-willie-disable-line
  • Remove debugger statements, so you don't accidentally ship halting code.

    • Same as above, you can disable this in specific cases with a line disable directive.

    Source:

    () => {
      debugger;
      do.something();
      debugger; // groundskeeper-willie-disable-line
    }

    Result:

    () => {
      do.something();
      debugger; // groundskeeper-willie-disable-line
    }
  • Remove code between comment <pragmas>, so you can use your debug code in development, but strip it out in production.

    Source:

    module.exports = {
      prodFunction(){ /* ... */ },
      // <testCode>
      testFunction(){ /* ... */ },
      //</testCode>
      otherProdCode(){ /* ... */ }
    };

    Result:

    module.exports = {
      prodFunction(){ /* ... */ },
      otherProdCode(){ /* ... */ }
    };

Complete Example:

####Input: Here we have a file that has some development code, a console, etc.

module.exports = {
    appCode(){
        console.log(`We're running with scissors!`);
        return [
            'included',
            /*<otherPragma>*/
            'excluded',
            /*</otherPragma>*/
            'also included'];
    }
};

//<debug>
module.exports.debugCode = {
    do.debugging.things.that.should.not.be.in.production();
}
//</debug>

Output:

When run with the Groundskeeper Willie plugin, we'll get back:

module.exports = {
    appCode(){
        return [
            'included',
            'also included'];
    }
};

Why would you do that?

Right now, we (at Netflix) use pragmas so that our developer logging, debug code, and other related exports can live alongside the production code, but, at distribution time, we can ship down the smallest payload, without junking up the console, or sending extra unused code.

What's a pragma?

For the purposes of our little plugin, pragmas are comments that have an opening tag, and a closing tag, with a provided name. The pragma may appear in either a line comment or a block comment, and look like HTML/XML-esq open and close tags.

//<thisIsAPragma>
const removed = code.that.will.be.removed();
//</thisIsAPragma>
return ['included', /* <other> */ 'excluded', /* </other> */ 'also included'];

Plugin Options

The following are the available options with their default values.

{
    "plugins": [
        [
            "groundskeeper-willie",
            {
                "removeConsole":  true,
                "removeDebugger": true,
                "removePragma":   true,
                "pragmaMatcher":  "^\\s?<(\/?)([\\w\\d\\-]*)>\\s*$"
            }
        ]
    ]
}

removeConsole

If you wish to leave in all console statements, set this to false.

removeDebugger

If you wish to leave in all debugger statements, set this to false.

removePragma

If you wish to leave in all pragmas-wrapped code, stet this to false.

pragmaMatcher

May be a regexp string or string array. If it is a regexp string, it should have two capture groups: first determines closing pragma, second โ€” pragma name. If it is a string array, it should contain names of pragmas, which will be removed.

Remove only debug and lol pragmas:

{
    "pragmaMatcher": "^\\s?<(\\/?)(debug|lol)>\\s*$"
}

Exactly the same as before, but as array:

{
    "pragmaMatcher": ["debug", "lol"]
}

babel-plugin-groundskeeper-willie's People

Contributors

betaorbust avatar greenkeeperio-bot avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.