-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (54 loc) · 1.58 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
58
59
60
61
62
var through = require('through');
var compiler = require('can-compile');
var path = require('path');
var xtend = require('xtend');
var generateRequires = require('./lib/generateRequires');
var templateRe = /\.(mustache|ejs|stache)$/;
var defaults = {
normalizer: function(filename) {
return path.relative(__dirname, filename);
},
version: '2.2.4',
requirePaths: {
'jquery': 'jquery',
'can': 'can',
'ejs': 'can/view/ejs/ejs',
'stache': 'can/view/stache/stache',
'mustache': 'can/view/mustache/mustache'
}
};
function isTemplate(file) {
return templateRe.test(file);
}
function compile(file, options) {
if(!isTemplate(file)) {
return through();
}
var type = path.extname(file).slice(1);
var config = xtend({}, defaults, options);
var compilerConfig = {
paths: config.paths,
normalizer: config.normalizer,
version: config.version,
wrapper: generateRequires(config.version, type, config.requirePaths) + 'module.exports={{{content}}}'
};
var buffer = "";
return through(function(chunk){
buffer += chunk.toString();
}, function() {
compiler([file], compilerConfig, function(err, output){
if(err) {
this.emit('error', err);
return;
}
this.queue(output);
this.queue(null);
}.bind(this));
});
}
compile.configure = function(rootOpts) {
return function(file, opts) {
return compile(file, xtend({}, rootOpts, opts));
};
};
module.exports = compile;