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()
。
答案 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对象。)