UNPKG

2.22 kBMarkdownView Raw
1# Description
2
3Write minimal node index.js files that require and export siblings by file basename
4
5# Latest Version
6
71.2.0
8
9# Installation
10```
11npm install requireindex
12```
13
14or in package.json
15
16```json
17{
18 ...
19 "dependencies": {
20 "requireindex": "1.1.x"
21 }
22}
23```
24
25# Usage
26Check the [test directory](https://github.com/stephenhandley/requireindex/tree/master/test) for example usage. The [test/lib](https://github.com/stephenhandley/requireindex/tree/master/test/lib) looks like:
27
28```
29lib/
30 index.js
31 Foo.js
32 bar/
33 index.js
34 f.js
35 fing.js
36 fed/
37 again.js
38 ignored.js
39 index.js
40 somemore.js
41 bam.js
42 _private.js
43
44```
45
46The index.js files in [test/lib/](https://github.com/stephenhandley/requireindex/tree/master/test/lib/index.js) and [test/lib/bar/](https://github.com/stephenhandley/requireindex/tree/master/test/lib/bar/index.js) contain:
47
48```js
49module.exports = require('requireindex')(__dirname);
50```
51
52and the index.js file in [test/lib/bar/fed/](https://github.com/stephenhandley/requireindex/tree/master/test/lib/bar/fed/index.js) contains:
53
54```js
55module.exports = require('requireindex')(__dirname, ['again', 'somemore']);
56```
57
58The optional second argument allows you to explicitly specify the required files using their basename. In this example [test/lib/bar/fed/ignored.js](https://github.com/stephenhandley/requireindex/tree/master/test/lib/bar/fed/ignored.js) is not included as a public module. The other way to make a module/file private without the need for explicitly naming all the other included files is to prefix the filename with an underscore, as demonstrated by [test/lib/_private.js](https://github.com/stephenhandley/requireindex/tree/master/test/lib/_private.js) which is not exported.
59
60So, with these index.js files, the result of
61
62```js
63require('lib');
64```
65
66is:
67
68```js
69{
70 bam: {
71 m: [Function],
72 n: [Function]
73 },
74 bar: {
75 f: [Function],
76 fed: {
77 again: [Function],
78 somemore: [Function]
79 },
80 fing: [Function]
81 },
82 Foo: {
83 l: [Function],
84 ls: [Function]
85 }
86}
87```
88
89#Build status
90[![build status](https://secure.travis-ci.org/stephenhandley/requireindex.png)](http://travis-ci.org/stephenhandley/requireindex)