This repository has been archived on 2023-02-03. You can view files and clone it, but cannot push or open issues or pull requests.
autonomic.zone/gulpfile.js
2017-08-31 12:44:57 +01:00

146 lines
3.9 KiB
JavaScript

'use strict';
var gulp = require('gulp');
var sass = require('gulp-sass');
var markdown = require('markdown');
var browserSync = require('browser-sync').create();
var autoprefixer = require('gulp-autoprefixer');
var concat = require('gulp-concat');
var jshint = require('gulp-jshint');
var util = require('gulp-util');
var uglify = require('gulp-uglify');
var cssmin = require('gulp-cssmin');
var nunjucksRender = require('gulp-nunjucks-render');
var data = require('gulp-data');
var pxtorem = require('gulp-pxtorem');
var imagemin = require('gulp-imagemin');
// De-caching for Data files
function requireUncached( $module ) {
delete require.cache[require.resolve( $module )];
return require( $module );
}
var nunjucksCustom = function(environment) {
environment.addFilter('limitTo', function(input, limit) {
if (typeof input === 'object' && Array.isArray(limit)){
if (limit.length >= 0){
var obj = {};
limit.forEach(function(key){
obj[key] = input[key];
});
return obj;
} else {
return input;
}
}
if (typeof limit !== 'number'){
return input;
}
if (typeof input === 'string'){
if(limit >= 0){
return input.substring(0, limit);
} else {
return input.substr(limit);
}
}
if (Array.isArray(input)){
limit = Math.min(limit, input.length);
if(limit >= 0){
return input.slice(0, limit);
} else {
return input.slice(input.length + limit, input.length);
}
}
return input;
});
}
var config = {
production: !!util.env.production // check for --production flag
};
gulp.task('fileinclude', function(){
gulp.src('src/pages/**/*.+(html|nunjucks)')
.pipe(data(function() {
return requireUncached('./src/data/en.json');
}))
.pipe(nunjucksRender({
path: ['src/templates'],
manageEnv: nunjucksCustom
}))
.pipe(gulp.dest('./dist'))
gulp.src(['src/fonts/**/*.*'])
.pipe(gulp.dest('./dist/css/fonts'));
gulp.src(['src/static/**/*.*'])
.pipe(imagemin(
imagemin.jpegtran({progressive: true})
))
.pipe(gulp.dest('./dist'));
});
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: './dist'
},
reloadDebounce: 100
});
});
gulp.task('sass', function () {
gulp.src('./scss/style.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer())
.pipe(pxtorem())
.pipe(config.production ? cssmin() : util.noop())
.pipe(gulp.dest('./dist/css/'))
.pipe(browserSync.stream({once: true}));
});
gulp.task('main-scripts', function(){
return gulp
.src([
'./js/!(_init)*.js', // all files that end in .js EXCEPT _init.js
'./js/_init.js' // include _init.js last
])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(concat('scripts.js'))
.pipe(config.production ? uglify() : util.noop())
.pipe(gulp.dest('./dist/js/'));
});
gulp.task('vendor-scripts', function() {
return gulp
.src([
'./node_modules/jquery/dist/jquery.js',
'./node_modules/jquery-tiny-pubsub/dist/ba-tiny-pubsub.js'
])
.pipe(concat('plugins.js'))
.pipe(config.production ? uglify() : util.noop())
.pipe(gulp.dest('./dist/js/'));
});
gulp.task('watch', ['fileinclude'], function () {
gulp.watch('./scss/**/*.scss', ['sass']);
gulp.watch('./src/**/*.{png,jpg,jpeg,gif,webp,svg,html,md,woff,woff2,ico}', ['fileinclude']);
gulp.watch('./src/**/*.*', ['fileinclude']);
gulp.watch('./js/**/*.js', ['main-scripts'])
gulp.watch('./dist/**/*.html').on('change', browserSync.reload);
});
gulp.task('build', ['main-scripts', 'vendor-scripts', 'sass', 'fileinclude']);
gulp.task('default', ['build', 'browser-sync', 'watch']);