[英]Browserify: Use module.exports if required, otherwise expose global
[英]Tell Browserify to expose module deps
有什么方法可以将模块的内部依赖项公开给全局require
吗?
我的意思是我有这样的index.js
:
var dep = require('./dep.js');
console.log(dep);
我已经通过如下的API将其添加到Browserify捆绑包中:
var b = browserify();
b.add('path/to/module');
b.bundle().pipe(process.stdout);
那么如何在结果包文件之外获取require('./dep.js')
?
我说的是Browserify的Node.js API,而不是CLI。
我不确定这是否是您要的内容,但是:
var browserify = require('browserify');
var b = browserify();
var deps = [];
b.add('path/to/module');
// pull array out of stream:
b.deps().on('data', function(data){deps.push(data);})
.on('end',function(){
deps.forEach(function(item)
{
//item.id will be the file name
console.log(item);
});
} );
如果你从字面上只是想需要依赖,browserify使用内部模块DEPS是在github 这里或NPM。
关于什么
// index.js
var dep = require('./dep.js');
console.log(dep);
//========================
// your browserify script
var b = browserify();
b.add('./path/to/index.js');
// expose the module to global require as 'deps'
b.require('./path/to/deps.js', { expose: 'deps' });
b.bundle().pipe(process.stdout);
这应该使deps.js
模块作为deps
在捆绑包之外可用
因此,在开发人员工具控制台中执行此操作将使您可以访问该模块
require('deps'); // will require the deps.js module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.