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

inspect-loader

v2.0.0

Published

Webpack loader designed for loader testing and debugging. Calls a function with the received input.

Downloads

11,261

Readme

npm node npm-stats deps travis appveyor coverage

npm install --save-dev inspect-loader

Put the inspect-loader in front of the loader you want to test and pass in a callback function. The callback function will be called with useful information about the given inputs (arguments). It also exposes the internal loader context for further inspection:

webpack({
    ...
    module: {
        rules: [{
            test: /\.js$/,
            use: [{
                loader: "inspect-loader",
                options: {
                    callback(inspect) {
                         console.log(inspect.arguments);
                         console.log(inspect.context);
                         console.log(inspect.options);
                    }
                }
            }, {
                loader: "my-loader" // loader that you want to test/debug
            }]
        }]
    }
});

The loader returns the received arguments, which means that you can place the inspect-loader in the middle of your loader pipeline. You can even inspect multiple loaders:

webpack({
    ...
            use: [{
                loader: "inspect-loader",
                options: {
                    callback: inspectALoader
                }
            }, {
                loader: "a-loader"
            }, {
                loader: "inspect-loader",
                options: {
                    callback: inspectBLoader
                }
            }, {
                loader: "b-loader"
            }]
    ...
});

Raw

This package exposes also a raw version that can be used to test raw loaders:

webpack({
    ...
    module: {
        rules: [{
            test: /\.js$/,
            use: [{
                loader: "inspect-loader/raw",
                options: {
                    callback(inspect) {
                         console.log(inspect.arguments[0] instanceof Buffer); // true
                    }
                }
            }, {
                loader: "my-raw-loader" // raw loader that you want to test/debug
            }]
        }]
    }
});

callback: Function | string

Can be a Function (preferred) or a string. In case it's a string, it is treated as a string reference and will be invoked on the inspectLoader.callbacks object like this:

const inspectLoader = require("inspect-loader");

inspectLoader.callbacks.myCallback = function () { ... };

webpack({
    ...
                loader: "inspect-loader",
                options: {
                    callback: "myCallback"
                }
    ...
});

The callback passes an inspect object as single argument that exposes the internal loader state:

{
    arguments, // A true array that carries all the input arguments that were passed to the loader
    context, // A reference to the loaderContext of the inspect-loader
    options // A reference to the options object of the inspect-loader
}
function callback(inspect) {
    console.log(inspect.arguments); // ["loader contents from the previous loader"]
    console.log(inspect.context); // { resource: "...", ... }
    console.log(inspect.options); // { callback: [Function] }
}

Please note: context and options are not references to the loaderContext of the loader you want to test. They just expose the internal state of the inspect-loader. This is useful if you have multiple callbacks and you want to find out which resource or loader pipeline has been invoked.

Assertions

Most of the time, you will probably want to do assertions on the inspect object. It is recommended to do this after the webpack compilation has finished, because otherwise the assertion error will be caught by webpack and reported as Module build error.

Not so good:

    ...
    loader: "inspect-loader",
    options: {
        callback(inspect) {
            // assertion errors will be caught as Module build error
            assert.deepEqual(inspect.arguments, [...])
        }
    }
    ...

Better:

let args;

webpack({
    ...
    loader: "inspect-loader",
    options: {
        callback(inspect) {
            args = inspect.arguments;
        }
    }
    ...
}, (err, stats) => {
    ...
    assert.deepEqual(args, [...])
});