让 Gulp-concat 只合并相关数据

Have Gulp-concat merge only relevant data

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 无关的数据,所以我想添加一个额外的步骤以仅从每个 [=] 中提取我需要的 json 的相关部分14=],并将 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 和 returns 相关数据。我不清楚的是如何通过 data.json 对象|路径到达 myCustomFunction()

这几乎就是 gulp-json-editor was made for (the usage example 甚至在 manifest.json 文件中演示的内容。

您只需将自定义函数传递给 jeditor()。每个 data.json 文件都会调用您的自定义函数,您可以 return 修改后的 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 对象。)