This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
forked from studybreak/jade-browser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
193 lines (158 loc) · 5.32 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var fs = require('fs')
, path = require('path')
, jade = require('jade')
, async = require('async')
, glob = require('glob')
, parser = require('uglify-js').parser
, compiler = require('uglify-js').uglify
, Expose = require('./lib/expose')
, render = require('./lib/render').render
, utils = require('./lib/utils');
var defaultOptions = exports.defaultOptions =
function(exportPath, patterns, options) {
var options = options || {};
options.ext = options.ext || 'jade';
options.namespace = options.namespace || 'jade';
options.built = false;
options.minified = false;
options.debug = options.debug || false;
options.minify = options.minify || false;
options.maxAge = options.maxAge || 86400;
options.exportPath = exportPath.replace(/\/$/,'');
options.root = path.normalize(options.root ? options.root.replace(/\/$/,'') : __dirname);
options.regexp = utils.toRegExp(exportPath, true);
options.headers = {
'Cache-Control': 'public, max-age=' + options.maxAge
, 'Content-Type': 'text/javascript'
};
if (typeof patterns == 'string') {
patterns = [patterns];
}
options.patterns = patterns;
options.files = [];
return options;
};
var middleware = exports.middleware = function(exportPath, patterns, options) {
options = options || {};
options = defaultOptions(exportPath, patterns, options);
return function(req, res, next){
if (!req.url.match(options.regexp)) {
return next();
};
function render() {
res.writeHead(200, options.headers);
res.end(options.built);
};
if (options.built) return render();
process(options, render);
}
};
var watch = exports.watch = function(exportPath, patterns, options, callback) {
options = options || {};
options = defaultOptions(exportPath, patterns, options);
process(options, function() {
console.log('Watching jade namespace:', options.namespace);
options.files.forEach(function(fd) {
// console.log('Watching:', fd);
fs.watchFile(fd, {persistent: true, interval: 500}, function(curr, prev) {
// make sure we don't fire twice...
if (curr.mtime.getTime() === prev.mtime.getTime()) return;
console.log("File changed:", fd);
options.built = false;
options.minified = false;
recache(options, callback)
})
});
});
};
var cache = exports.cache = function(exportPath, patterns, options, callback) {
options = options || {};
options = defaultOptions(exportPath, patterns, options);
recache(options, callback);
}
var recache = function(options, callback) {
process(options, function() {
write(options);
if (callback) callback();
});
};
var write = function(options) {
console.log('Caching jade namespace:', options.namespace);
var filename = options.cacheRoot + options.exportPath;
// cache development version
fs.writeFileSync(filename, options.built, 'utf8');
// cache minified version
if (options.minify) {
fs.writeFileSync(filename.replace('.js', '-min.js'), options.minified, 'utf8');
}
// do we have an event listener?
if (options.onWrite && typeof(options.onWrite) === 'function') {
options.onWrite();
};
}
var process = exports.process = function(options, callback) {
var files = [];
options.patterns.forEach(function(pattern) {
pattern = path.join(options.root, pattern);
try {
var matches = glob.sync(pattern);
matches = matches.filter(function(match) {
return match.match(options.ext + '$');
});
files = files.concat(matches);
} catch(e) {}
});
options.files = files;
var getFile = function(filename, callback) {
fs.readFile(filename, 'utf8', function(err, content){
if (err) return callback(err);
var tmpl = jade.compile(content, {
filename: filename
, inline: false
, compileDebug: false
, client: true
});
if (typeof tmpl == 'function') {
var fn = 'var jade=window.' + options.namespace + '; return anonymous(locals);'+ tmpl.toString();
fn = new Function('locals', fn);
callback(null, {
filename: filename
, fn: fn
});
} else {
callback(new Error('Failed to compile'));
}
});
};
async.map(options.files, getFile, function(err, files) {
build(options, files, callback);
})
}
var build = exports.build = function(options, files, callback) {
var templates = {}, filename;
files.forEach(function(template) {
filename = template.filename.replace(options.root + '/', '')
templates[filename] = template.fn;
});
var code = jade.runtime.escape.toString() +';'
code += jade.runtime.attrs.toString().replace(/exports\./g, '') + ';'
code += ' return attrs(obj, escaped);'
var payload = new Expose();
payload.expose({
attrs: new Function('obj', 'escaped', code)
, escape: jade.runtime.escape
, dirname: utils.dirname
, normalize: utils.normalize
, render: render(options.namespace)
, templates: templates
}, options.namespace, 'output');
// cache
options.built = payload.exposed('output');
if (options.minify) {
var code = parser.parse(options.built);
code = compiler.ast_mangle(code);
code = compiler.ast_squeeze(code);
options.minified = compiler.gen_code(code);
}
callback();
}