forked from zkat/can.viewify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (36 loc) · 960 Bytes
/
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
"use strict";
var through = require("through"),
path = require("path");
function compile(engine, data) {
function view() {
var x = data;
if (!window.can || !window.can.view.engine) {
console.warn("can.viewify requires that can.view.engine be "+
"previously loaded into the window. Sorry!");
return x;
} else {
return window.can.view.engine(x);
}
}
var compiled = "module.exports = (" + view.toString().replace(
/data/, JSON.stringify(data)).replace(/engine/g, engine) + ")();";
return compiled;
}
module.exports = function (file) {
var data = "",
ext = path.extname(file).substr(1);
function write (buf) { data += buf; }
function end (engine) {
return function() {
this.queue(compile(engine, data));
this.queue(null);
};
}
switch (ext) {
case "mustache":
case "ejs":
return through(write, end(ext));
default:
return through();
}
};