Skip to content

Instantly share code, notes, and snippets.

@sh4869
Created August 29, 2016 18:11
Show Gist options
  • Save sh4869/888cc937155be992d291137a481dad5a to your computer and use it in GitHub Desktop.
Save sh4869/888cc937155be992d291137a481dad5a to your computer and use it in GitHub Desktop.
Gulpでjsonファイルをいい感じに読み込みたい
var gulp = require('gulp');
var pug = require('gulp-pug');
var data = require('gulp-data');
var fs = require('fs');
// hamlタスク設定
gulp.task('pug', function() {
taskname = this.seq.slice(-1)[0]
gulp.src(['./*/*.pug','./*.pug'],{base:'./'})
.pipe(data(function(file){
var dirname = "./json/";
var files = fs.readdirSync(dirname);
var json = {};
files.forEach(function(filename){
json[filename.replace(".json","")] = require(dirname + filename);
});
return {data:json};
}))
.pipe(pug({pretty: true}))
.pipe(gulp.dest('./'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment