Skip to content

Instantly share code, notes, and snippets.

@alireza-ahmadi
Last active August 29, 2015 14:12
Show Gist options
  • Save alireza-ahmadi/0b88127c5b4961f7ec1f to your computer and use it in GitHub Desktop.
Save alireza-ahmadi/0b88127c5b4961f7ec1f to your computer and use it in GitHub Desktop.
gulp = require 'gulp'
sourcestream = require 'vinyl-source-stream'
buffer = require 'vinyl-buffer'
util = require 'gulp-util'
sourcemaps = require 'gulp-sourcemaps'
uglify = require 'gulp-uglify'
browserify = require 'browserify'
livereload = require 'gulp-livereload'
minify = require 'gulp-minify-css'
express = require 'express'
sass = require 'gulp-sass'
path = require 'path'
del = require 'del'
mkdirp = require 'mkdirp'
fs = require 'fs'
# Utilities
source = (addr) ->
if addr?
return ('./app/' + addr)
else
return './app'
destination = (addr) ->
if addr?
return ('./build/' + addr)
else
return './build'
errorHandler = (error) ->
util.log (util.colors.red 'ERROR:'), error.message
this.emit 'end'
return
# Clean previously generated files
gulp.task 'clean', (cb) ->
del.sync destination()
return
# Copy HTML files
gulp.task 'html', ->
gulp.src source '**/*.html'
.pipe gulp.dest destination()
# Copy HTML files
gulp.task 'images', ->
gulp.src source 'images/**/*.*'
.pipe gulp.dest destination 'images'
# Compile SASS files
gulp.task 'sass', ->
gulp.src source 'styles/all.scss'
.pipe sourcemaps.init()
.pipe sass()
.on 'error', errorHandler
.pipe sourcemaps.write './'
.pipe gulp.dest destination 'styles'
# Create fonts folder
gulp.task 'stylesFolders', ->
mkdirp.sync (destination 'styles') if !fs.existsSync destination 'styles'
mkdirp.sync destination 'styles/fonts'
return
# Copy fonts
gulp.task 'fonts', ['stylesFolders'], ->
gulp.src source 'styles/fonts/**/*.*'
.pipe gulp.dest destination 'styles/fonts'
# Browserify JS files
gulp.task 'browserify', ->
settings =
entries: source 'js/all.js'
debug: true
bundler = browserify settings
bundler.bundle()
.on 'error', errorHandler
.pipe sourcestream 'all.js'
.pipe buffer()
.pipe sourcemaps.init loadMaps : true
.pipe uglify()
.pipe sourcemaps.write './'
.pipe gulp.dest destination 'js'
# Copy bower files
gulp.task 'bower', ->
gulp.src source 'lib/**/*.*'
.pipe gulp.dest destination 'lib'
# Run webserver
gulp.task 'webserver', ->
app = express()
app.use express.static destination()
app.listen 7300
# Watch changes
gulp.task 'watch', ['webserver'], ->
lr = livereload
lr.listen()
gulp.watch (source '**/*.html'), ['html']
gulp.watch (source 'images/**/*.*'), ['images']
gulp.watch (source 'styles/**/*.scss'), ['sass']
gulp.watch (source 'js/**/*.js'), ['browserify']
gulp.watch (destination '**/*.*'), (file) ->
lr.changed file.path
return
return
gulp.task 'build', ['clean', 'html', 'sass', 'browserify', 'images', 'bower', 'fonts']
gulp.task 'default', ['build', 'watch']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment