处理Gulp插件中的多个文件 [英] Handle multiple files in a Gulp plugin

查看:114
本文介绍了处理Gulp插件中的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个gulp插件。您可以像这样使用它:

I am trying to write a gulp plugin. You use it like this:

var gulp = require('gulp'),
    codo = require('gulp-codo');

gulp.task('doc', function () {
    return gulp.src('*.coffee')
    .pipe(codo()) // codo(name, title, readme, dir)
});

我希望* .coffee将当前目录中的所有coffeescript文件提供给codo()。

I want *.coffee to provide all the coffeescript files in the current directory to codo().

在插件中,输入的处理方式如下所示:

In the plugin the input is handled like so:

 return through.obj(function(file, enc, cb) {

     invokeCodo(file.path, name, title, readme, dir, function(err, data) {
         if(err) {
            cb(new gutil.PluginError('gulp-codo', err, {fileName: file.path}));
            return;
         }
         file.contents = new Buffer(data);
         cb(null, file);
    });
});

我注意到file.path只是第一个通配符匹配而不是所有的匹配。我怎样才能循环这每场比赛。我试着在这里搜索,但只发现了有关在Gulp插件中生成多个输出的问题。

I noticed that file.path is only the first wildcard match and not all the matches. How can I loop this for each match. I tried search here, but only found questions relating to generating mutiple outputs in a Gulp plugin.

感谢。

推荐答案

gulp-foreach 可能会对您有所帮助。你的任务应该看起来像这样:

gulp-foreach might help you. Your task should then look something like this:

gulp.task('doc', function () {
    return gulp.src('*.coffee')
        .pipe(foreach(function(stream, file){
            return stream
                .pipe(codo()) // codo(name, title, readme, dir)
                .pipe(concat(file.name));
        });
});

另外,不要忘记在文件顶部包含 gulp-foreach 这样的文件:

Also, do not forget to include gulp-foreach like this at the top of your file:

var foreach = require('gulp-foreach');

这篇关于处理Gulp插件中的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆