forked from maxparm/node-underscorify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (49 loc) · 1.44 KB
/
index.js
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
// Generated by CoffeeScript 1.6.3
var defaultOptions, minify, path, through, transform, _;
through = require('through');
_ = require("underscore");
minify = require("html-minifier").minify;
path = require('path');
defaultOptions = {
extensions: ['tpl', 'html'],
templateSettings: {},
htmlMinifier: false,
requires: []
};
transform = function(options) {
options = _.defaults(options || {}, defaultOptions);
return function(file) {
var buffer, isTemplate;
isTemplate = _.some(options.extensions, function(ext) {
return path.extname(file) === '.' + ext;
});
if (!isTemplate) {
return through();
}
buffer = "";
return through(function(chunk) {
return buffer += chunk.toString();
}, function() {
var compiled, html, jst;
compiled = "";
if (options.requires.length) {
compiled = _.reduce(options.requires, function(s, r) {
if (r.variable && r.module) {
s += r.variable + ' = require("' + r.module + '");' + "\n";
}
return s;
}, '');
}
html = buffer.toString();
if (options.htmlMinifier) {
html = minify(html, options.htmlMinifier);
}
jst = _.template(html, void 0, options.templateSettings).source;
compiled += "module.exports = " + jst + ";\n";
this.queue(compiled);
return this.queue(null);
});
};
};
module.exports = transform();
module.exports.transform = transform;