让Gulp-concat只合并相关数据

时间:2017-01-13 18:59:30

标签: node.js gulp gulp-concat

concat的基本用例很简单:

gulp.task('generate-manifest', function () {
  return gulp.src('./*/data.json')
    .pipe(concat('manifest.json'))
    .pipe(gulp.dest(./path/to/dest));
});

但是data.json包含一些与manifest.json无关的数据,所以我想添加一个额外的步骤,从每个data.json中仅提取我需要的json的相关部分,然后添加那个json到清单。

我希望我可以这样:

gulp.task('generate-manifest', function () {
  return gulp.src('./*/data.json')
    .pipe(myCustomFunction())
    .pipe(concat('manifest.json'))
    .pipe(gulp.dest(./path/to/dest));
});

myCustomFunction()data.json并仅返回相关数据。我不清楚的是如何将data.json对象|路径传递给myCustomFunction()

1 个答案:

答案 0 :(得分:1)

这几乎是gulp-json-editor的作用(usage example甚至在manifest.json文件上展示了它。)

您所要做的就是将自定义功能传递给jeditor()。为每个data.json文件调用自定义函数,您可以返回仅包含相关数据的已修改JSON对象:

var jeditor = require("gulp-json-editor");

gulp.task('generate-manifest', function () {
  return gulp.src('./*/data.json')
    .pipe(jeditor(function(json) {
      delete json.unwantedData;
      return json;
    }))
    .pipe(concat('manifest.json'))
    .pipe(gulp.dest('./path/to/dest'));
});

(顺便说一句:我很确定你不能只是连接一堆JSON对象并从中获取一个有效的JSON对象。)