portfolio/gulpfile.js

149 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-08-26 11:15:02 +00:00
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');
2018-10-30 10:44:47 +00:00
var rollup = require('rollup-stream');
2018-08-26 11:15:02 +00:00
var sourcemaps = require('gulp-sourcemaps');
var browserSync = require('browser-sync');
2018-09-22 17:22:05 +00:00
var jsonld = require('jsonld');
2018-10-30 10:44:47 +00:00
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
2018-09-22 17:22:05 +00:00
var through = require('through2')
2018-08-26 11:15:02 +00:00
// todo: rollup for d3 & possibly jsonld
2019-11-21 19:21:06 +00:00
const server = browserSync.create();
function reload(done) {
server.reload();
done();
}
2018-08-26 11:15:02 +00:00
var paths = {
"styles": {
"src": "./src/scss/*.scss",
"dest": "./assets/css/"
},
"scripts": {
2018-10-30 10:14:36 +00:00
"src": "./src/js/portfolio.js",
2018-10-30 10:44:47 +00:00
"dest": "./assets/js/",
"d3src": "./src/js/d3.bundle.js",
"d3destDir": "./assets/js/"
2018-08-26 11:15:02 +00:00
},
"data": {
"src": "./rubenvandeven.jsonld",
2018-09-22 17:22:05 +00:00
"dest": "./assets/js/"
2018-08-26 11:15:02 +00:00
}
};
2019-11-21 19:21:06 +00:00
gulp.task('styles', function(done) {
// console.log(done);
return gulp.src(paths.styles.src, { sourcemaps: true })
2018-08-26 11:15:02 +00:00
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(paths.styles.dest))
2019-11-21 19:21:06 +00:00
.pipe(server.reload({ stream: true }));
2018-08-26 11:15:02 +00:00
});
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' }))
2018-10-30 10:44:47 +00:00
// .pipe(sourcemaps.write('maps'))
2019-11-21 18:59:38 +00:00
.pipe(sourcemaps.write(''))
2018-10-30 10:44:47 +00:00
.pipe(gulp.dest(paths.scripts.d3destDir)) // save .min.js
2019-11-21 19:21:06 +00:00
// .pipe(server.reload())
2018-10-30 10:44:47 +00:00
});
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' }))
2018-08-26 11:15:02 +00:00
.pipe(sourcemaps.write('maps'))
2018-10-30 10:44:47 +00:00
.pipe(gulp.dest(paths.scripts.d3destDir)) // save .min.js
2018-08-26 11:15:02 +00:00
});
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");
}
2018-09-22 17:22:05 +00:00
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)=> {
2018-09-22 17:22:05 +00:00
transformedFile.contents = Buffer.from(JSON.stringify(flattened), enc);
createHtaccessFromFlattenedJsonld(flattened);
2018-09-22 17:22:05 +00:00
callback(null, transformedFile)
});
}))
.pipe(gulp.dest(paths.data.dest));
});
2018-08-26 11:15:02 +00:00
var watchStylesAndScripts = function() {
2019-02-18 10:55:12 +00:00
gulp.watch(paths.styles.src, gulp.series('styles'));
2019-11-21 19:21:06 +00:00
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));
2018-08-26 11:15:02 +00:00
}
gulp.task('watch', watchStylesAndScripts);
// watch files for changes and reload
gulp.task('serve', function() {
2019-11-21 19:21:06 +00:00
server.init({
2018-08-26 11:15:02 +00:00
server: {
baseDir: '.'
}
});
2019-11-21 19:21:06 +00:00
gulp.watch(['index.html','rubenvandeven.jsonld'], reload);
2018-08-26 11:15:02 +00:00
watchStylesAndScripts();
});
gulp.task('default', function() {
gulp.parallel('styles', 'scripts');
});