Git Product home page Git Product logo

module-deps's Introduction

module-deps

walk the dependency graph to generate json output that can be fed into browser-pack

build status

example

var mdeps = require('module-deps');
var JSONStream = require('JSONStream');

var stringify = JSONStream.stringify();
stringify.pipe(process.stdout);

var file = __dirname + '/files/main.js';
mdeps(file).pipe(stringify);

output:

$ node example/deps.js
[
{"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"}}
,
{"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"}}
,
{"id":"/home/substack/projects/module-deps/example/files/bar.js","source":"module.exports = function (n) {\n    return n * 100;\n};\n","deps":{}}
]

and you can feed this json data into browser-pack:

$ node example/deps.js | browser-pack | node
main: 1055

usage

usage: module-deps [files]

  generate json output from each entry file

methods

var mdeps = require('module-deps')

var d = mdeps(files, opts={})

Return a readable stream d of javascript objects from an array of filenames files.

Each file in files can be a string filename or a stream.

Optionally pass in some opts:

  • opts.transform - a string or array of string transforms (see below)

  • opts.transformKey - an array path of strings showing where to look in the package.json for source transformations. If falsy, don't look at the package.json at all.

  • opts.resolve - custom resolve function using the opts.resolve(id, parent, cb) signature that browser-resolve has

  • opts.filter - a function (id) to skip resolution of some module id strings. If defined, opts.filter(id) should return truthy for all the ids to include and falsey for all the ids to skip.

  • opts.packageFilter - transform the parsed package.json contents before using the values. opts.packageFilter(pkg, dir) should return the new pkg object to use.

  • opts.noParse - an array of absolute paths to not parse for dependencies. Use this for large dependencies like jquery or threejs which take forever to parse.

  • opts.cache - an object mapping filenames to file objects to skip costly io

  • opts.packageCache - an object mapping filenames to their parent package.json contents for browser fields, main entries, and transforms

  • opts.paths - array of global paths to search. Defaults to splitting on ':' in process.env.NODE_PATH

events

d.on('transform', function (tr, file) {})

Every time a transform is applied to a file, a 'transform' event fires with the instantiated transform stream tr.

d.on('file', function (file) {})

Every time a file is read, this event fires with the file path.

transforms

module-deps can be configured to run source transformations on files before parsing them for require() calls. These transforms are useful if you want to compile a language like coffeescript on the fly or if you want to load static assets into your bundle by parsing the AST for fs.readFileSync() calls.

If the transform is a function, it should take the file name as an argument and return a through stream that will be written file contents and should output the new transformed file contents.

If the transform is a string, it is treated as a module name that will resolve to a module that is expected to follow this format:

var through = require('through');
module.exports = function (file) { return through() };

You don't necessarily need to use the through module to create a readable/writable filter stream for transforming file contents, but this is an easy way to do it.

When you call mdeps() with an opts.transform, the transformations you specify will not be run for any files in node_modules/. This is because modules you include should be self-contained and not need to worry about guarding themselves against transformations that may happen upstream.

Modules can apply their own transformations by setting a transformation pipeline in their package.json at the opts.transformKey path. These transformations only apply to the files directly in the module itself, not to the module's dependants nor to its dependencies.

package.json transformKey

Transform keys live at a configurable location in the package.json denoted by the opts.transformKey array.

For a transformKey of ['foo','bar'], the transformKey can be a single string ("fff"):

{
  "foo": {
    "bar": "fff"
  }
}

or an array of strings (["fff","ggg"]):

{
  "foo": {
    "bar": ["fff","ggg"]
  }
}

If you want to pass options to the transforms, you can use a 2-element array inside of the primary array. Here fff gets an options object with {"x":3} and ggg gets {"y":4}:

{
  "foo": {
    "bar": [["fff",{"x":3}],["ggg",{"y":4}]]
  }
}

usage

module-deps [FILES] OPTIONS

  Generate json output for the entry point FILES.

OPTIONS are:

  -t TRANSFORM  Apply a TRANSFORM.
  -g TRANSFORM  Apply a global TRANSFORM.

install

With npm, to get the module do:

npm install module-deps

and to get the module-deps command do:

npm install -g module-deps

license

MIT

module-deps's People

Contributors

1602 avatar andreypopp avatar cymen avatar dgbeck avatar dominictarr avatar jaredhanson avatar ljharb avatar mantoni avatar radiosilence avatar volune avatar ximus avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.