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

modules-loader

v0.1.0

Published

Loads module according to defined rules.

Downloads

2

Readme

modules-loader

What it's for

Loads modules according to defined rules.

Usage

var loader = require( 'modules-loader' ),
    path = require( 'path' );

loader.load( path.join( __dirname, 'foo', function( modules ) {
    console.log( modules );
});

Documentation

This modules provides the load method to load the modules, but also the rules object to define the rules.

Rules

Some default rules already exist, you can find what they are with:

console.log( loader.rules );

{
   'isJavascript': [Function],
   'isNotGit': [Function],
   'isNotNodeModule': [Function]
}

They're quite explicit :-). The first requires javascript files only, the second requires files that are not in .git/ folders and the third requires files that are not in node_modules/ folders.

Writing such a rule is easy. Let's take a look at an example:

function isJavascript( files ) {
    // The files argument is an array with the list of file paths.
    // Absolute file paths.
    return files.filter( function( file ) {

        // Only return the files ending in .js
        return file.slice( -3 ) === '.js';
    });
}

Adding your own rule is done this way:

var loader = require( 'modules-loader' );

loader.rules.isCustomRule = function( files ) {
    // Your filtering code there.
};

Deleting an existing rule can be done this way:

delete loader.rules.isJavascript;

The load method

The load method requires two arguments:

  1. The absolute path to the folder in which to load the modules.
  2. A callback.

Getting the absolute path of a folder isn't so hard, here is an example:

var path = require( 'path' );

console.log( path.join( __dirname, 'foo' ) );

"/absolute/path/to/foo"

The callback gets the modules argument, an object with all the modules loaded.

Here is an example with the following folder structure:

foo/
    bar/
        bar.js
    baz/
        baz.js
    bar.js
    baz.js

loader.load( folder, function( modules ) {
    console.log( modules );
});

{
    "barBar.js": [Object object],
    "bazBaz.js": [Object object],
    "bar.js": [Object object],
    "baz.js": [Object object]
}

Such a naming convention is chosen for the following reasons:

  • To avoid names conflicts, the folder is added to the property name.
  • Because you might load non-js files, the extension is kept.

Contributors

License

MIT License.