node package manager

require-globify

require-globify

Build Status Build Status Dependency Status devDependency Status

Transform for browserify, which allows to require files with globbing expressions.

Installation

require-globify

Usage

browserify -t require-globify entry.js > bundle.js

Example

The transform is triggered by adding an additional parameter to the classic require() call.

// just expand to multiple require calls, one for each matched file 
require('./includes/*.js', {mode: 'expand'});
 
// return an object that maps each matched path to it's require() call 
var hash = require('./includes/*.js', {mode: 'hash'});

Interface

The second parameter to require must be an object and supports the following keys:

mode [required]

Possible values are

  • 'expand': replaces the call with multiple calls, one for each match.

This replaces the option glob: true in <1.2.0.

  • 'hash': replaces the call with 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 resolver option. This replaces the option hash: true in <1.2.0.

  • 'list': replaces the call with an array.

Every matched file is represented by an object that contains two properties: + name which represents it's key. The keys are generated by the resolver option. + module which contains it's respective require call as the value.

  • function(base, files, config) { return "require(...)" }

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 are very welcome to suggest other options by starting an issue!.

E.g. 'list' to return the require calls wrapped in an array, if you'd find a use-case for it :p

resolve [optional, default:["path-reduce", "strip-ext"]]

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

Possible values are

  • 'path'

Every file uses it's relative path as its key. The relative path will always begin with either ../ or ./. On Windows, paths will be written unix-style (so \ will be replaced by /). This replaces the options hash: "path", ext: true in <1.2.0.

  • 'strip-ext'

Every file has it's extension removed, but only if it would not cause a duplicate key. This replaces the options ext: false in <1.2.0.

  • 'path-reduce'

Strips the common path from all matches. In most cases this will replace the options hash: true, ext: false in <1.2.0.

  • '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 {"path": "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). 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!.

options [optional, default:{}]

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

ext [deprecated, optional, default:false]

This option is replaced by resolve: 'strip-ext', but remains supported until version 2.0.0. WARNING: Backwards compatibility is not available in combination with the newer "resolve" option.

glob [deprecated]

This option is replaced by mode: 'expand', but remains supported until version 2.0.0

hash [deprecated]

This option is replaced by mode: 'hash' and resolve: ['path', 'strip-ext'], but remains supported until version 2.0.0

Credits

Original concept from Jiří špác, completely reimplemented by Adriaan Callaerts(@call-a3).
Hashing with paths implemented by Pat Collins(@patcoll).

License

MIT

Changelog

  • 1.2.1:
    • Fixed bugs #12, #13 and #14
    • Streamlined badges on README
  • 1.2.0: Complete overhaul of architecture, adding new features such as pass-through options for node-glob and multiple bugfixes.
  • 1.1.0: Added hashing with path.
  • 1.0.*: Bugfixes.
  • 1.0.0: Rewrite based on browserify-transform-tools.
  • 0.* : Base implementation by Jiří špác(@capaj).