forked from zkat/can.viewify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (52 loc) · 1.57 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
"use strict";
var through = require("through"),
jsdom = require("jsdom"),
fs = require("fs"),
path = require("path");
function compile(opts, data, callback) {
jsdom.env({
html: "<h1>can.viewify</h1>",
src: [
fs.readFileSync(require.resolve("jquery/jquery.js")),
fs.readFileSync(require.resolve("canjs/can.jquery.js")),
fs.readFileSync(require.resolve("canjs/can.ejs.js"))
],
done: function(err, win) {
if (err) {
callback(err);
} else {
var can = win.can;
for(var key in can.view.Scanner.tags) {
if (can.view.Scanner.tags.hasOwnProperty(key)) {
can.view.Scanner.tags[key] = Function.prototype;
}
}
var script = can.view.types["."+opts.ext].script(opts.filename, data);
callback(null, script);
}
}
});
}
module.exports = function (file, opts) {
var data = "",
ext = path.extname(file).substr(1),
filename = path.basename(file, "."+ext);
function write (buf) { data += buf; }
function end () {
var stream = this;
compile({ext: ext, filename: filename}, data, function(err, script) {
// TODO - this require('can') was not working on a project
stream.queue("/*var can=require('can');*/"+
(/ejs/i.test(ext) ? "require('can/view/ejs/ejs.js');" : "")+
"module.exports=can.view.preload('"+filename+"',("+script+"));");
stream.queue(null);
});
}
switch (ext) {
case "mustache":
case "ejs":
return through(write, end);
default:
return through();
}
};