npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-groundskeeper-willie

v2.0.0

Published

Groundskeeper-style babel plugin to remove console calls and pragma contents.

Downloads

226

Readme

Groundskeeper Willie Babel Plugin

NPM Version

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
			}
		]
	]
}

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.