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

require2

v0.0.0-draft.1

Published

Next-gen require() functionality for NodeJS

Downloads

4

Readme

require2

IMPORTANT!!! This package is currently in being drafted. All suggestions to how it should work are welcomed! No implementation is provided for now until some parts of the interface are agreed upon.

Build Status Coverage Status Dependency Status devDependency Status

Develop Status Develop Coverage Status

Next-gen require() functionality for NodeJS.
This includes requireing multiple files at once using glob patterns and custom strategies for requireing.

Installation

require2 on NPM

Usage

var require2, includes, templates;
require2 = require('require2').bind(global);

// the following
includes = require2('./includes/*.js');
// is equivalent to
includes = [require('./includes/include1.js'), require('./includes/include2.js')];

// and the following
templates = require2('./templates/*.hbs', {
              strategy: 'object',
              keys: ['path-reduce', 'strip-extension']
            });
// is equivalent to
templates = {
              'template1': require('./templates/template1.hbs'),
              'template2': require('./templates/template2.hbs')
            };

Interface of require2(pattern [, options])

Pattern must be a string and can be a traditional module identifier (same as original require) or a glob pattern.
The second parameter to require must be an object and supports the following keys:

strategy [optional, default: 'array']

The strategy parameter can be provided in two formats:

  • 'name-of-strategy'
  • ['name-of-strategy', ...options-for-strategy]

Possible values are

  • 'array': requires all matched files in order and returns them as an array.

  • 'map': returns an object.

Every matched file is represented by a key and it's respective require call as the value. The keys are generated by the option of the same name as the strategy.

  • function(base, files, config) { return /* whatever you want */; }

You can provide a custom function. It takes the parameters base (the path of the file that is being transformed), files (an array that contains all the files matched by the glob, sorted on their full path) and config (the config object passed to the require call in the source file). If you believe your custom function is of general purpose to others, you can add a pull request for it.

  • You can "globally" extend the require2 strategies by assigning to require('require2').strategies
require('require2/strategies')['my-custom-strategy'] = function(base, files, config) {
    return /* my-awesome-output */;
};

// from now on, you can do the following from any file
  require2('./my/glob/**/*', {strategy: 'my-custom-strategy'});

If you believe your strategy could benefit a lot of people, you can submit a pull request to have your strategy added to the main package or start an issue.

map [optional, default:["path-reduce", "strip-extension"]]

The list of functions to determine the key of a matched file when strategy: "map" is used. You can provide a single value, but you can also provide an array of these values. In the case an array is provided, the functions are executed in order to determine the final key.

Possible values are

  • 'path'

Every file uses it's relative path (as returned by node-glob) as its key.

  • 'strip-ext'

Every file has it's extension removed, but only if it would not cause a duplicate key.

  • 'path-reduce'

Strips the common path from all matches.

  • 'reduce-prefix'

Tries to reduce the key by determining the largest common prefix of each match and removing them from the matched filename.

  • 'reduce-postfix'

Tries to reduce the key by determining the largest common postfix of each match and removing them from the matched filename.

  • 'reduce'

Combination of reduce-prefix and reduce-postfix.

  • function(base, files, config) { return {"/full/path/to/file": "key"} }

You can provide a custom function. It takes the parameters base (the path of the file that is being transformed), files (an object that contains all the files matched by the glob, using their full path as keys) and config (the config object passed to the require call in the source file).

  • You can "globally" extend the require2 key resolvers by assigning to require('require2').keys
require('require2/keys')['my-custom-key'] = function(base, keyMap, config) {
    var i = 0, newKeyMap = {};
    for (file in keyMap) {
      newKeyMap[file] = '' + (i++); // just use numbers as keys
    }
    return newKeyMap;
};

// from now on, you can do the following from any file
  require2('./my/glob/**/*', {strategy: 'my-custom-strategy'});

If you believe your custom function is of general purpose to others, you can add a pull request for it.

  • You are very welcome to suggest other options by starting an issue!.

glob [optional, default:{}]

This allows options to be provided to node-glob, which is used internally to find matching files.

Credits

Original concept based on require-globify.

License

MIT

Changelog

  • 0.0.0-draft.* : First proposal of interface.