Skip to content

Instantly share code, notes, and snippets.

@alivelee
Last active August 29, 2015 14:27
Show Gist options
  • Save alivelee/19d4f5cec713e25a9f3d to your computer and use it in GitHub Desktop.
Save alivelee/19d4f5cec713e25a9f3d to your computer and use it in GitHub Desktop.
My web development gulpfile
var browserSync = require('browser-sync');
var del = require('del');
var gulp = require('gulp');
var pageSpeed = require('psi');
var plugins = require('gulp-load-plugins')();
var runSequence = require('run-sequence');
// Optimize images
gulp.task('images', function () {
return gulp.src('app"<%= asset_path('$1') %>"**/*')
.pipe(plugins.cache(plugins.imagemin({ progressive: true, interlaced: true })))
.pipe(gulp.dest('dist/images'))
.pipe(plugins.size({ title: 'images' }));
});
// Copy all files at the root level (app)
gulp.task('copy', function () {
return gulp.src([ 'app/*', '!app/*.html' ], { dot: true })
.pipe(gulp.dest('dist'))
.pipe(plugins.size({ title: 'Copy' }));
});
// Compile, prefix and minify styles
gulp.task('styles', function () {
return gulp.src([ 'app/styles/*.sass' ])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.sass({ indentedSyntax: true, precision: 10, onError: plugins.util.log }))
.pipe(plugins.autoprefixer({ browsers: [ 'ie >= 10', 'android >= 4.1' ] }))
.pipe(plugins.sourcemaps.write())
.pipe(gulp.dest('.tmp/styles'))
// Minify styles
.pipe(plugins.csso())
.pipe(gulp.dest('dist/styles'))
.pipe(plugins.size({ title: 'Styles' }));
});
// Compile and minify JavaScript files
gulp.task('scripts', function() {
return gulp.src('app/scripts/*.coffee')
.pipe(plugins.sourcemaps.init())
.pipe(plugins.coffee().on('error', plugins.util.log))
.pipe(plugins.concat('application.js'))
.pipe(plugins.sourcemaps.write())
.pipe(gulp.dest('.tmp/scripts'))
// Minify scripts
.pipe(plugins.uglify({ preserveComments: 'some' }))
.pipe(gulp.dest('dist/scripts'))
.pipe(plugins.size({ title: 'Scripts' }));
});
// Minify HTML
gulp.task('html', function () {
return gulp.src('app/**/*.html')
.pipe(plugins.minifyHtml())
.pipe(gulp.dest('dist'))
.pipe(plugins.size({ title: 'HTML' }));
});
// Clean output directory
gulp.task('clean', del.bind(null, ['.tmp', 'dist/*', '!dist/.git'], { dot: true }));
// Watch files for changes and reload
gulp.task('watch', [ 'scripts', 'styles' ], function() {
browserSync({ notify: false, server: ['.tmp', 'app'] });
gulp.watch(['app/**/*.html'], browserSync.reload);
gulp.watch(['app/styles/**/*.{sass,css}'], ['styles', browserSync.reload]);
gulp.watch(['app/scripts/**/*.{coffee,js}'], ['scripts', browserSync.reload]);
gulp.watch(['app"<%= asset_path('$1') %>"**/*'], browserSync.reload);
});
// Build and serve the output from the dist build
gulp.task('watch:dist', ['default'], function () {
browserSync({ notify: false, server: 'dist' });
});
// Build production files
gulp.task('build', [ 'clean' ], function (callback) {
runSequence('styles', 'scripts', [ 'html', 'images', 'copy' ], callback);
});
// Run PageSpeed Insights
gulp.task('pageSpeed', function (callback) {
// TODO: Update the URL.
pageSpeed.output('example.com', { strategy: 'mobile' }, callback);
});
// Make the default task watch
gulp.task('default', [ 'watch' ]);
{
"devDependencies": {
"browser-sync": "^1.3.0",
"del": "^1.1.0",
"gulp": "^3.8.5",
"gulp-autoprefixer": "^2.0.0",
"gulp-cache": "0.2.2",
"gulp-coffee": "*",
"gulp-concat": "*",
"gulp-csso": "^1.0.0",
"gulp-imagemin": "^2.0.0",
"gulp-load-plugins": "^0.8.0",
"gulp-minify-html": "0.1.5",
"gulp-sass": "^1.2.0",
"gulp-size": "^1.0.0",
"gulp-sourcemaps": "^1.3.0",
"gulp-uglify": "^1.0.1",
"gulp-util": "*",
"opn": "^1.0.0",
"psi": "^1.0.4",
"run-sequence": "^1.0.1"
},
"engines": {
"node": ">=0.10.0"
},
"private": true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment