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

loaderify

v0.1.5

Published

a browserify transform which allows you to run functions against different required code, like a webpack loader chain would

Downloads

5

Readme

loaderify

A Browserify transform which allows one to add functional loaders on matching require() statements, similar to webpack

    npm install --save-dev loaderify

I wrote this transform after spending hours trying to find a way to inject templates and styles in to my Angular (ng2+) components that ALSO supported minifications, less, sass, emmet, etc. There were transforms for injecting html and css (stringify), but they hung on minification. There were transforms to inject scss/sass (scssify) but they injected <style> tags instead of just strings. Worse yet, the two transforms didn't get along well together, meaning I could choose between un-minified templates and css, or scss with no templates.

I didn't like those options

I'd seen this exact problem solved at work, where we use webpack. In webpack, you specify loaders for files matching certain patterns. These loaders can be functions or chains of functions, which operate on the source file and return a modified source. Seemed like a simple enough thing to do with browserify transforms, so I set about writing this plugin.

##How do I use it?

simply add loaderify in to your bundle process, specifying a list of loaders in the config, like so:

function bundle(){
    return browserify({})
    .add('./entry.js') 
    .transform(loaderify, {
        loaders: [
            {
                Pattern: '**/*.html', 
                Function: injectMinifedHtml
            },
            {
                Pattern: '**/*.scss', 
                Function: injectSass
            },
            {
                Pattern: 'assets/styles/*.css', 
                Function: injectCss
            }
        ]
    })
    .bundle()
    .pipe(gulp.dest('dist/')));
}

The strings in Pattern are standard wildcard matches. ** matches 0 or more directories, and * matches all files in the current directory. So **/*.html matches all html files, while assets/styles/*.css matches top level .css files in the assets/styles directory.

The functions in the Function key are in the format function(filelocation, contents, callback), where filelocation is the location of the required file relative to process.cwd() and contents holds the contents. The body can do whatever it wants to transform those contents, and when they are passed back to the callback, all instances of require('./filename') will be replaced with the results of your transforms!

For conditional transforms, the callback also an abort argument in the form of callback(abort, results). That way, your transforms can choose to leave the require() statement alone.

##What else can it do?

One possible use for the conditional transforms is environment-specific file loads. A transform could be written like so:

function envSwitch(fileloc, contents, callback){
    var env = process.env;
    if(env !== 'LOCAL') {
        return callback(true);
    } else {
        // find the corresponding mock service
        var mockLocation = path.join(path.dirname(fileloc), 'mock.'+path.basename(fileloc));
        // require the mock service instead!
        return callback(null, 'require(\''+mockLocation+'\')');
    }
}

function bundle(){
    return b
    .add('./entry.js')
    .transform(loaderify, {
        loaders: [
            {
                Pattern: 'services/**/*.js', 
                Function: envSwitch
            }
        ]
    })
    .bundle();
}

The possibilities are limitless. Just think how many transforms you can replace with this!