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

webpack-remove-code-blocks

v0.1.6

Published

A webpack plugin that removes marked blocks of code from any code processed by webpack.

Downloads

2,760

Readme

Webpack Remove Code Blocks test workflow

This Webpack Remove Code Blocks package was originated from Webpack Remove Block.

This is a webpack loader that can remove blocks of code marked with special labels in comments. It can be incorporated into the build process to remove the code that you don't want to see in production. The loader supports multiple block types.

The key difference from the original loader is that the syntax is not limited only with :start and :end markers.

Usage example

Let's start with a simple usage example. For example, we want to remove some code from a bundle while we build a project. To do that, we need to take a few simple steps.

Firstly, we need to add the loader and some additional settings to our webpack configuration:

module.exports = {
    module: {
        rules: [
            {
                test: /\.js$/,                                              // files we want to procces
                exclude: /(node_modules|bower_components|\.spec\.js)/,      // files we want to exclude
                use: [
                    {
                        loader: 'webpack-remove-code-blocks',               // use the loader
                    },
                ],
            },
        ],
    },
};

Then, we can mark unwanted blocks of code in our .js files using comments with the special syntax devblock:start and devblock:end:

/* devblock:start */
console.log('something not for production');
/* devblock:end */

After the bundling process, the marked blocks will be removed (the comments will be removed too).

Advanced usage example

Let's suppose, that we have a more sophisticated task. We want to use different labels (we might want to keep some code in staging, but not in the production environment) and process different file extensions. That's not a problem.

The only thing we need to do is to provide some additional settings to our webpack configuration:

module.exports = {
    module: {
        rules: [
            {
                test: /\.js|\.ts|\.tsx$/,                                   // files we want to procces
                exclude: /(node_modules|bower_components|\.spec\.js)/,      // files we want to exclude
                use: [
                    {
                        loader: 'webpack-remove-code-blocks',               // use the loader
                        options: {
                            blocks: [                                       // define three different blocks
                                'debug',
                                'devblock',
                                {
                                    start: 'devblock_start',
                                    end: 'devblock_end',
                                    prefix: '/*',
                                    suffix: '*/',
                                },
                            ],
                        },
                    },
                ],
            },
        ],
    },
};

Let's now build our project with this code inside:

/* debug:start */
console.log('debug');
/* debug:end */
var makeFoo = function(bar, baz) {
    // The following code will be removed with the loader
    /* devblock:start */
    if (bar instanceof Bar !== true) {
        throw new Error('makeFoo: bar param is required and must be instance of Bar');
    }
    /* devblock:end */
    /* devblock_start */
    if (baz instanceof Baz !== true) {
        throw new Error('makeFoo: baz param is required and must be instance of Baz');
    }
    /* devblock_end */

    // This code will remain
    return new Foo(bar, baz);
}

After the bundling process, the result will be as follows:

var makeFoo = function(bar, baz) {
    // The following code will be removed with the loader

    // This code will remain
    return new Foo(bar, baz);
}

Options

If you want to define different comment blocks, use the options.blocks array. Each element of the array describes a unique block of comments to be removed. The block can be described by an object with the following properties:

start: 'dev_start',             # a string value that defines the beginning of a block to remove
end: 'dev_end',                 # a string value that defines the end of a block to remove
prefix: '/*',                   # a string value that defines the beginning of a comment
suffix: '*/',                   # a string value that defines the end of a comment

Or, if you don't want to clutter your configuration, a block can be described by just a simple string. The string debug will represent a block of comments with the following properties:

start: 'debug:start',
end: 'debug:end',
prefix: '/*',
suffix: '*/',

License

The MIT License (MIT). Please see License File for more information.