-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
55 lines (42 loc) · 1.72 KB
/
engine.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
// Ractive.js Client Template Engine wrapper for SocketStream 0.3
var path = require('path');
var fs = require('fs');
exports.init = function(ss, config) {
// set config variables
config = config || {};
var nl = config.pretty ? '\n' : '';
// load ractive.js
var filename = config.ractiveFilename || 'ractive.min.js';
var ractivePath = path.join(path.dirname(require.resolve('ractive')), filename);
var clientCode = fs.readFileSync(ractivePath, 'utf8');
// TODO: there's a bug here, so doing this for now.
// SEE: https://github.com/socketstream/socketstream/issues/381
// AND: https://github.com/socketstream/socketstream/issues/353
clientCode = clientCode.replace('//# sourceMappingURL=' + filename + '.map', '');
ss.client.send('lib', 'ractive', clientCode);
// load ractive.js.map
// ractiveMapPath = path.join(path.dirname(require.resolve('ractive')), filename + '.map');
// clientMapCode = fs.readFileSync(ractiveMapPath, 'utf8');
// ss.client.send('lib', 'ractive-map', clientMapCode);
return {
name: config.namespace || 'Ractive',
selectFormatter: function(templatePath, formatters, defaultFormatter) {
if (path.extname(templatePath).toLowerCase() === '.jade') {
return false;
}
return defaultFormatter;
},
// Opening code to use when a template is called for the first time
prefix: function() {
return nl + nl;
},
// Closing code once all templates have been written into the <script> tag
suffix: function() {
return '';
},
// Compile template into a function and attach to window.<windowVar>
process: function(template, path, id) {
return nl + '<script id="' + id.replace(/^templates-/, '') + '" type="text/ractive">' + nl + template + nl + '</script>' + nl + nl;
}
};
};