0

我正在编写自己的 gulp 插件,看起来像这样......

var through2 = require('through2');
var order = require('gulp-order');

module.exports = function() {
    return through2.obj(function(file, encoding, callback) {
        callback(null, transform(file));
    });
};

function transform(file) {
    // I will modify file.contents here - its ok
    return file;
}

我想在来自 gulp.src 的缓冲区上应用其他一些 gulp 插件。可以使用through2吗?例如,在调用 through2.obj() 之前,我想应用 gulp-order 插件 - 我该怎么做?

4

1 回答 1

0

如果您想将不同的 gulp 插件链接在一起lazypipe通常是不错的选择:

var through2 = require('through2');
var order = require('gulp-order');

function yourPlugin()
    return through2.obj(function(file, encoding, callback) {
        callback(null, transform(file));
    });
}

function transform(file) {
    // I will modify file.contents here - its ok
    return file;
}

function orderPlugin()
    return order(['someFolder/*.js', 'someOtherFolder/*.js']);
}

module.exports = function() {
   return lazypipe().pipe(orderPlugin).pipe(yourPlugin)();
};
于 2016-10-13T07:56:52.203 回答