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()。
发布于 2017-01-14 04:00:06
这就是创建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对象。)
https://stackoverflow.com/questions/41641645
复制相似问题