Skip to content

Instantly share code, notes, and snippets.

@tothandras
Created February 10, 2015 17:56
Show Gist options
  • Save tothandras/980c6ce650403aadc8ab to your computer and use it in GitHub Desktop.
Save tothandras/980c6ce650403aadc8ab to your computer and use it in GitHub Desktop.
########################################################################################################################
# Gulp
########################################################################################################################
gulp = require('gulp')
# Load all gulp plugins and utilities
$ = require('gulp-load-plugins')(pattern: ['gulp-*', 'log', 'del', 'nib', 'jeet', 'main-bower-files'])
$.cleancss = new (require("less-plugin-clean-css"))
advanced: true
$.autoprefix = new (require('less-plugin-autoprefix'))
browsers: ["last 2 versions"]
########################################################################################################################
# Configurable paths
########################################################################################################################
paths =
src: 'src'
app: 'app'
build: 'dist'
bower: 'bower'
scripts: 'scripts'
views: 'views'
styles: 'styles'
fonts: 'fonts'
images: 'images'
libraries: 'lib'
paths.app = "#{paths.src}/#{paths.app}"
paths.libraries = "#{paths.src}/#{paths.libraries}"
files =
index: [
'index.html'
'index.jade'
]
templates: [
'**/*.tpl.html'
'**/*.tpl.jade'
]
styles: [
'**/*.css'
'**/main.less'
'**/*.less'
]
coffee: [
'**/*.module.coffee'
'**/*.coffee'
]
js: [
'**/*.js'
'config.js'
]
config: 'config.json'
images: 'images/**/*'
fonts: 'fonts/**/*'
########################################################################################################################
# Tasks
########################################################################################################################
# Use --production for release build
IS_RELEASE_BUILD = $.util.env.production?
CDN = $.util.env.cdn?
# Clean build folder
gulp.task 'clean', ->
$.del [paths.build, "#{paths.app}/config.js"], (err) -> if err? then $.util.log(err)
# Lint CoffeeScript (rules can be change in coffeescript.json)
gulp.task 'lint', ->
gulp.src files.coffee, cwd: paths.app
.pipe $.coffeelint()
.pipe $.coffeelint.reporter()
# Convert CoffeeScript classes to AngularJS modules
# Compile CoffeeScript files and copy the JavaScript files to build folder
gulp.task 'scripts', ['lint', 'config'], ->
scripts = files.coffee.concat(files.js)
gulp.src scripts, cwd: paths.app
.pipe $.changed paths.scripts, cwd: paths.build
.pipe $.flatten()
.pipe $.if '*.coffee' and not IS_RELEASE_BUILD, $.sourcemaps.init()
.pipe $.if '*.coffee', $.ngClassify()
.pipe $.if '*.coffee', $.coffee()
.pipe $.if '*.coffee' and not IS_RELEASE_BUILD, $.sourcemaps.write()
.pipe $.if IS_RELEASE_BUILD, $.ngAnnotate()
.pipe $.if IS_RELEASE_BUILD, $.uglify()
.pipe $.if IS_RELEASE_BUILD, $.concat('page.min.js')
.pipe $.if IS_RELEASE_BUILD, $.rev()
.pipe gulp.dest paths.scripts, cwd: paths.build
# Compile Less files and copy CSS files to build folder
gulp.task 'styles', ->
gulp.src files.styles, cwd: paths.app
.pipe $.changed paths.styles, cwd: paths.build
.pipe $.flatten()
.pipe $.if '*.less', $.concat('styles.less')
.pipe $.if '*.less', $.less
paths: paths.src
plugins: [$.autoprefix, $.cleancss]
.pipe $.if IS_RELEASE_BUILD, $.concat('styles.css')
.pipe $.if IS_RELEASE_BUILD, $.rev()
.pipe gulp.dest paths.styles, cwd: paths.build
# Compile Jade templates and copy HTML files to paths.views
gulp.task 'templates', ->
gulp.src files.templates, cwd: paths.app
.pipe $.changed paths.views, cwd: paths.build
.pipe $.flatten()
.pipe $.if '*.jade', $.jade pretty: true
.pipe $.if IS_RELEASE_BUILD, $.angularTemplatecache
module: 'app'
root: paths.views
.pipe $.if IS_RELEASE_BUILD, $.rev()
.pipe gulp.dest paths.views, cwd: paths.build
gulp.task 'fonts', ->
gulp.src files.fonts, cwd: paths.src
.pipe gulp.dest paths.fonts, cwd: paths.build
gulp.task 'images', ->
gulp.src files.images, cwd: paths.src
.pipe gulp.dest paths.images, cwd: paths.build
gulp.task 'config', ->
gulp.src files.config, cwd: paths.app
.pipe $.ngConfig 'app', createModule: false, wrap: true, constants: {PRODUCTION: IS_RELEASE_BUILD}
.pipe gulp.dest paths.app
# Install bower dependencies
gulp.task 'bower', -> $.bower()
# Compile index and inject css and scripts tags
gulp.task 'index', ['bower', 'styles', 'scripts', 'templates'], ->
scripts = gulp.src ["#{paths.scripts}/*.module.js", "#{paths.scripts}/*.js",
"#{paths.views}/*.js"], cwd: paths.build
styles = gulp.src "#{paths.styles}/*.css", cwd: paths.build, read: false
# Copy bower libraries to paths.bower
bower = gulp.src $.mainBowerFiles()
.pipe $.if IS_RELEASE_BUILD, $.ngAnnotate()
.pipe $.if IS_RELEASE_BUILD, $.uglify()
.pipe $.if '*.js', $.concat('libs.min.js')
.pipe $.if '*.css', $.concat('libs.min.css')
.pipe $.if IS_RELEASE_BUILD, $.rev()
.pipe gulp.dest paths.bower, cwd: paths.build
# Inject tags
gulp.src files.index, cwd: paths.app
.pipe $.inject bower, addRootSlash: false, name: 'bower'
.pipe $.inject scripts, addRootSlash: false
.pipe $.inject styles, addRootSlash: false
.pipe $.if '*.jade', $.jade pretty: not IS_RELEASE_BUILD
.pipe gulp.dest paths.build
prepend = (string, array) ->
returnArray = []
if Array.isArray(array)
for item in array
returnArray.push("#{string}/#{item}")
return returnArray
return "#{string}/#{array}"
# Watch for file changes
gulp.task 'watch', ->
if not IS_RELEASE_BUILD
gulp.watch prepend(paths.app, files.index), ['index']
gulp.watch prepend(paths.app, files.templates), ['templates']
gulp.watch prepend(paths.app, files.styles), ['index']
gulp.watch prepend(paths.app, files.coffee), ['index']
gulp.watch prepend(paths.app, files.js), ['index']
gulp.task 'default', ['index', 'templates', 'fonts']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment