gulp-data
gulp-data copied to clipboard
Using multiple json files with extended arrays
The current behaviour is that, when you using multiple json files and you create the same array in different json file. The array get's overwritten, the better behaviour would be to extend it / merge it with existing array.
Example: I have global json file, where I have array and I need to extend it on different page.
Is it related to issue #5 and #6 ?
probably, but doesn't seems to work for me
.pipe(data(function() { return require('./src/templates/_core/main.json') })) .pipe(data(function(file) { return require('./src/templates/_core/pages/'+ path.basename(file.path).replace('.hbs','.json')) }))
I'm using one main json file, and then one for each page.
I solved it with gulp-merge-json
. Every time a json file get changed I create a combined .cache-data.json
and pass the single json file to my templates. This speed up also page generation since i have a lot json files.
cool, thanks!
I used merge-json to solve this problem.
Like this:
gulp.task('pug', () => {
return gulp.src('src/index.pug')
.pipe(data(() => mergeJson.merge(require('./src/data/projects.json'),
require(
'./src/data/skills.json'))))
.pipe(pug({}))
.pipe(gulp.dest('dist/'))
})
But I think isn't best solution. :thinking:
@raulfdm yeah the problem here is that you have to add a line per every new json file. This is what i have tried but ending up with an empty html page. any ideas?
gulp.task('views:build', ['views:templates'] , () => {
return gulp.src(config.views.src + '*.twig')
.pipe($.data(function() {
return JSON.parse(fs.readFileSync('src/data/timber.json'))
})) // load data from base json file
.pipe($.twig())
.pipe(gulp.dest(config.views.tmp))
.pipe(reload({stream: true}));
});
gulp.task('views:templates', () => {
return gulp.src(config.views.src + 'templates/*.twig')
.pipe($.data(function(file) {
return JSON.parse(fs.readFileSync('src/data/' + path.basename(file.path).replace('.twig', '.json')));
}))
.pipe($.twig())
});
@moesphemie I changed my approach to solve this problem, creating an object with objects, like this:
gulp.task('pug', () => {
return gulp.src('src/index.pug')
.pipe(data(() => {
const result = {
projects: require('./src/data/projects.json'),
about: require('./src/data/about.json'),
abilities: require('./src/data/skills.json')
}
if (PRODUCTION)
result.assets = require('./src/data/rev-manifest.json')
return result
}))
.pipe(pug({}))
.pipe(gulp.dest(DEST_FOLDER))
})
In this way, I just can check:
if (assets)
//Do something