22
22
` requireDir('./dir') ` will return the equivalent of:
23
23
24
24
``` js
25
- { a: require (' ./dir/a.js' )
26
- , b: require (' ./dir/b.json' )
25
+ {
26
+ a: require (' ./dir/a.js' ),
27
+ b: require (' ./dir/b.json' )
27
28
}
28
29
```
29
30
30
- And if CoffeeScript has been registered via ` require('coffee-script/register') ` ,
31
+ If CoffeeScript is registered via ` require('coffee-script/register') ` ,
31
32
` c.coffee ` will also be returned. Any extension registered with node will work the same way without any additional configuration.
32
33
33
34
## Installation
@@ -51,7 +52,7 @@ var dir = requireDir('./path/to/dir');
51
52
You can optionally customize the behavior by passing an extra options object:
52
53
53
54
``` js
54
- var dir = requireDir (' ./path/to/dir' , {recurse: true });
55
+ var dir = requireDir (' ./path/to/dir' , { recurse: true });
55
56
```
56
57
57
58
## Options
@@ -60,25 +61,6 @@ var dir = requireDir('./path/to/dir', {recurse: true});
60
61
(` node_modules ` within subdirectories will be ignored.)
61
62
Default is false.
62
63
63
- ` duplicates ` : By default, if multiple files share the same basename, only the
64
- highest priority one is ` require() ` 'd and returned. (Priority is determined by
65
- the order of ` require.extensions ` keys, with directories taking precedence
66
- over files if ` recurse ` is true.) Specifying this option ` require() ` 's all
67
- files and returns full filename keys in addition to basename keys.
68
- Default is false.
69
-
70
- E.g. in the example above, if there were also an ` a.json ` , the behavior would
71
- be the same by default, but specifying ` duplicates: true ` would yield:
72
-
73
- ``` js
74
- { a: require (' ./dir/a.js' )
75
- , ' a.js' : require (' ./dir/a.js' )
76
- , ' a.json' : require (' ./dir/a.json' )
77
- , b: require (' ./dir/b.json' )
78
- , ' b.json' : require (' ./dir/b.json' )
79
- }
80
- ```
81
-
82
64
` filter ` : Apply a filter on the filename before require-ing. For example, ignoring files prefixed with ` dev ` in a production environment:
83
65
84
66
``` js
@@ -109,10 +91,29 @@ requireDir('./dir', {
109
91
})
110
92
```
111
93
94
+ ` duplicates ` : By default, if multiple files share the same basename, only the
95
+ highest priority one is ` require() ` 'd and returned. (Priority is determined by
96
+ the order of ` require.extensions ` keys, with directories taking precedence
97
+ over files if ` recurse ` is true.) Specifying this option ` require() ` 's all
98
+ files and returns full filename keys in addition to basename keys.
99
+ Default is false.
100
+
101
+ In the example above, if there were also an ` a.json ` , the behavior would
102
+ be the same by default, but specifying ` duplicates: true ` would yield:
103
+
104
+ ``` js
105
+ {
106
+ a: require (' ./dir/a.js' ),
107
+ ' a.js' : require (' ./dir/a.js' ),
108
+ ' a.json' : require (' ./dir/a.json' ),
109
+ b: require (' ./dir/b.json' ),
110
+ ' b.json' : require (' ./dir/b.json' )
111
+ }
112
+ ```
113
+
112
114
## Tips
113
115
114
- If you want to ` require() ` the same directory in multiple places, you can do
115
- this in the directory itself! Just make an ` index.js ` file with the following:
116
+ Make an ` index.js ` in a directory with this code to clean things up:
116
117
117
118
``` js
118
119
module .exports = require (' require-dir' )(); // defaults to '.'
0 commit comments