模块数据
将依赖图走动以生成可输入的 json 输出 浏览器软件包
实例
var mdeps = require('module-deps');
var JSONStream = require('JSONStream');
var md = mdeps();
md.pipe(JSONStream.stringify()).pipe(process.stdout);
md.end({ file: __dirname + '/files/main.js' });
输出 :
$ 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":{}}
]
您可以将 json 数据输入 浏览器软件包数字 :
$ node example/deps.js | browser-pack | node
main: 1055
使用量
usage: module-deps [files]
generate json output from each entry file
方法
var mdeps = require('module-deps')
var d = mdeps (opts ) (中文(简体) )
返回对象变换流 d 需要条目文件名或
{ id: ..., file: ... } 对象作为输入,并为每个对象生成对象
依赖自递归模块作为输出.
每个文件在 files 可以是字符串文件名或流.
可以选择通过一些 opts数字 :
-
opts.transform- 字符串或数组的字符串变换(见下文) -
opts.transformKey- 显示查找位置的字符串阵列路径 package.json 用于源变换. 如果发作,不要看 包裹。 json 一点都没有. -
opts.resolve- 使用opts.resolve(id, parent, cb)签名 浏览器解析 已经 -
opts.filter- 跳过某些模块解析的函数( id)id字符串. 如果定义,opts.filter(id)应返回真实性,以便包含所有编号 和假的,所有ids跳过. -
opts.postFilter- 之后被调用的函数(id、文件、pkg)id已经 已经解决。 返回错误以跳过此文件 . -
opts.packageFilter- 在使用前将已解析的软件包进行转换 价值观.opts.packageFilter(pkg, dir)应该返回新的pkg对象 使用时. -
opts.noParse- 一系列绝对路径,不分析依赖性。 使用 这对像jquery或3j这样的大型依赖性来说是需要永久解析的. -
opts.cache- 对象映射文件名以文件对象以跳过昂贵的io -
opts.packageCache- 对象映射文件名到他们的父软件包.json 浏览器字段、主要条目和变换的内容 -
opts.fileCache- 对象映射文件名到原始来源以避免读取 从磁盘. -
opts.persistentCache- 一个复杂的缓存处理器,允许同步和持续 隐藏数据。 A 类persistentCache需要遵循此接口 :function persistentCache ( file, // the path to the file that is loaded id, // the id that is used to reference this file pkg, // the package that this file belongs to fallback fallback, // async fallback handler to be called if the cache doesn't hold the given file cb // callback handler that receives the cache data ) { if (hasError()) { return cb(error) // Pass any error to the callback } var fileData = fs.readFileSync(file) var key = keyFromFile(file, fileData) if (db.has(key)) { return cb(null, { source: db.get(key).toString(), package: pkg, // The package for housekeeping deps: { 'id': // id that is used to reference a required file 'file' // file path to the required file } }) } // // The fallback will process the file in case the file is not // in cache. // // Note that if your implementation doesn't need the file data // then you can pass `null` instead of the source and the fallback will // fetch the data by itself. // fallback(fileData, function (error, cacheableEntry) { if (error) { return cb(error) } db.addToCache(key, cacheableEntry) cb(null, cacheableEntry) }) } -
opts.paths- 搜索的全球路径。 默认拆分于':'内process.env.NODE_PATH -
opts.ignoreMissing- 忽略未能解决的文件
输入对象
输入对象应为带有这些参数的字符串文件名或对象:
row.file- 文件名row.expose- 名称应作为row.noparse当属实时, 不分析文件内容的依赖性
或对象可以指定变换:
row.transform- 字符串名称、路径或函数row.options- 将选项转换为对象row.global- 布尔,无论转变是全球性的
事件
d.on (' transform', 函数 (tr, 文件) ) qq)
每次变换都应用到 file页:1 'transform' 发生火灾
即时变流 tr。 。 。 .
d.on('file',函数 (file) * )
每次读取文件时,此事件都会随文件路径起火.
d.on ('missing', 函数 (id, 父) qq)
何时 opts.ignoreMissing 启用,此事件为每个缺失的软件包起火.
d.on('package',函数 (pkg) * )
每次读到一个包,这个事件就会起火. 该目录名称
软件包可用于 pkg.__dirname。 。 。 .
变换
模块解析器可以配置为在文件前运行源转换
解析 require() 打电话 这些变换如果你想的话是有用的
编译语言 咖啡纸 苍蝇或
如果您想要将静态资产装入捆绑中, 请解析 AST
fs.readFileSync() 打电话.
如果变换是一个函数,它应该采取 file 名称作为参数
返回将写入文件内容并输出的流
新建已转换的文件内容 .
如果变换为字符串, 它将被作为模块名称处理 到一个模块,预计将遵循此格式:
var through = require('through2');
module.exports = function (file, opts) { return through() };
你不必用这个 通过2 模块创建 可读/ 可写过滤流, 用于转换文件内容, 但是这是 很简单.
当你打电话 mdeps() 带有 opts.transform,改变你
指定不会运行节点 模块/中的任何文件 。 这是因为模块
包括你 应该是自成一体 不需要担心守卫
面对上游可能发生的转变.
模块可以通过设置转换管道来应用自己的转换
他们的包裹 opts.transformKey 路径。 这些转变
仅适用于模块本身中直接的文件,而不是模块的
受抚养人或受抚养人.
软件包.json 变换键
变形键活在包中的可配置位置. json 表示为
页:1 opts.transformKey 阵列.
变形金库 ['foo','bar'],转换键可以是单个字符串
(单位:千美元)"fff":
{
"foo": {
"bar": "fff"
}
}
或字符串数组( r)["fff","ggg"]:
{
"foo": {
"bar": ["fff","ggg"]
}
}
如果您想要将选项传递到变换中, 您可以使用 2 元素阵列
在主阵列内。 给 fff 获取选项对象 {"x":3}
财务报告和审定财务报表 ggg 获得 {"y":4}数字 :
{
"foo": {
"bar": [["fff",{"x":3}],["ggg",{"y":4}]]
}
}
发送给模块解析构建器的选项也在下面提供
opts._flags。这些选项有时需要,如果您的转换
当浏览器以调试模式运行时, 需要做一些不同的事情
实例.
使用量
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.
安装
与 无,使模块完成:
npm install module-deps
并获得 module-deps 命令执行 :
npm install -g module-deps
许可证
麻省理工学院
