Last active
August 29, 2015 14:07
-
-
Save jeremyfuksa/5770b20460761f697a86 to your computer and use it in GitHub Desktop.
My standard gulpfile.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var gulp = require('gulp'), | |
| $ = require('gulp-load-plugins')(), | |
| beep = require('beepbeep'), | |
| gutil = require('gulp-util'), | |
| browserSync = require('browser-sync'), | |
| reload = browserSync.reload; | |
| var paths = { | |
| 'devassets': 'assets/', | |
| 'devroot': './', | |
| 'prodassets': 'dist/assets/', | |
| 'prodroot': 'dist/' | |
| }; | |
| var onError = function(err) { | |
| beep([0,0,0]); | |
| gutil.log(gutil.colors.red(err)); | |
| }; | |
| // -------------------------------------------------------- | |
| // Utility Tasks | |
| // -------------------------------------------------------- | |
| // Delete 'dist' directory | |
| gulp.task('clean', function(){ | |
| del([paths.prodroot], function(err){ | |
| if (err) { | |
| gutil.log(gutil.colors.red(err)); | |
| } else { | |
| gutil.log(gutil.colors.green(paths.prodroot + ' directory deleted.')); | |
| } | |
| }); | |
| }); | |
| // Compile SASS files | |
| gulp.task('sass', function(){ | |
| return(gulp.src(paths.devassets + 'sass/*.scss')) | |
| .pipe($.plumber({ | |
| errorHandler: onError | |
| })) | |
| .pipe($.rubySass()) | |
| .pipe(gulp.dest(paths.devassets + 'css')) | |
| .pipe(reload({stream: true})); | |
| }); | |
| // Validate all Javascript | |
| gulp.task('jshint', function(){ | |
| return(gulp.src(paths.devassets + 'js/*.js')) | |
| .pipe($.plumber({ | |
| errorHandler: onError | |
| })) | |
| .pipe($.jshint()) | |
| .pipe(reload({stream: true})); | |
| }); | |
| // Compile HTML files | |
| gulp.task('html', function(){ | |
| return(gulp.src(paths.devroot + 'templates/*.html')) | |
| .pipe($.fileInclude({ | |
| prefix: '@@', | |
| basepath: '@file' | |
| })) | |
| .pipe(gulp.dest(paths.devroot)) | |
| .pipe(reload({stream: true})); | |
| }); | |
| // -------------------------------------------------------- | |
| // Build Packaging Scripts | |
| // -------------------------------------------------------- | |
| // Package all CSS files | |
| gulp.task('package-css', function(){ | |
| return(gulp.src(paths.devassets + 'css/*.css')) | |
| .pipe($.plumber({ | |
| errorHandler: onError | |
| })) | |
| .pipe($.autoprefixer({ | |
| browsers: ['last 2 versions'] | |
| })) | |
| .pipe(gulp.dest(paths.prodassets + 'css')) | |
| .pipe($.csso()) | |
| .pipe($.rename({ extname: '.min.css'})) | |
| .pipe(gulp.dest(paths.prodassets + 'css')); | |
| }); | |
| // Package all Javascript | |
| gulp.task('package-scripts', function(){ | |
| var nonMinified = $.filter(['*', '!*.min.js']); | |
| return(gulp.src(paths.devassets + 'assets/js/**')) | |
| .pipe($.plumber({ | |
| errorHandler: onError | |
| })) | |
| .pipe(gulp.dest(paths.prodassets + 'js')) | |
| .pipe(nonMinified) | |
| .pipe($.uglify()) | |
| .pipe($.rename({ extname: '.min.js'})) | |
| .pipe(gulp.dest(paths.prodassets + 'js')) | |
| }); | |
| // Optimize and package images | |
| gulp.task('package-images', function(){ | |
| var pngcrush = require('imagemin-pngcrush'); | |
| return(gulp.src(paths.devassets + 'images/*')) | |
| .pipe($.plumber({ | |
| errorHandler: onError | |
| })) | |
| .pipe($.imagemin({ | |
| progressive: true, | |
| use: [pngcrush()] | |
| })) | |
| .pipe(gulp.dest(paths.prodassets + 'images')); | |
| }); | |
| // Package all root files | |
| gulp.task('package-files', function(){ | |
| return(gulp.src(paths.devroot + '*.*')) | |
| .pipe(gulp.dest(paths.prodroot)) | |
| }); | |
| // -------------------------------------------------------- | |
| // Keyword arguments that should be used with 'gulp' | |
| // -------------------------------------------------------- | |
| gulp.task('build', ['package-images', 'package-scripts', 'package-css', 'package-files'], function(){ gutil.log(gutil.colors.green('Build complete.')); }); | |
| gulp.task('watch', ['serve'], function(){ | |
| gulp.watch(paths.devassets + 'sass/**/*.scss', ['sass']); | |
| gulp.watch(paths.devassets + 'js/*.js', ['jshint']); | |
| gulp.watch(paths.devroot + 'templates/**/*.html', ['html']); | |
| }); | |
| gulp.task('serve', function(){ | |
| browserSync({ | |
| server: { | |
| baseDir: paths.devroot | |
| } | |
| }); | |
| gulp.watch( | |
| [ | |
| paths.devroot + '*.html', | |
| paths.devassets + 'css/**/*.css', | |
| paths.devassets + 'js/*.js' | |
| ], | |
| { cwd: paths.devroot }, | |
| reload | |
| ); | |
| }) | |
| gulp.task('default', ['clean'], function(){ gulp.start('build'); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment