我想把我的文件合并到一个文件中,然后删除原始文件。我应该如何在默认任务中解决此问题?
var es = require('event-stream');
var concat = require('gulp-concat');
var unique = require('array-unique');
function concatGroup(groupName, group){
return gulp.src(unique(group.files))
.pipe(concat(groupName + '.js'))
.pipe(gulp.dest(group.target));
}
gulp.task('default', function () {
var groups = {
test: {
files: [array of files],
target: "target dir"
},
test2: {
files: [array of files],
target: "target dir"
},
test3: {
files: [array of files],
target: "target dir"
}
};
var streams = [];
for (var groupName in groups) {
streams.push(concatGroup(groupName, groups[groupName]));
}
return es.concat.apply(es, streams);
});
只需侦听流中的end
事件,然后使用del
:
var concat = require('gulp-concat');
var unique = require('array-unique');
var del = require('del');
gulp.task('default', function (done) {
var groupName = "test",
group = {
files: [array of files],
target: "target dir"
};
var files = unique(group.files)
gulp.src(files)
.pipe(concat(groupName + '.js'))
.pipe(gulp.dest(group.target))
.on('end', function() {
del(files).then(function() {
done();
});
});
});
感谢大家的帮助,这是我的最后一段代码,运行良好。我只需要创建一个新的空流,并将其返回给事件流合并,并在文件删除时标记为完成。
var es = require('event-stream');
var concat = require('gulp-concat');
var unique = require('array-unique');
var del = require('del');
function concatGroup(groupName, group) {
var stream = through.obj();
gulp.src(unique(group.files))
.pipe(concat(groupName + '.js'))
.pipe(gulp.dest(group.target))
.on('end', function () {
del(group.files).then(function () {
stream.end();
});
});
return stream;
}
gulp.task('default', function () {
var groups = {
test: {
files: [array of files],
target: "target dir"
},
test2: {
files: [array of files],
target: "target dir"
},
test3: {
files: [array of files],
target: "target dir"
}
};
var streams = [];
for (var groupName in groups) {
streams.push(concatGroup(groupName, groups[groupName]));
}
return es.concat.apply(es, streams);
});