forked from maxparm/node-underscorify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
48 lines (42 loc) · 1.43 KB
/
index.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
through = require('through')
_ = require("underscore")
minify = require("html-minifier").minify
path = require('path')
defaultOptions =
extensions: ['tpl', 'html']
templateSettings: {}
htmlMinifier: false
requires: []
transform = (options) ->
options = _.defaults(options || {}, defaultOptions)
return (file) ->
isTemplate = _.some options.extensions, (ext) ->
path.extname(file) is '.'+ext
return through() if not isTemplate
buffer = ""
return through(
(chunk) ->
buffer += chunk.toString()
,
() ->
compiled = "";
if options.requires.length
compiled = _.reduce(options.requires, (s, r) ->
if r.variable and r.module
s += r.variable + ' = require("' + r.module + '");' + "\n"
s
, '')
html = buffer.toString()
if options.htmlMinifier
html = minify(html, options.htmlMinifier)
jst = _.template(
html,
undefined,
options.templateSettings
).source;
compiled += "module.exports = " + jst + ";\n";
@queue(compiled)
@queue(null)
)
module.exports = transform()
module.exports.transform = transform