-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (53 loc) · 1.65 KB
/
index.js
File metadata and controls
61 lines (53 loc) · 1.65 KB
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
// Generated by CoffeeScript 1.8.0
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(instance_opts) {
instance_opts = _.defaults(instance_opts || {}, defaultOptions);
return function(file, opts) {
var buffer, isTemplate, options;
if (opts && typeof opts['extensions'] === 'string') {
opts['extensions'] = opts['extensions'].split(',');
}
options = _.defaults(opts || {}, instance_opts);
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 += 'var ' + 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;