在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):joshswan/gulp-merge-json开源软件地址(OpenSource Url):https://github.com/joshswan/gulp-merge-json开源编程语言(OpenSource Language):JavaScript 99.6%开源软件介绍(OpenSource Introduction):gulp-merge-jsonA gulp plugin for deep-merging multiple JSON files into one file. Export as JSON or a node module. Usagegulp.src('jsonFiles/**/*.json')
.pipe(merge(options))
.pipe(gulp.dest('./dist')); Options
Examplesvar merge = require('gulp-merge-json');
/**
* Basic functionality
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge())
.pipe(gulp.dest('./dist'));
/**
* Edit JSON with function
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
fileName: 'file.json',
edit: (parsedJson, file) => {
if (parsedJson.someValue) {
delete parsedJson.otherValue;
}
return parsedJson;
},
}))
.pipe(gulp.dest('./dist'));
/**
* Edit final JSON with transformer function
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
fileName: 'file.json',
transform: (mergedJson) => {
return {
key: {
type: 'data',
...mergedJson,
};
};
},
}))
.pipe(gulp.dest('./dist'));
/**
* Provide a default object (files are merged in order so object values will be overwritten)
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
startObj: { someKey: 'defaultValue' },
}))
.pipe(gulp.dest('./dist'));
/**
* Provide an overwriting object (merged at the end)
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
endObj: { someKey: 'specialValue' },
}))
.pipe(gulp.dest('./dist'));
/**
* Output module.exports = {JSON_DATA}
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
exportModule: true,
}))
.pipe(gulp.dest('./dist'));
/**
* Output a custom variable = {JSON_DATA}
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
fileName: 'dataModule.js',
exportModule: 'const myVar',
}))
.pipe(gulp.dest('./dist'));
/**
* Provide replacer and space options for JSON.stringify
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
jsonSpace: ' ',
jsonReplacer: (key, value) => {/*...*/}
})
.pipe(gulp.dest('./dist'));
/**
* Use a customizer function for custom merging behavior
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
customizer: (objA, objB) => {
// Example: Concat arrays but only keep unique values
if (Array.isArray(objA) && Array.isArray(objB)) {
return objA.concat(objB).filter((item, index, array) => (
array.indexOf(item) === index
));
}
return undefined;
},
}))
.pipe(gulp.dest('./dist'));
/**
* JSON5
*/
gulp.src('jsonFiles/**/*.json5')
.pipe(merge({
json5: true,
}))
.pipe(gulp.dest('./dist')); Example Input/*
json/defaults.json
*/
{
"key1": {
"data1": "value1",
"data2": "value2"
},
"key2": {
"dataA": "valueA",
"dataB": {
"a": "b",
"c": "d"
}
}
}
/*
json/development.json
*/
{
"key1": {
"data1": "devValue"
},
"key2": {
"dataB": {
"c": "DEV MODE!"
}
},
"key3": {
"important": "value"
}
} Example Output/*
dist/combined.json
*/
{
"key1": {
"data1": "devValue",
"data2": "value2"
},
"key2": {
"dataA": "valueA",
"dataB": {
"dataA": "valueA",
"dataB": {
"a": "b",
"c": "DEV MODE!"
}
}
},
"key3": {
"important": "value"
}
} |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论