Skip to content

Instantly share code, notes, and snippets.

@iCodeForBananas
Created January 2, 2015 15:16
Show Gist options
  • Select an option

  • Save iCodeForBananas/14237e4fb18115f7fa73 to your computer and use it in GitHub Desktop.

Select an option

Save iCodeForBananas/14237e4fb18115f7fa73 to your computer and use it in GitHub Desktop.
'use strict';
var gulp = require('gulp');
var open = require('open');
var wiredep = require('wiredep').stream;
var nodemon = require('nodemon');
var livereload = require('gulp-livereload');
var htmlmin = require('gulp-htmlmin');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var ngtemplate = require('gulp-ngtemplate');
var ngAnnotate = require('gulp-ng-annotate');
var del = require('del');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var notify = require('gulp-notify');
var sass = require('gulp-sass');
var exec = require('child_process').exec;
var gutil = require('gulp-util');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer-core');
var sourcemaps = require('gulp-sourcemaps');
var imagemin = require('gulp-imagemin');
var jshint = require('gulp-jshint');
var stylish = require('jshint-stylish');
// Error notification methods
var beep = function() {
var os = require('os');
var file = 'gulp/error.wav';
if (os.platform() === 'linux') {
// linux
exec('aplay ' + file);
} else {
// mac
console.log('afplay ' + file);
exec('afplay ' + file);
}
};
var handleError = function(task) {
return function(err) {
beep();
notify.onError({
message: task + ' failed, check the logs..',
sound: false
})(err);
gutil.log(gutil.colors.bgRed(task + ' error:'), gutil.colors.red(err));
};
};
// Scripts
gulp.task('scripts', ['html', 'annotate'], function() {
return gulp.src('.tmp/**/*.js')
.pipe(sourcemaps.init())
.pipe(concat('app.js'))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('public/dist'))
.pipe(livereload());
});
// Annotate angular files for uglification
gulp.task('annotate', function() {
return gulp.src('assets/**/*.js')
.pipe(ngAnnotate())
.pipe(gulp.dest('.tmp'));
});
// HTML
gulp.task('html', function() {
return gulp.src('assets/**/*.html')
.pipe(htmlmin({
collapseWhitespace: true
}))
.pipe(ngtemplate({
module: 'serpProgress'
}))
.pipe(rename(function (path) {
path.basename += '.html';
}))
.pipe(gulp.dest('.tmp'));
});
// Inject Bower components
gulp.task('wiredep', function () {
return gulp.src('views/dashboard/index.html')
.pipe(wiredep({
directory: 'public/bower_components',
ignorePath: '../../public',
exclude: [/bootstrap-sass-official/, /bootstrap.js/, '/json3/', '/es5-shim/']
}))
.pipe(gulp.dest('public/dist'));
});
// Start server
gulp.task('nodemon', function () {
return nodemon({
script: './bin/www',
ext: 'html js',
ignore: ['assets']
})
.on('log', function (event) {
console.log(event.colour);
})
// opens browser on initial server start
.on('config:update', function () {
setTimeout(function () {
open('http://localhost:3000');
}, 500);
});
});
// Optimize asset images
gulp.task('optimize', function() {
return gulp.src('assets/**/*.{gif,jpg,png,svg}')
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
// png optimization
optimizationLevel: 3
}))
.pipe(gulp.dest('public/images/'));
});
// Linting
gulp.task('jshint', function() {
return gulp.src([
'assets/**/*.js'
])
.pipe(jshint())
.pipe(jshint.reporter(stylish))
.on('error', function() {
beep();
});
});
// Clean
gulp.task('clean', function () {
return del('.tmp/**/*.*');
});
// Sass
gulp.task('sass', function() {
return gulp.src('assets/**/*.scss')
.pipe(sass({
sourceComments: false,
outputStyle: 'compressed'
}))
.on('error', handleError('SASS'))
// generate .maps
.pipe(sourcemaps.write({
'includeContent': false,
'sourceRoot': '.'
}))
// autoprefixer
.pipe(sourcemaps.init({
'loadMaps': true
}))
// we don't serve the source files
// so include scss content inside the sourcemaps
.pipe(sourcemaps.write({
'includeContent': true
}))
// write sourcemaps to a specific directory
// give it a file and save
.pipe(gulp.dest('public/stylesheets'));
});
// Default task
gulp.task('default', function () {
gulp.start('build');
});
// Build
gulp.task('build', ['jshint', 'wiredep', 'scripts', 'sass', 'clean']);
// Watch
gulp.task('serve', ['nodemon'], function() {
livereload.listen();
gulp.watch([
'views/**/*.html',
'assets/**/*.*',
'bower.json'
],
[
'build'
],
function(event) {
console.log(1);
return gulp.src(event.path)
.pipe(livereload());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment