var gulp = require('gulp'); var sass = require('gulp-sass'); var babel = require('gulp-babel'); var concat = require('gulp-concat'); var rename = require('gulp-rename'); var uglify = require('gulp-uglify'); var sourcemaps = require('gulp-sourcemaps'); var browserSync = require('browser-sync'); var jsonld = require('jsonld'); var through = require('through2') // todo: rollup for d3 & possibly jsonld var paths = { "styles": { "src": "./src/scss/*.scss", "dest": "./assets/css/" }, "scripts": { "src": "./src/js/*.js", "dest": "./assets/js/" }, "data": { "src": "./rubenvandeven.jsonld", "dest": "./assets/js/" } }; gulp.task('styles', function() { gulp.src(paths.styles.src, { sourcemaps: true }) .pipe(sass().on('error', sass.logError)) .pipe(gulp.dest(paths.styles.dest)) .pipe(browserSync.reload({ stream: true })); }); gulp.task('scripts', function() { return gulp.src(paths.scripts.src) .pipe(sourcemaps.init()) .pipe(babel({ ignore: [ './src/js/d3.v5.js', ] })) .pipe(concat('portfolio.js')) .pipe(gulp.dest(paths.scripts.dest)) // save .js .pipe(uglify()) .pipe(rename({ extname: '.min.js' })) .pipe(sourcemaps.write('maps')) .pipe(gulp.dest(paths.scripts.dest)) // save .min.js }); gulp.task('jsonld', function(){ return gulp.src(paths.data.src) .pipe(through.obj(function (file, enc, callback) { let data = JSON.parse(file.contents.toString(enc)); var transformedFile = file.clone(); // console.log(data); jsonld.flatten(data, {"@context": "https://schema.org/"}, (err, flattened)=> { transformedFile.contents = Buffer.from(JSON.stringify(flattened), enc); callback(null, transformedFile) }); })) .pipe(gulp.dest(paths.data.dest)); }); var watchStylesAndScripts = function() { gulp.watch(paths.styles.src,['styles']); gulp.watch(paths.scripts.src,['scripts', browserSync.reload]); gulp.watch([paths.data.src], ['jsonld', browserSync.reload]); } gulp.task('watch', watchStylesAndScripts); // watch files for changes and reload gulp.task('serve', function() { browserSync({ server: { baseDir: '.' } }); gulp.watch(['index.html','rubenvandeven.jsonld'], browserSync.reload); watchStylesAndScripts(); }); gulp.task('default', function() { gulp.parallel('styles', 'scripts'); });