Trimmed down and incomplete version of Google's Material Design Lite gulpfile
'use strict';
// import path from 'path';
import gulp from 'gulp';
import del from 'del';
import runSequence from 'run-sequence';
import browserSync from 'browser-sync';
import gulpLoadPlugins from 'gulp-load-plugins';
// import pkg from './package.json';
const $ = gulpLoadPlugins();
const reload = browserSync.reload;
// Lint Javascript
gulp.task('lint', () =>
gulp.src('src/scripts/*.js')
.pipe($.eslint())
.pipe($.eslint.format())
);
// Optimize images
gulp.task('images', () =>
gulp.src('src/images/*')
.pipe($.cache($.imagemin({
progressive: true,
interlaced: true,
})))
.pipe(gulp.dest('dist/images'))
.pipe($.size({ title: 'images' }))
);
// Copy all files at the root level (src)
gulp.task('copy', () =>
gulp.src([
'src/*',
'!src/*.html',
], {
dot: true,
}).pipe(gulp.dest('dist'))
.pipe($.size({ title: 'copy' }))
);
// Run CSS through rucksack and cssnano
gulp.task('styles', () =>
gulp.src('src/styles/*.css')
.pipe($.sourcemaps.init())
.pipe($.rucksack())
.pipe($.cssnano())
.pipe($.size({ title: 'styles' }))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest('dist/styles'))
);
// Transpile, concatenate, and minify JavaScript.
gulp.task('scripts', () =>
gulp.src([
// Note: Since we are not using useref in the scripts build pipeline,
// you need to explicitly list your scripts here in the right order
// to be correctly concatenated
'./src/scripts/main.js',
// Other scripts
])
.pipe($.newer('.tmp/scripts'))
.pipe($.sourcemaps.init())
.pipe($.babel())
.pipe($.sourcemaps.write())
.pipe(gulp.dest('.tmp/scripts'))
.pipe($.concat('main.min.js'))
.pipe($.uglify({ preserveComments: 'some' }))
// Output files
.pipe($.size({ title: 'scripts' }))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('dist/scripts'))
);
// Scan HTML for assets & optimize them
gulp.task('html', () => {
return gulp.src('src/*.html')
.pipe($.useref({ searchPath: '{.tmp,src}' }))
// Remove any unused CSS
.pipe($.if('*.css', $.uncss({
html: [
'src/index.html',
],
// CSS Selectors for UnCSS to ignore
ignore: [],
})))
// Concatenate and minify styles
// In case you are still using useref build blocks
.pipe($.if('*.css', $.cssnano()))
// Minify HTML
.pipe(('*.html', $.htmlmin({
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeOptionalTags: true,
})))
// Output files
.pipe($.if('*.html', $.size({ title: 'html', showFiles: true })))
.pipe(gulp.dest('dist'));
});
// Clean output directory
gulp.task('clean', () => del(['.tmp', 'dist/*', '!dist/.git'], { dot: true }));
// Set default task sequence
gulp.task('default', ['clean'], cb =>
runSequence(
'styles',
['lint', 'html', 'scripts', 'images', 'copy'],
cb
)
);
// Watch files for changes & reload
gulp.task('serve', ['scripts', 'styles'], () => {
browserSync({
notify: false,
scrollElementMapping: ['main'],
server: ['src'],
port: 3000,
});
gulp.watch(['src/*.html'], reload);
gulp.watch(['src/styles/*.css'], ['styles', reload]);
gulp.watch(['src/scripts/*.js'], ['lint', 'scripts']);
gulp.watch(['src/images/*'], reload);
});
// Build and serve the output from the dist build
gulp.task('serve:dist', ['default'], () =>
browserSync({
notify: false,
scrollElementMapping: ['main', '.mdl-layout'],
server: 'dist',
port: 3001,
})
);