Skip to content

Instantly share code, notes, and snippets.

@bikeonastick
Created January 21, 2015 06:45
Show Gist options
  • Save bikeonastick/1d98558466cbfd32e8c8 to your computer and use it in GitHub Desktop.
Save bikeonastick/1d98558466cbfd32e8c8 to your computer and use it in GitHub Desktop.
gulpfile.js for sample project
// Load plugins
var gulp = require('gulp'),
sass = require('gulp-ruby-sass'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
connect = require('gulp-connect'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
livereload = require('gulp-livereload'),
del = require('del');
// gulp connect
gulp.task('serve', function() {
connect.server({
root: './dist',
port: 9999,
livereload: true
});
});
// Styles
gulp.task('styles', function() {
return gulp.src('src/styles/main.scss')
.pipe(sass({ style: 'expanded', }))
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(gulp.dest('dist/styles'))
.pipe(rename({ suffix: '.min' }))
.pipe(minifycss())
.pipe(gulp.dest('dist/styles'))
.pipe(notify({ message: 'Styles task complete' }));
});
// Scripts
gulp.task('scripts', function() {
return gulp.src('src/scripts/**/*.js')
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'))
.pipe(concat('main.js'))
.pipe(gulp.dest('dist/scripts'))
.pipe(rename({ suffix: '.min' }))
.pipe(uglify())
.pipe(gulp.dest('dist/scripts'))
.pipe(notify({ message: 'Scripts task complete' }));
});
// html
gulp.task('html', function() {
return gulp.src('src/**/*.html')
.pipe(connect.reload())
.pipe(gulp.dest('dist'))
.pipe(notify({ message: 'HTML task complete' }));
});
// Images
gulp.task('images', function() {
return gulp.src('src/images/**/*')
.pipe(connect.reload())
.pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
.pipe(gulp.dest('dist/images'))
.pipe(notify({ message: 'Images task complete' }));
});
// Clean
gulp.task('clean', function(cb) {
del(['dist/styles', 'dist/scripts', 'dist/images', 'dist/**/*.html'], cb)
});
// Default task
gulp.task('default', ['clean'], function() {
gulp.start('watch', 'styles', 'scripts', 'images', 'html' ,'serve');
});
// Watch
gulp.task('watch', function() {
// Watch .scss files
gulp.watch('src/styles/**/*.scss', ['styles']);
// Watch .js files
gulp.watch('src/scripts/**/*.js', ['scripts']);
// Watch image files
gulp.watch('src/images/**/*', ['images']);
// Watch html files
gulp.watch('src/**/*.html', ['html']);
// Create LiveReload server
livereload.listen();
// Watch any files in dist/, reload on change
gulp.watch(['dist/**']).on('change', livereload.changed);
});
@bikeonastick
Copy link
Author

totally adapted (read: stolen) from here: http://markgoodyear.com/2014/01/getting-started-with-gulp/

@markleppke
Copy link

Thanks, This should help. I should have mentioned that gulp-connect comes with live reload, you may be able to pull it out and leverage just the gulp-connect add-on (https://www.npmjs.com/package/gulp-connect)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment