在Gulp中concat之后删除原始文件

时间:2016-04-06 11:03:34

标签: node.js gulp gulp-concat

我想将我的文件连接起来并将其写入单个文件,然后我想删除原始文件。我应该如何在默认任务中解决这个问题?

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);
});

2 个答案:

答案 0 :(得分:4)

只需在您的信息流中收听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();
      });
    });
});

答案 1 :(得分:0)

谢谢你们的帮助,这是我的最终代码,它运作正常。我只需创建一个新的空流并返回事件流合并,并在删除文件时将其标记为已完成。

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);
});