Package Exports
- require-dir-all
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (require-dir-all) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
require-dir-all
Yet another Node.js helper to require
all files in directory
Link to package page in npm repository
Inspired by require-all and require-dir packages.
!!! WARNING: the package is in alpha state, it may be unstable and it may change its API !!!
Installation
npm install require-dir-all --save
Usage
Basic usage
var modules = require('require-dir-all')('directory_to_require');
Afterwards variable modules
will contain exported values from all the files in directory accessible as its properties, for
example modules.module1
You may provide additional options using second optional parameter:
var modules = require('require-dir-all')(
'directory_to_require', // directory
{ // options
map: function( ) { /* you may postprocess the name of property the module will be stored and exported object */ }
recursive: false, // recursively go through subdirectories; default: false
excludeDir: /^(\.(git|svn)|(node_modules))$/ // default value - reg exp to exclude subdirectories
}
);
Options:
map
: function to postprocess eachrequire
'd file (see example below); default:null
recursive
- recursively go through subdirectories; default:false
excludeDir
- reg exp to exclude subdirectories, default:/^(\.(git|svn)|(node_modules))$/
Simple
If you need to require all the .js
, .json
, .coffee
files in the directory modules
, add following line:
var modules = require('require-dir-all')('modules');
or if you like:
var require_dir_all = require('require-dir-all');
var modules = require_dir_all('modules');
Object modules
will be populated with properties which names will correspond to module names and values - to exported
objects.
modules = {
module1: require('module1')
module2: require('module2')
}
By default directories .git
, .svn
, node_modules
are excluded
Example
Assume you have following structure:
modules/
module1.js
module2.js
app.js
File module1.js
exports:
module.exports = 'module1.exports';
File module2.js
exports:
module.exports = 'module2.exports';
In app.js
:
var modules = require('require-dir-all')('modules');
console.log('modules:', modules);
Result:
modules: {
module1: 'module1.exports',
module2: 'module2.exports'
}
Example located in demo/simple/
To run: cd demo/simple/
, then run npm install
, then node app
Map
Option map
allows to define function to run for each require
'd file.
Object properties. These properties may be changed:
name
- module name to be stored in result objectexports
- module's exports value
These properties are read-only:
path
- filepath,base
- base part of file name,ext
- file extension
Assume you have following structure:
modules/
module1
module2
If each file module1.js
, module2.js
in modules
directory exports a constructor to which the some config parameters are passed like this:
'use strict';
// Object constructor
var Object1 = function(config) {
this.name = 'Object1';
this.config = config;
};
// Exporting constructor function
module.exports = Object1;
and the code which require
's these files in app_old.js
is like following:
// For
var config1 = { value: 'config1' },
config2 = 'config2';
var module1 = new (require('modules/module1'))(config1),
module2 = new ()require('module/module2'))(config2);
You may replace this with following code:
// Store config for each module in config object properties
// with property names corresponding to module names
var config = {
module1: { value: 'config1' },
module2: { value: 'config2' }
};
// Require all files in modules subdirectory
var modules = require('require-dir-all')(
'modules', // Directory to require
{ // Options
map: function(reqModule) {
// define function to be post-processed over exported object for each require-d module
reqModule.exports =
// create new object with corresponding config passed to constructor
new reqModule.exports( config[reqModule.name] );
// Also may change the property name if needed
// reqModule.name = 'prefix_'+reqModule.name;
}
}
);
console.log('modules:', JSON.stringify(modules, null, 2));
Result:
modules: {
"module1": {
"name": "Object1",
"config": {
"value": "config1"
}
},
"module2": {
"name": "Object2",
"config": {
"value": "config2"
}
}
}
Example located in demo/map/
To run: cd demo/map/
, then run npm install
, then node app