/
template-cache.coffee
76 lines (65 loc) · 2.05 KB
/
template-cache.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# =====
getGlob = (loc, type, lang) ->
config.glob.src[loc].client[type][lang]
glob =
views:
rb: getGlob 'rb', 'views', 'html'
app: getGlob 'app', 'views', 'html'
# helpers
# =======
getAppOrRb = (base, loc, type) -> # base = file.base from buffer
return if not base
return 'rb' if base.indexOf(config[loc].rb.client[type].dir) isnt -1
'app'
# streams
# =======
addToDistPath = -> # add 'views/' for app and 'rapid-build/views/' for rb
transform = (file, cb) ->
appOrRb = getAppOrRb file.base, 'src', 'views'
dirName = config.dist[appOrRb].client.views.dirName
dirName = path.join config.rb.prefix.distDir, dirName if appOrRb is 'rb'
relPath = path.join dirName, file.relative
modPath = path.join file.base, relPath
file.path = modPath
cb null, file
es.map transform
# tasks
# =====
runTask = (src, dest, file, isProd) ->
defer = q.defer()
opts = {}
opts.root = '/' if config.angular.templateCache.useAbsolutePaths
opts.module = config.angular.moduleName
gulp.src src
.pipe addToDistPath()
.pipe gulpif isProd, minifyHtml empty:true, conditionals:true, ssi:true
.pipe templateCache file, opts
.pipe gulp.dest dest
.on 'end', ->
console.log "created #{file}".yellow
defer.resolve()
defer.promise
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()