2019-04-07 22:00:52 -04:00
|
|
|
const gulp = require('gulp')
|
|
|
|
const sass = require('gulp-sass')
|
|
|
|
const postcss = require('gulp-postcss')
|
|
|
|
const autoprefixer = require('autoprefixer')
|
|
|
|
const cssnano = require('cssnano')
|
|
|
|
const sourcemaps = require('gulp-sourcemaps')
|
2019-04-07 22:18:31 -04:00
|
|
|
const bytediff = require('gulp-bytediff')
|
2019-04-07 22:00:52 -04:00
|
|
|
const browserSync = require('browser-sync').create()
|
2019-04-07 20:52:18 -07:00
|
|
|
const chalk = require('chalk');
|
2019-04-07 22:00:52 -04:00
|
|
|
|
|
|
|
const paths = {
|
|
|
|
styles: {
|
|
|
|
src: 'src/**/*.scss',
|
|
|
|
dest: 'dist'
|
|
|
|
},
|
|
|
|
html: {
|
|
|
|
src: 'index.html'
|
|
|
|
}
|
2019-04-07 17:41:13 -07:00
|
|
|
}
|
|
|
|
|
2019-04-07 20:52:18 -07:00
|
|
|
// https://stackoverflow.com/a/20732091
|
|
|
|
function humanFileSize(size) {
|
|
|
|
var i = Math.floor( Math.log(size) / Math.log(1024) );
|
|
|
|
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
|
|
|
|
};
|
|
|
|
|
2019-04-07 23:57:25 -04:00
|
|
|
function formatByteMessage(source, data) {
|
|
|
|
const change = (data.savings > 0 ? 'saved' : 'gained')
|
|
|
|
const prettySavings = humanFileSize(Math.abs(data.savings))
|
|
|
|
const prettyStartSize = humanFileSize(data.startSize)
|
|
|
|
let prettyEndSize = humanFileSize(data.endSize)
|
2019-04-07 20:52:18 -07:00
|
|
|
|
2019-04-07 23:57:25 -04:00
|
|
|
if (data.endSize > data.startSize) {
|
|
|
|
prettyEndSize = chalk.yellow(prettyEndSize)
|
2019-04-07 20:52:18 -07:00
|
|
|
}
|
|
|
|
|
2019-04-07 23:57:25 -04:00
|
|
|
if (data.endSize < data.startSize) {
|
|
|
|
prettyEndSize = chalk.green(prettyEndSize)
|
2019-04-07 20:52:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return `${chalk.cyan(source.padStart(12, ' '))}: ${data.fileName} ${change} ${prettySavings} (${prettyStartSize} -> ${prettyEndSize})`
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:41:13 -07:00
|
|
|
function style() {
|
2019-04-07 22:00:52 -04:00
|
|
|
return (
|
|
|
|
gulp.src(paths.styles.src)
|
|
|
|
.pipe(sourcemaps.init())
|
|
|
|
.pipe(sass())
|
|
|
|
.on('error', sass.logError)
|
2019-04-07 22:18:31 -04:00
|
|
|
.pipe(bytediff.start())
|
2019-04-07 20:52:18 -07:00
|
|
|
.pipe(postcss([ autoprefixer()]))
|
2019-04-07 23:57:25 -04:00
|
|
|
.pipe(bytediff.stop((data) => formatByteMessage('autoprefixer', data)))
|
2019-04-07 20:52:18 -07:00
|
|
|
.pipe(bytediff.start())
|
|
|
|
.pipe(postcss([cssnano()]))
|
2019-04-07 23:57:25 -04:00
|
|
|
.pipe(bytediff.stop((data) => formatByteMessage('cssnano', data)))
|
2019-04-07 22:00:52 -04:00
|
|
|
.pipe(sourcemaps.write('.'))
|
|
|
|
.pipe(gulp.dest(paths.styles.dest))
|
|
|
|
.pipe(browserSync.stream())
|
|
|
|
)
|
2019-04-07 17:41:13 -07:00
|
|
|
}
|
|
|
|
|
2019-04-07 22:00:52 -04:00
|
|
|
function reload() {
|
|
|
|
browserSync.reload()
|
2019-04-07 17:47:23 -07:00
|
|
|
}
|
|
|
|
|
2019-04-07 17:41:13 -07:00
|
|
|
function watch() {
|
2019-04-07 22:00:52 -04:00
|
|
|
style()
|
2019-04-07 17:47:23 -07:00
|
|
|
|
2019-04-07 22:00:52 -04:00
|
|
|
browserSync.init({
|
|
|
|
server: {
|
|
|
|
baseDir: './',
|
|
|
|
},
|
|
|
|
startPath: 'index.html'
|
|
|
|
})
|
2019-04-07 17:47:23 -07:00
|
|
|
|
2019-04-07 22:00:52 -04:00
|
|
|
gulp.watch(paths.styles.src, style)
|
|
|
|
gulp.watch(paths.html.src, reload)
|
2019-04-07 17:41:13 -07:00
|
|
|
}
|
|
|
|
|
2019-04-07 22:00:52 -04:00
|
|
|
module.exports.style = style
|
|
|
|
module.exports.watch = watch
|