UNPKG

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