kfwerf
8/19/2014 - 4:24 PM

GulpOptimizer

#

gulp = require 'gulp'
coffee = require 'gulp-coffee'
sass = require 'gulp-sass'
util = require 'gulp-util'
watch = require 'gulp-watch'
imagemin = require 'gulp-imagemin'
pngcrush = require 'imagemin-pngcrush'
concat = require 'gulp-concat'
compass = require 'gulp-compass'
uglify = require 'gulp-uglify'
css = require 'gulp-css'
html = require 'gulp-minify-html'
templateCache = require 'gulp-angular-templatecache'
replace = require 'gulp-replace'


# 

# jsfiles = require './jsfiles.json'
devpath = '../../dev'
buildpath = '..'
paths =
	directories:
		base: devpath
		src:  [
			"/fonts/"
			"/locale/"
			"/php/"
		]
		dest: "#{buildpath}"
	files:
		src: [
			# "#{devpath}/footer.php"
			"#{devpath}/functions.php"
			"#{devpath}/header.php"
			"#{devpath}/content-footer.php"
			"#{devpath}/content-header.php"
			"#{devpath}/content-modals.php"
			"#{devpath}/content-sidenav.php"
			"#{devpath}/index.php"
			"#{devpath}/jsfiles.json"
			"#{devpath}/manifest.appcache"
			# "#{devpath}/style.css"
		]
		dest: "#{buildpath}/"
	# coffee: 
	# 	src: ["#{copiedpath}/coffee/**/*.coffee", "#{copiedpath}/coffee/*.coffee"]
	# 	dest: "#{buildpath}/js"
	# compass: 
	# 	src: ["#{copiedpath}/sass/**/*.scss", "#{copiedpath}/sass/*.scss"]
	# 	dest: "#{buildpath}/css"
	images: 
		src: "#{devpath}/img/**"
		dest: "#{buildpath}/img"
	css: 
		src: ["#{devpath}/css/app.css", "#{devpath}/css/mini.css"]
		dest: "#{buildpath}/css/"
	html:
		src: "#{devpath}/views/**"
		dest: "#{buildpath}/views"
	templates: 
		src: "#{devpath}/views/**/*.html"
		dest: "#{buildpath}/views"

objTasks = {}


#

gulp.task 'copy', ->
	for strKey, path of paths.directories.src
		gulp.src "#{paths.directories.base}#{path}**"
			.pipe gulp.dest "#{buildpath}#{path}"
	
	gulp.src paths.files.src
			.pipe gulp.dest paths.files.dest

gulp.task 'images', ->
	gulp.src paths.images.src
		.pipe imagemin(
			progressive: true
			svgoPlugins: [ removeViewBox: false ]
			use: [ pngcrush() ]
		)
		.pipe gulp.dest paths.images.dest

gulp.task 'css', ->
	console.log paths.css.src, paths.css.dest
	gulp.src paths.css.src
		.pipe css()
		.pipe gulp.dest paths.css.dest

gulp.task 'loader', ->
	jsfiles = require "#{buildpath}/jsfiles.json"
	for index, path of jsfiles.development.loader
		jsfiles.development.loader[index] = "#{devpath}/#{path}"

	gulp.src jsfiles.development.loader
		.pipe uglify()
		.pipe concat 'loader.js' 
		.pipe gulp.dest "#{buildpath}/js/"

gulp.task 'components', ->
	jsfiles = require "#{buildpath}/jsfiles.json"
	for index, path of jsfiles.development.components
		jsfiles.development.components[index] = "#{devpath}/#{path}"

	gulp.src "#{devpath}/js/components/modernizr/modernizr.js"
		.pipe uglify()
		.pipe concat 'modernizr.js' 
		.pipe gulp.dest "#{buildpath}/js/"

	gulp.src jsfiles.development.components
		.pipe uglify()
		.pipe concat 'components.js' 
		.pipe gulp.dest "#{buildpath}/js/"

gulp.task 'app', ->
	jsfiles = require "#{buildpath}/jsfiles.json"
	for index, path of jsfiles.development.app
		jsfiles.development.app[index] = "#{devpath}/#{path}"
	
	gulp.src jsfiles.development.app
		.pipe replace "angular.module('anybetApp', [", "angular.module('anybetApp', [ 'templates', "
		.pipe uglify()
		.pipe concat 'app.js'
		.pipe gulp.dest "#{buildpath}/js/"

gulp.task 'all', ->
	gulp.src ["#{buildpath}/js/components.js", "#{buildpath}/js/templates.js", "#{buildpath}/js/app.js"]
		.pipe uglify()
		.pipe concat 'all.js'
		.pipe gulp.dest "#{buildpath}/js/"

gulp.task 'templates', ->
	gulp.src paths.templates.src
		.pipe templateCache 'templates.js',
			standalone: true
			root: '/views/'
		.pipe gulp.dest "#{buildpath}/js/"

	# gulp.src "#{buildpath}/js/app.js"
	# 	.pipe replace '', ''
	# 	.pipe gulp.dest "" 

gulp.task 'html', ->
	gulp.src paths.html.src
		.pipe html( empty: true, quotes: true )
		.pipe gulp.dest paths.html.dest

gulp.task 'js', [ 'components', 'app', 'templates', 'all' ], ->
	console.log 'Js Build'

gulp.task 'build-app', [ 'copy', 'css', 'js', 'html' ], ->
	console.log 'App build'

gulp.task 'build-all', [ 'build-app', 'images' ], ->
	console.log 'Build completed'