简体   繁体   English

Browserify需要目录中的所有文件

[英]Browserify Require All Files in Directory

I'm new to Browserify (and Javascript build systems in general) and have reached a point where I am thoroughly confused. 我是Browserify(以及一般的Javascript构建系统)的新手,并且已经达到了我彻底困惑的程度。

I have Gulp setup to do my builds, which has always worked fine, and lately I've been using Browserify to bundle -- mostly so I can separate my code into separate files and require() them where they need to be. 我有Gulp设置来做我的构建,它一直工作正常,最近我一直在使用Browserify捆绑 - 主要是因此我可以将我的代码分成单独的文件并且require()它们需要的地方。

My issue is, I have a folder with a bunch of small modules I need to require within another module, and I am trying to avoid hard coding the names of all of them. 我的问题是,我有一个文件夹,其中有一些我需要在另一个模块中需要的小模块,我试图避免硬编码所有这些模块的名称。 Is there a way to do a require of all of the files? 有没有办法要求所有文件?

I've tried using Bulkify and Folderify but cannot get them to work. 我尝试过使用BulkifyFolderify但无法让它们工作。 For example, with Bulkify, the package installed is called bulkify and is in the node_modules folder, but then they tell you to require bulk-require , which is in a sub node_modules folder of the bulkify package. 例如,使用Bulkify,安装的软件包称为bulkify ,位于node_modules文件夹中,但随后它们会告诉您需要bulk-require ,它位于bulkify软件包的sub node_modules文件夹中。 When I try that, Browserify chokes with a Cannot find module 'bulk-require'... type error. 当我尝试这样做时,Browserify会Cannot find module 'bulk-require'...一个Cannot find module 'bulk-require'...类型错误。

At this point I am confused because I have no idea why the installation directions of both of those do not work (nor whether they will even help me). 此时我很困惑,因为我不知道为什么这两个的安装方向都不起作用(也不管他们是否会帮助我)。 Am I supposed to be using them in my Gulp file? 我应该在我的Gulp文件中使用它们吗? Or can I use them in one of my modules and it will get called during the Browserify? 或者我可以在我的一个模块中使用它们,它会在Browserify期间被调用吗?

Here is a snippet of my build task for this if this helps: 如果这有帮助,这是我的构建任务的片段:

// Report Builder
gulp.task('script-builder', function() {

    // Unminified
    // **********************************************************
    browserify({entries: './resources/assets/js/report/builder.js'})
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .bundle()
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .pipe(source('builder.js'))
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .pipe(buffer())
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .pipe(gulp.dest('./public/js/debug'));

    // Minified
    // **********************************************************
    browserify({entries: './resources/assets/js/report/builder.js'})
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .bundle()
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .pipe(source('builder.js'))
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .pipe(buffer())
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .pipe(ext_replace('.min.js'))
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .pipe(uglify())
        .on('error', function (err) { console.log(err); this.emit('end'); })
        .pipe(gulp.dest('./public/js/dist'));

});

I have no idea what I am doing here. 我不知道我在这做什么。 Am I just going to have to hardcode the paths in my require() calls or is there a better way? 我是否只require()require()调用中对路径进行硬编码,还是有更好的方法?


EDIT 编辑

I can clearly see bulk-require in the bulkify node module: 我可以清楚地看到bulkify节点模块中的bulk-require

在此输入图像描述

But, when I try to require bulk-require , it chokes: 但是,当我尝试要求bulk-require ,它会窒息:

module.exports = function(type, driver, node) {

    var propertiesContainer = '#property-container';

    var bulk = require('bulk-require');
    var mods = bulk(__dirname, ['properties/**/*.js']);

}

Error: Cannot find module 'bulk-require' from '/path/to/my/project/resources/assets/js/report' 错误:无法从'/ path /到/ my / project / resources / assets / js / report'找到模块'bulk-require'


EDIT 2 编辑2

I was able to make this work using the require-globify package ( https://github.com/capaj/require-globify ). 我能够使用require-globify包( https://github.com/capaj/require-globify )来完成这项工作。 In my javascript, I used: 在我的javascript中,我用过:

var properties = require('./properties/*.js', {mode: 'hash', resolve: ['path-reduce', 'strip-ext']});

That returned an object with keys as the filename without extension or the path prefix. 返回一个对象,其中键作为没有扩展名的文件名或路径前缀。

In my gulpfile.js, I did this: 在我的gulpfile.js中,我这样做了:

browserify({
    entries: './resources/assets/js/report/builder.js',
    transform: ['require-globify']
})
.on('error', function (err) { console.log(err); this.emit('end'); })
.bundle()
.on('error', function (err) { console.log(err); this.emit('end'); })
.pipe(source('builder.js'))
.on('error', function (err) { console.log(err); this.emit('end'); })
.pipe(buffer())
.on('error', function (err) { console.log(err); this.emit('end'); })
.pipe(gulp.dest('./public/js/debug'));

That's a pretty easy task to achieve. 这是一项非常容易实现的任务。 Using fs you may dynamically require all the dependencies at once simply accessing your node_modules folder. 使用fs,您可以一次性动态地需要所有依赖项,只需访问node_modules文件夹即可。

var normalizedPath = require("path").join(__dirname, "node_modules/bulkify");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
  require("./node_modules/bulkify" + file);
});

More answers on this question can be found here 关于这个问题的更多答案可以在这里找到

EDIT Apologies for the generic answer I kinda misunderstood the question about dynamically requiring files into Browserify. 编辑道歉的通用答案我有点误解了关于动态要求文件到Browserify的问题。

Require-globify seems a neat approach for your problem. Require-globify似乎是一个很好的解决方案。

Take a moment to have a look at this answer as well Compiling dynamically required modules with Browserify 花一点时间来看看这个答案以及使用Browserify编译动态所需的模块

I haven't used it, but I think bulkify will do what you want. 我没有用它,但我认为bulkify会做你想要的。

Am I supposed to be using them in my Gulp file? 我应该在我的Gulp文件中使用它们吗? Or can I use them in one of my modules and it will get called during the Browserify? 或者我可以在我的一个模块中使用它们,它会在Browserify期间被调用吗?

Yes and yes. 是的,是的。

I think the gist of it is something like this: 我认为它的要点是这样的:

gulpfile.js

var bulkify = require('bulkify');

browserify(...)
  .transform(bulkify)
  // ...

another-module.js (bundled module) another-module.js (捆绑模块)

var bulk = require('bulk-require');
var a_bunch_of_small_modules = bulk(__dirname, ['somdir/**/*.js']);
a_bunch_of_small_modules.somedir.whatever();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM