Skip to content
Permalink
Browse files

feat(watch): add support for watching the template cache

  • Loading branch information
jyounce committed Jun 5, 2015
1 parent dc48e5b commit b6237f21bc221f8e6a3a4f455f344c5201d1c4ae
Showing with 31 additions and 8 deletions.
  1. +2 −1 helpers/log.coffee
  2. +14 −4 tasks/minify/template-cache.coffee
  3. +15 −3 tasks/watch/watch.coffee
@@ -5,5 +5,6 @@ module.exports =
else
console.log JSON.stringify v, null, '\t'

watch: (taskName, file) ->
watch: (taskName, file, opts={}) ->
taskName = opts.logTaskName or taskName
console.log "#{taskName} #{file.event}: #{file.path}".yellow
@@ -1,10 +1,11 @@
module.exports = (gulp, config) ->
module.exports = (gulp, config, watchFile={}) ->
q = require 'q'
path = require 'path'
es = require 'event-stream'
gulpif = require 'gulp-if'
minifyHtml = require 'gulp-minify-html'
templateCache = require 'gulp-angular-templatecache'
forWatchFile = !!watchFile.path

# globs
# =====
@@ -52,15 +53,24 @@ module.exports = (gulp, config) ->
defer.resolve()
defer.promise

# register task
# =============
gulp.task "#{config.rb.prefix.task}template-cache", ->
run = ->
isProd = config.env.name is 'prod'
file = if isProd then 'min' else 'main'
file = config.fileName.views[file]
dest = config.dist.rb.client.scripts.dir
src = [ glob.views.rb, glob.views.app ]
runTask src, dest, file, isProd

runSingle = -> # todo: optimize for one file
run()

runMulti = ->
run()

# register task
# =============
return runSingle() if forWatchFile
gulp.task "#{config.rb.prefix.task}template-cache", -> runMulti()



@@ -15,6 +15,7 @@ module.exports = (gulp, config, browserSync) ->
image: require "#{config.req.tasks}/copy/copy-images"
js: require "#{config.req.tasks}/copy/copy-js"
less: require "#{config.req.tasks}/compile/less"
tCache: require "#{config.req.tasks}/minify/template-cache"
buildSpa: ->
gulp.start "#{config.rb.prefix.task}watch-build-spa"
browserSync: ->
@@ -87,21 +88,23 @@ module.exports = (gulp, config, browserSync) ->
tasks[taskName] gulp, config, file

addAndUnlinkTask = (taskName, file, opts) ->
return changeTask taskName, file if opts.taskOnly
tasks[taskName](gulp, config, file).then ->
return tasks.browserSync() if opts.bsReload
tasks.buildSpa()

events = (file, taskName, opts={}) -> # add, change, unlink
log.watch taskName, file
log.watch taskName, file, opts
return tasks.buildSpa() if taskName is 'spa.html'
return if not file
return if not file.event
return if not file.path
return if not taskName
file = addFileProps file, opts
unlinkTaskName = if opts.taskOnly then taskName else 'clean'
switch file.event
when 'change' then changeTask taskName, file
when 'unlink' then addAndUnlinkTask 'clean', file, opts
when 'unlink' then addAndUnlinkTask unlinkTaskName, file, opts
when 'add' then addAndUnlinkTask taskName, file, opts

# watches
@@ -117,6 +120,15 @@ module.exports = (gulp, config, browserSync) ->
defer.resolve()
defer.promise

# html watch (handle angular template cache)
# ==========================================
htmlWatch = ->
if config.angular.templateCache.dev.enable
createWatch glob.src.html, 'tCache',
lang:'html', srcType:'views', taskOnly:true, logTaskName:'template cache'
else
createWatch glob.src.html, 'html', lang:'html', srcType:'views', bsReload:true

# register task
# =============
gulp.task "#{config.rb.prefix.task}watch", ->
@@ -135,7 +147,7 @@ module.exports = (gulp, config, browserSync) ->
createWatch glob.src.js[0], 'js', lang:'js', srcType:'scripts'
createWatch glob.src.js[1], 'js', lang:'js', srcType:'scripts', loc:'server'
# views
createWatch glob.src.html, 'html', lang:'html', srcType:'views', bsReload:true
htmlWatch()
# spa
# createWatch config.src.rb.client.spa.path, 'spa.html', lang:'spa.html'
]).done -> defer.resolve()

0 comments on commit b6237f2

Please sign in to comment.
You can’t perform that action at this time.