|
'use strict'; |
|
|
|
// Include Gulp & Tools We'll Use |
|
var gulp = require('gulp'); |
|
var $ = require('gulp-load-plugins')(); |
|
var del = require('del'); |
|
var runSequence = require('run-sequence'); |
|
var browserSync = require('browser-sync'); |
|
var reload = browserSync.reload; |
|
var args = require('yargs').argv; |
|
var path = require('path'); |
|
var fs = require('fs'); |
|
|
|
// Grab bower_components directory |
|
if (fs.existsSync('./.bowerrc')) { |
|
var bower_config = JSON.parse(fs.readFileSync('./.bowerrc')); |
|
bower_path = bower_config.directory; |
|
} |
|
var bower_path = bower_path || './bower_components'; |
|
|
|
// |
|
// Config |
|
var config = { |
|
environment : (args.env || args.environment || 'development'), |
|
browsersync_proxy : 'dev.example.com', |
|
header : '/** DO NOT EDIT! This file is automatically generated by gulp.js **/', |
|
sass_path : 'src/sass', |
|
source_images_path : 'src/img', |
|
source_javascripts_path : 'src/js', |
|
source_fonts_path : 'src/fonts', |
|
source_html_path : 'src', |
|
http_path : 'dest', |
|
javascripts_path : 'dest/js', |
|
css_path : 'dest/css', |
|
images_path : 'dest/img', |
|
fonts_path : 'dest/fonts', |
|
html_path : 'dest', |
|
sass_includes : [ |
|
//bower_path + '/normalize-scss', |
|
//bower_path + '/bourbon/dist' |
|
] |
|
}; |
|
|
|
// Prepend sass path to includes |
|
config.sass_includes.unshift(config.sass_path); |
|
|
|
// Prepend the CWD to each SASS include (above) |
|
config.sass_includes = config.sass_includes.map(function(includePath) { |
|
return path.join(process.cwd(), includePath); |
|
}); |
|
|
|
// |
|
// BrowserSync |
|
gulp.task('browser-sync', function() { |
|
browserSync({ |
|
// server: { |
|
// baseDir: "./" + config.http_path |
|
// } |
|
// ... or ... |
|
proxy: config.browsersync_proxy, |
|
online: true, |
|
open: "external", |
|
host: config.browsersync_proxy |
|
}); |
|
|
|
gulp.watch(config.html_path+'/**/*.html', reload); |
|
// gulp.watch(config.css_path+'/**/*.css', reload); |
|
gulp.watch(config.javascripts_path+'/**/*.js', reload); |
|
// gulp.watch(config.images_path+'/**/*', reload); |
|
}); |
|
|
|
// |
|
// Images |
|
gulp.task('images', function() { |
|
var filter_svg = $.filter('**/*.svg'); |
|
var filter_other = $.filter('**/*.{png,gif,jpg,jpeg}'); |
|
|
|
return gulp.src(config.source_images_path+'/**/*') |
|
.pipe(filter_other) |
|
.pipe($.cache($.imagemin({progressive: true, interlaced: true}))) |
|
.pipe(filter_other.restore()) |
|
.pipe(filter_svg) |
|
.pipe($.cache($.svgmin())) |
|
.pipe(filter_svg.restore()) |
|
.pipe(gulp.dest(config.images_path)) |
|
.pipe(reload({ stream: true })) |
|
; |
|
}); |
|
|
|
gulp.task('images:watch', function() { |
|
return gulp.watch(config.source_images_path+'/**/*', ['images']).on('change', function(event) { |
|
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...'); |
|
}); |
|
}); |
|
|
|
// |
|
// SASS |
|
gulp.task('sass', function() { |
|
return gulp.src(['Gemfile', 'Gemfile.lock', config.sass_path+'/**/*.scss']) |
|
.pipe($.rubySass({ |
|
sourcemap: !(config.environment === 'production'), |
|
compass: false, |
|
quiet: true, |
|
bundleExec: true, |
|
style: (config.environment === 'production') ? 'compressed' : 'expanded', |
|
precision: 10, |
|
loadPath: config.sass_includes |
|
})) |
|
.pipe($.rename(function (currentPath) { |
|
if (currentPath.basename.indexOf('.min') === -1) { |
|
currentPath.basename += '.min'; |
|
} |
|
})) |
|
.pipe(gulp.dest(config.css_path)) |
|
; |
|
}); |
|
|
|
gulp.task('sass:watch', function() { |
|
return gulp.watch(config.sass_path+'/**/*.scss', ['sass']).on('change', function(event) { |
|
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...'); |
|
}); |
|
}); |
|
|
|
// |
|
// Styles |
|
gulp.task('styles', function() { |
|
return gulp.src(config.css_path+'/**/*.css') |
|
.pipe($.pleeease()) |
|
.pipe($.if(config.environment === 'production', $.minifyCss())) |
|
.pipe($.if(config.header, $.header(config.header+'\n'))) |
|
.pipe(gulp.dest(config.css_path)) |
|
.pipe(reload({ stream: true })) |
|
; |
|
}); |
|
|
|
gulp.task('styles:watch', function() { |
|
return gulp.watch(config.css_path+'/**/*.css', ['styles']).on('change', function(event) { |
|
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...'); |
|
}); |
|
}); |
|
|
|
gulp.task('styles:clean', del.bind(null, [config.css_path])); |
|
|
|
// |
|
// Scripts |
|
gulp.task('scripts', function() { |
|
return gulp.src(config.source_javascripts_path+'/**/*.js') |
|
.pipe($.if(config.environment === 'production', $.uglify())) |
|
.pipe($.if(config.environment === 'production', $.stripDebug())) |
|
.pipe($.header(config.header+'\n')) |
|
.pipe($.rename(function (currentPath) { |
|
if (currentPath.basename.indexOf('.min') === -1) { |
|
currentPath.basename += '.min'; |
|
} |
|
})) |
|
.pipe(gulp.dest(config.javascripts_path)) |
|
; |
|
}); |
|
|
|
gulp.task('scripts:watch', function() { |
|
return gulp.watch(config.source_javascripts_path+'/**/*.js', ['scripts']).on('change', function(event) { |
|
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...'); |
|
}); |
|
}); |
|
|
|
gulp.task('scripts:clean', del.bind(null, [config.javascripts_path])); |
|
|
|
// |
|
// HTML Fileinclude |
|
gulp.task('html', function() { |
|
return gulp.src(config.source_html_path+'/*.html') |
|
.pipe($.fileInclude({prefix: '@@', basepath: '@file' })) |
|
.pipe(gulp.dest(config.html_path)) |
|
; |
|
}); |
|
|
|
gulp.task('html:watch', function() { |
|
return gulp.watch(config.source_html_path+'/**/*.html', ['html']).on('change', function(event) { |
|
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...'); |
|
}); |
|
}); |
|
|
|
gulp.task('html:clean', del.bind(null, [config.html_path])); |
|
|
|
// |
|
// Fonts |
|
gulp.task('fonts', function() { |
|
return gulp.src(config.source_fonts_path+'/**/*.{eot,svg,ttf,woff}') |
|
.pipe(gulp.dest(config.fonts_path)) |
|
; |
|
}); |
|
|
|
gulp.task('fonts:clean', del.bind(null, [config.fonts_path])); |
|
|
|
// |
|
// Main Tasks |
|
|
|
// Clean |
|
gulp.task('clean', ['styles:clean']); |
|
|
|
// Watch |
|
gulp.task('watch', ['images:watch', 'sass:watch', 'styles:watch', 'scripts:watch', 'html:watch']); |
|
|
|
// Build |
|
gulp.task('build', function(cb) { |
|
console.log('---'); |
|
console.log('Building project using environment: ' + config.environment); |
|
console.log('---'); |
|
runSequence('clean', ['fonts', 'images', 'scripts'], 'sass', 'styles', 'html', cb); |
|
}); |
|
|
|
// Default |
|
gulp.task('default', function(cb) { |
|
runSequence('build', 'watch', 'browser-sync', cb); |
|
}); |