barebones/gulpfile.babel.js

197 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-06-06 16:23:36 +02:00
import gulp from 'gulp';
import clean from 'gulp-clean';
import sass from 'gulp-sass';
import gulpif from 'gulp-if';
import autoprefixer from 'gulp-autoprefixer';
import sourcemaps from 'gulp-sourcemaps';
import { rollup } from 'rollup';
import buble from 'rollup-plugin-buble';
import nodeResolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import json from 'rollup-plugin-json';
import replace from 'rollup-plugin-replace';
import multiEntry from 'rollup-plugin-multi-entry';
import uglify from 'rollup-plugin-uglify';
import { minify } from 'uglify-js';
import imagemin from 'gulp-imagemin';
2017-11-21 18:27:56 +01:00
import notify from 'gulp-notify';
2017-06-06 16:23:36 +02:00
import runSequence from 'run-sequence';
2017-11-21 18:27:56 +01:00
import path from 'path';
2017-06-06 16:23:36 +02:00
2017-11-30 13:39:39 +01:00
/**
* Barebones config
*/
import config from './config.barebones';
const { log } = console;
let production = false; // = build
2017-11-21 18:27:56 +01:00
let error = false;
2017-06-06 16:23:36 +02:00
/**
* Tasks - in order
*/
const tasks = ['styles', 'scripts', 'images'];
2017-06-06 16:23:36 +02:00
2017-11-21 18:27:56 +01:00
/**
* Notification
*
* @param {string} message
* @param {string} status
*/
function notification(message = '', status = 'success') {
return gulp.src('./node_modules/gulp-notify/test/fixtures/1.txt')
.pipe(notify({
title: 'Barebones',
message,
icon: path.join(__dirname, `assets/icons/${status}.png`),
}));
2017-11-21 18:27:56 +01:00
}
2017-06-06 16:23:36 +02:00
/**
2017-11-30 13:10:22 +01:00
* Clean
2017-06-06 16:23:36 +02:00
*/
2017-11-30 13:10:22 +01:00
gulp.task('clean', () => (
2020-04-27 17:18:59 +02:00
gulp.src([`${config.base.public}/js`], {
read: false,
})
2020-04-27 17:17:36 +02:00
.pipe(clean())
2017-06-06 16:23:36 +02:00
));
/**
* Styles
*/
gulp.task('styles', () => {
const stylesheets = config.styles;
stylesheets.push(`${config.base.src}/styles/*.scss`);
gulp.src(stylesheets)
.pipe(gulpif(!production, sourcemaps.init()))
.pipe(sass({
outputStyle: production ? 'compressed' : 'nested',
}).on('error', (err) => {
notification('Failed to compile styles. 😱', 'error');
log(err.stack);
error = true;
}))
.pipe(autoprefixer({
browsers: ['last 10 versions'],
}))
.pipe(gulpif(!production, sourcemaps.write('.')))
2020-04-27 17:17:36 +02:00
.pipe(gulp.dest(`${config.base.public}`));
});
2017-06-06 16:23:36 +02:00
const roll = (entry, dest) => {
let env = 'development';
if (production) {
env = 'production';
}
2017-06-06 16:23:36 +02:00
return rollup({
entry,
plugins: [
multiEntry(),
buble(),
nodeResolve({
browser: true,
main: true,
jsnext: true,
}),
commonjs({
include: [
'node_modules/**',
2017-11-30 13:39:39 +01:00
`${config.base.src}/**`,
2017-06-06 16:23:36 +02:00
],
}),
json(),
replace({
'process.env.NODE_ENV': JSON.stringify(env),
}),
production ? uglify({}, minify) : '',
],
}).then((bundle) => {
bundle.write({
format: 'iife',
moduleName: 'BarebonesBundle',
sourceMap: !production,
dest,
2017-06-06 16:23:36 +02:00
});
}).catch((err) => {
notification('Failed to compile scripts. 😱', 'error');
log(err.stack);
error = true;
});
};
gulp.task('scripts', (cb) => {
if (config.scripts.length) {
config.scripts.forEach((filePath) => {
const formattedPath = filePath.replace(/^\/|\/$/g, ''); // remove leading forward slash
2018-04-17 13:31:33 +02:00
const entry = `${config.base.src}/scripts/${formattedPath}`;
const dest = `${config.base.public}/js/${formattedPath.replace('.js', '.min.js')}`;
// regex to remove duplicate forward slashes
roll(entry.replace(/([^:]\/)\/+/g, '$1'), dest.replace(/([^:]\/)\/+/g, '$1'));
});
}
cb();
2017-06-06 16:23:36 +02:00
});
/**
* Watch
*/
gulp.task('watch-files', tasks, () => {
2017-11-30 13:39:39 +01:00
gulp.watch(`${config.base.src}/styles/**/*.scss`, ['styles']);
gulp.watch(`${config.base.src}/scripts/**/*.js`, ['scripts']);
gulp.watch(`${config.base.src}/images/**/*.*`, ['images']);
2017-06-06 16:23:36 +02:00
});
/**
* Images
*/
gulp.task('images', (cb) => {
// hadle all images that are not svg
2017-11-30 13:39:39 +01:00
gulp.src(`${config.base.src}/images/**/*.*`)
.pipe(imagemin({
progressive: true,
svgoPlugins: [{
removeViewBox: false,
}],
}))
.pipe(gulp.dest(`${config.base.public}/img`))
.on('end', () => cb());
2017-06-06 16:23:36 +02:00
});
/**
* Main Tasks
*/
gulp.task('watch', cb => (
runSequence('clean', () => {
runSequence(tasks, 'watch-files', () => {
if (!error) {
notification('Watching files... 👀');
}
cb();
});
})
2017-06-06 16:23:36 +02:00
));
gulp.task('build', (cb) => {
production = true;
runSequence('clean', () => {
runSequence(tasks, () => {
if (!error) {
notification('Build complete! 🍻');
cb();
}
});
});
2017-06-06 16:23:36 +02:00
});
gulp.task('default', cb => (
runSequence('clean', () => {
runSequence(tasks, () => {
cb();
});
})
2017-06-06 16:23:36 +02:00
));