-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (138 loc) · 4.88 KB
/
Copy pathindex.js
File metadata and controls
175 lines (138 loc) · 4.88 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// index.js
// (c) 2011 StudyBreak Inc; Allan Carroll <allan@studybreak.com>
// Innervate is freely distributable under the MIT license.
//var exports = module.exports = require('./lib/innervate');
var findit = require('findit');
var fs = require('fs');
var jade = require('jade');
var uglify = require("uglify-js");
// TODO: decide how views are rendered server side and how models are specified.
// Also, how do models/collections and views correspond. Always 1-1 from view
// to one or the other?
var exports = module.exports = function (options) {
options = options || {};
options.mount = options.mount || '/innervate.js';
options.require = options.require && !Array.isArray(options.require) ?
[options.require] : (options.require || []);
// TODO: Add file watching support for easier use in development.
var text = options.require
// Gather a list of all files in the require directory and below.
.map(findit.sync)
// Flatten the list.
.reduce(function (memo, files) { return memo.concat(files); })
// Read the files' contents.
.map(function (file) { return [file, fs.readFileSync(file, 'utf8')]; })
// Filter the content using the file-extension based filter.
.map(function (file) {
var ext = file[0].split('.').slice(-1)[0];
return filters[ext] && filters[ext](file[0], file[1], options);
})
// Remove the output of any files with no specified filter.
.filter(function (text) { return text; });
text.unshift(runtime);
var output = minify(text.join(''));
var self = function (req, res, next) {
if (req.url.split('?')[0] !== options.mount) return next();
res.statusCode = 200;
res.setHeader('last-modified', self.modified.toString());
res.setHeader('content-type', 'text/javascript');
res.end(output);
};
self.modified = new Date();
return self;
};
// TODO: figure out how filters correspond to a namespace better, not just the
// template namespace. Should be easy to specify which kind of thing the file is
/**
*
*/
var filters = {
/**
*
*/
js: function (filename, text, options) {
return text;
},
/**
*
*/
// TODO: Make IV template agnostic. Probably needs ability to pass in filters
// that produce js functions for template types
jade: function (filename, text, options) {
options = options || {};
options.compileDebug = false;
try {
// Compile the template
var parser = new jade.Parser(String(text), filename, options);
var js = new jade.Compiler(parser.parse(), options).compile();
// Take the extension and path off the filename and use as a template id.
var id = filename.split('/').slice(-1)[0].split('.')[0];
return ''
+ 'window.iv._[' + id + ']='
+ 'function (locals) {'
+ 'var buf = [], t = window.iv, attrs = t.$a, escape = t.$e;'
+ 'with (locals || {}) {' + js + '};'
+ 'return buf.join("");'
+ '};';
}
catch (err) {
parser = parser.context();
jade.runtime.rethrow(err, parser.filename, parser.lexer.lineno);
}
}
};
/**
*
*/
function minify (js) {
var ast = uglify.parser.parse(js); // parse code and get the initial AST
ast = uglify.uglify.ast_mangle(ast); // get a new AST with mangled names
ast = uglify.uglify.ast_squeeze(ast); // get an AST with compression optimizations
return uglify.uglify.gen_code(ast, {beautify: false}); // compressed code here
}
/**
* Stringify a runtime jade function preserving minification
*/
function stringify (fn) {
return fn.toString().replace(/\n/g, '');
}
// TODO: Make the templates more ICH-like with syntax like iv.templatename({})
// TODO: Add logic to detect duplicate template names
/**
* Client side wrapper code (minified).
* Runtime borrowed from JadeVu [https://github.com/LearnBoost/jadevu]
* Copyright (c) 2011 Guillermo Rauch <guillermo@learnboost.com>
*/
var runtime = ''
+ '(function () {'
+ 'var p = "undefined" != typeof iv ? iv : undefined'
+ ', w = window'
+ ', s = Object.prototype.toString'
+ ', t = function(i, p){'
+ 'var r = _[i](p);'
+ 'return "undefined" != typeof jQuery ? jQuery(r) : r;'
+ '}'
+ ', exports = {t: t}'
+ ', _ = t._ = {};'
+ 't.noConflict = function(){'
+ 'w.iv = p;'
+ 'return t;'
+ '};'
+ 'var $k = Object.keys || function(obj){'
+ 'var a = [];'
+ 'for(var i in obj)'
+ 'if (obj.hasOwnProperty(i)) a.push(i);'
+ 'return a;'
+ '};'
+ 'var $a = Array.isArray || function(obj){'
+ 'return "[object Array]" == s.call(obj);'
+ '};'
+ 't.$e = ' + stringify(jade.runtime.escape) + ';'
+ 't.$a = '
+ stringify(jade.runtime.attrs)
.replace(/escape/g, 't.$e')
.replace(/Object\.keys/g, '$k')
.replace(/Array\.isArray/g, '$a')
+ ';'
+ 'if (!w.iv) w.iv = t;'
+ '})();';