portfolio/gulpfile.js
2019-11-22 21:40:56 +01:00

148 lines
4.2 KiB
JavaScript

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 rollup = require('rollup-stream');
var sourcemaps = require('gulp-sourcemaps');
var browserSync = require('browser-sync');
var jsonld = require('jsonld');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var through = require('through2')
// todo: rollup for d3 & possibly jsonld
const server = browserSync.create();
function reload(done) {
server.reload();
done();
}
var paths = {
"styles": {
"src": "./src/scss/*.scss",
"dest": "./assets/css/"
},
"scripts": {
"src": "./src/js/portfolio.js",
"dest": "./assets/js/",
"d3src": "./src/js/d3.bundle.js",
"d3destDir": "./assets/js/"
},
"data": {
"src": "./rubenvandeven.jsonld",
"dest": "./assets/js/"
}
};
gulp.task('styles', function(done) {
// console.log(done);
return gulp.src(paths.styles.src, { sourcemaps: true })
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(paths.styles.dest))
.pipe(server.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(sourcemaps.write(''))
.pipe(gulp.dest(paths.scripts.d3destDir)) // save .min.js
// .pipe(server.reload())
});
gulp.task('d3', function() {
return rollup( 'rollup.config.js' )
.pipe(source('d3.bundle.js'))
.pipe(gulp.dest(paths.scripts.d3destDir)) // save .js
.pipe(buffer())
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(sourcemaps.write('maps'))
.pipe(gulp.dest(paths.scripts.d3destDir)) // save .min.js
});
function createHtaccessFromFlattenedJsonld(flattened) {
let contentLines = [
"# Don't edit: generated using gulpfile.js",
"RewriteEngine On"
];
let typeLines = {}; // prevent duplicates
for(let el of flattened['@graph']) {
try{
let url = new URL(el['@id']);
if(url.origin != 'https://rubenvandeven.com') {
console.error("Invalid url in id", el['@id']);
continue;
}
if(url.pathname == '' || url.pathname == '/') continue;
let pathname = url.pathname.substr(1); // remove preceding slash ("/ruben" -> "ruben")
contentLines.push(`RewriteRule "^${pathname}$" "index.html"`);
let type = el['@type'].replace(/.*[#|\/]/, "");
typeLines[type] = `RewriteRule "^@type/${type}$" "index.html"`;
} catch(err) {
// not an url
}
}
require('fs').writeFileSync('.htaccess', contentLines.join("\n")+"\n"+Object.values(typeLines).join("\n") + "\n");
}
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, {}, (err, flattened)=> {
transformedFile.contents = Buffer.from(JSON.stringify(flattened), enc);
createHtaccessFromFlattenedJsonld(flattened);
callback(null, transformedFile)
});
}))
.pipe(gulp.dest(paths.data.dest));
});
var watchStylesAndScripts = function() {
gulp.watch(paths.styles.src, gulp.series('styles'));
gulp.watch(paths.scripts.src,gulp.series('scripts', reload));
gulp.watch(paths.scripts.d3src,gulp.series('d3', reload));
gulp.watch([paths.data.src], gulp.series('jsonld', reload));
}
gulp.task('watch', watchStylesAndScripts);
// watch files for changes and reload
gulp.task('serve', function() {
server.init({
server: {
baseDir: '.'
}
});
gulp.watch(['index.html','rubenvandeven.jsonld'], reload);
watchStylesAndScripts();
});
gulp.task('default', function() {
gulp.parallel('styles', 'scripts');
});