1 | # module-deps
|
2 |
|
3 | walk the dependency graph to generate json output that can be fed into
|
4 | [browser-pack](https://github.com/substack/browser-pack)
|
5 |
|
6 | [![build status](https://secure.travis-ci.org/substack/module-deps.png)](http://travis-ci.org/substack/module-deps)
|
7 |
|
8 | # example
|
9 |
|
10 | ``` js
|
11 | var mdeps = require('module-deps');
|
12 | var JSONStream = require('JSONStream');
|
13 |
|
14 | var md = mdeps();
|
15 | md.pipe(JSONStream.stringify()).pipe(process.stdout);
|
16 | md.end({ file: __dirname + '/files/main.js' });
|
17 | ```
|
18 |
|
19 | output:
|
20 |
|
21 | ```
|
22 | $ node example/deps.js
|
23 | [
|
24 | {"id":"/home/substack/projects/module-deps/example/files/main.js","source":"var foo = require('./foo');\nconsole.log('main: ' + foo(5));\n","entry":true,"deps":{"./foo":"/home/substack/projects/module-deps/example/files/foo.js"}}
|
25 | ,
|
26 | {"id":"/home/substack/projects/module-deps/example/files/foo.js","source":"var bar = require('./bar');\n\nmodule.exports = function (n) {\n return n * 111 + bar(n);\n};\n","deps":{"./bar":"/home/substack/projects/module-deps/example/files/bar.js"}}
|
27 | ,
|
28 | {"id":"/home/substack/projects/module-deps/example/files/bar.js","source":"module.exports = function (n) {\n return n * 100;\n};\n","deps":{}}
|
29 | ]
|
30 | ```
|
31 |
|
32 | and you can feed this json data into
|
33 | [browser-pack](https://github.com/substack/browser-pack):
|
34 |
|
35 | ```
|
36 | $ node example/deps.js | browser-pack | node
|
37 | main: 1055
|
38 | ```
|
39 |
|
40 | # usage
|
41 |
|
42 | ```
|
43 | usage: module-deps [files]
|
44 |
|
45 | generate json output from each entry file
|
46 |
|
47 | ```
|
48 |
|
49 | # methods
|
50 |
|
51 | ``` js
|
52 | var mdeps = require('module-deps')
|
53 | ```
|
54 |
|
55 | ## var d = mdeps(opts={})
|
56 |
|
57 | Return an object transform stream `d` that expects entry filenames or
|
58 | `{ id: ..., file: ... }` objects as input and produces objects for every
|
59 | dependency from a recursive module traversal as output.
|
60 |
|
61 | Each file in `files` can be a string filename or a stream.
|
62 |
|
63 | Optionally pass in some `opts`:
|
64 |
|
65 | * `opts.transform` - a string or array of string transforms (see below)
|
66 |
|
67 | * `opts.transformKey` - an array path of strings showing where to look in the
|
68 | package.json for source transformations. If falsy, don't look at the
|
69 | package.json at all.
|
70 |
|
71 | * `opts.resolve` - custom resolve function using the
|
72 | `opts.resolve(id, parent, cb)` signature that
|
73 | [browser-resolve](https://github.com/shtylman/node-browser-resolve) has
|
74 |
|
75 | * `opts.filter` - a function (id) to skip resolution of some module `id` strings.
|
76 | If defined, `opts.filter(id)` should return truthy for all the ids to include
|
77 | and falsey for all the ids to skip.
|
78 |
|
79 | * `opts.postFilter` - a function (id, file, pkg) that gets called after `id` has
|
80 | been resolved. Return false to skip this file.
|
81 |
|
82 | * `opts.packageFilter` - transform the parsed package.json contents before using
|
83 | the values. `opts.packageFilter(pkg, dir)` should return the new `pkg` object to
|
84 | use.
|
85 |
|
86 | * `opts.noParse` - an array of absolute paths to not parse for dependencies. Use
|
87 | this for large dependencies like jquery or threejs which take forever to parse.
|
88 |
|
89 | * `opts.cache` - an object mapping filenames to file objects to skip costly io
|
90 |
|
91 | * `opts.packageCache` - an object mapping filenames to their parent package.json
|
92 | contents for browser fields, main entries, and transforms
|
93 |
|
94 | * `opts.fileCache` - an object mapping filenames to raw source to avoid reading
|
95 | from disk.
|
96 |
|
97 | * `opts.paths` - array of global paths to search. Defaults to splitting on `':'`
|
98 | in `process.env.NODE_PATH`
|
99 |
|
100 | * `opts.ignoreMissing` - ignore files that failed to resolve
|
101 |
|
102 | # input objects
|
103 |
|
104 | Input objects should be string filenames or objects with these parameters:
|
105 |
|
106 | * `row.file` - filename
|
107 | * `row.expose` - name to be exposed as
|
108 | * `row.noparse` when true, don't parse the file contents for dependencies
|
109 |
|
110 | or objects can specify transforms:
|
111 |
|
112 | * `row.transform` - string name, path, or function
|
113 | * `row.options` - transform options as an object
|
114 | * `row.global` - boolean, whether the transform is global
|
115 |
|
116 | # events
|
117 |
|
118 | ## d.on('transform', function (tr, file) {})
|
119 |
|
120 | Every time a transform is applied to a `file`, a `'transform'` event fires with
|
121 | the instantiated transform stream `tr`.
|
122 |
|
123 | ## d.on('file', function (file) {})
|
124 |
|
125 | Every time a file is read, this event fires with the file path.
|
126 |
|
127 | ## d.on('missing', function (id, parent) {})
|
128 |
|
129 | When `opts.ignoreMissing` is enabled, this event fires for each missing package.
|
130 |
|
131 | ## d.on('package', function (pkg) {})
|
132 |
|
133 | Every time a package is read, this event fires. The directory name of the
|
134 | package is available in `pkg.__dirname`.
|
135 |
|
136 | # transforms
|
137 |
|
138 | module-deps can be configured to run source transformations on files before
|
139 | parsing them for `require()` calls. These transforms are useful if you want to
|
140 | compile a language like [coffeescript](http://coffeescript.org/) on the fly or
|
141 | if you want to load static assets into your bundle by parsing the AST for
|
142 | `fs.readFileSync()` calls.
|
143 |
|
144 | If the transform is a function, it should take the `file` name as an argument
|
145 | and return a through stream that will be written file contents and should output
|
146 | the new transformed file contents.
|
147 |
|
148 | If the transform is a string, it is treated as a module name that will resolve
|
149 | to a module that is expected to follow this format:
|
150 |
|
151 | ``` js
|
152 | var through = require('through2');
|
153 | module.exports = function (file, opts) { return through() };
|
154 | ```
|
155 |
|
156 | You don't necessarily need to use the
|
157 | [through2](https://github.com/rvagg/through2) module to create a
|
158 | readable/writable filter stream for transforming file contents, but this is an
|
159 | easy way to do it.
|
160 |
|
161 | When you call `mdeps()` with an `opts.transform`, the transformations you
|
162 | specify will not be run for any files in node_modules/. This is because modules
|
163 | you include should be self-contained and not need to worry about guarding
|
164 | themselves against transformations that may happen upstream.
|
165 |
|
166 | Modules can apply their own transformations by setting a transformation pipeline
|
167 | in their package.json at the `opts.transformKey` path. These transformations
|
168 | only apply to the files directly in the module itself, not to the module's
|
169 | dependants nor to its dependencies.
|
170 |
|
171 | ## package.json transformKey
|
172 |
|
173 | Transform keys live at a configurable location in the package.json denoted by
|
174 | the `opts.transformKey` array.
|
175 |
|
176 | For a transformKey of `['foo','bar']`, the transformKey can be a single string
|
177 | (`"fff"`):
|
178 |
|
179 | ``` json
|
180 | {
|
181 | "foo": {
|
182 | "bar": "fff"
|
183 | }
|
184 | }
|
185 | ```
|
186 |
|
187 | or an array of strings (`["fff","ggg"]`):
|
188 |
|
189 | ``` json
|
190 | {
|
191 | "foo": {
|
192 | "bar": ["fff","ggg"]
|
193 | }
|
194 | }
|
195 | ```
|
196 |
|
197 | If you want to pass options to the transforms, you can use a 2-element array
|
198 | inside of the primary array. Here `fff` gets an options object with `{"x":3}`
|
199 | and `ggg` gets `{"y":4}`:
|
200 |
|
201 | ``` json
|
202 | {
|
203 | "foo": {
|
204 | "bar": [["fff",{"x":3}],["ggg",{"y":4}]]
|
205 | }
|
206 | }
|
207 | ```
|
208 |
|
209 | Options sent to the module-deps constructor are also provided under
|
210 | `opts._flags`. These options are sometimes required if your transform
|
211 | needs to do something different when browserify is run in debug mode, for
|
212 | example.
|
213 |
|
214 | # usage
|
215 |
|
216 | ```
|
217 | module-deps [FILES] OPTIONS
|
218 |
|
219 | Generate json output for the entry point FILES.
|
220 |
|
221 | OPTIONS are:
|
222 |
|
223 | -t TRANSFORM Apply a TRANSFORM.
|
224 | -g TRANSFORM Apply a global TRANSFORM.
|
225 |
|
226 | ```
|
227 |
|
228 | # install
|
229 |
|
230 | With [npm](http://npmjs.org), to get the module do:
|
231 |
|
232 | ```
|
233 | npm install module-deps
|
234 | ```
|
235 |
|
236 | and to get the `module-deps` command do:
|
237 |
|
238 | ```
|
239 | npm install -g module-deps
|
240 | ```
|
241 |
|
242 | # license
|
243 |
|
244 | MIT
|