forked from nicjansma/node-handlebars-precompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlebars-precompiler.js
172 lines (141 loc) · 4.39 KB
/
handlebars-precompiler.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
// Modified version of https://github.com/wycats/handlebars.js/blob/master/bin/handlebars
// Changed from command-line compiler to node module
var fs = require('fs'),
handlebars = require('handlebars'),
basename = require('path').basename,
uglify = require('uglify-js'),
vm = require('vm');
exports.do = function(opts) {
(function(opts) {
var template = [0];
if (!opts.templates.length) {
throw 'Must define at least one template or directory.';
}
opts.templates.forEach(function(template) {
fs.statSync(template);
try {
fs.statSync(template);
} catch (err) {
throw 'Unable to open template file "' + template + '"';
}
});
if (opts.simple && opts.min) {
throw 'Unable to minimze simple output';
}
if (opts.simple && (opts._.length !== 1 || fs.statSync(opts._[0]).isDirectory())) {
throw 'Unable to output multiple templates in simple mode';
}
}(opts));
var template = opts.templates[0];
// Convert the known list into a hash
var known = {};
if (opts.known && !Array.isArray(opts.known)) {
opts.known = [opts.known];
}
if (opts.known) {
for (var i = 0, len = opts.known.length; i < len; i++) {
known[opts.known[i]] = true;
}
}
var output = [];
function processTemplate(template, root) {
var path = template,
stat = fs.statSync(path);
// make the filename regex user-overridable
var fileRegex = /\.handlebars$/;
if (stat.isDirectory()) { // if its dir, loop through file in dir
fs.readdirSync(template).map(function(file) {
var path = template + '/' + file;
if (fileRegex.test(path) ) {
processTemplate(path, root || template);
}
});
} else { // real shit
var data = fs.readFileSync(path, 'utf8');
//dummy jQuery
var jQuery = function() { return jQuery; };
jQuery.ready = function() { return jQuery; };
jQuery.inArray = function() { return jQuery; };
jQuery.jquery = "1.7.2";
//dummy DOM element
var element = {
firstChild: function () { return element; },
innerHTML: function () { return element; }
};
var sandbox = {
// DOM
document: {
createRange: false,
createElement: function() { return element; }
},
// Console
console: console,
// jQuery
jQuery: jQuery,
$: jQuery,
// handlebars template to compile
template: fs.readFileSync(path, 'utf8'),
// compiled handlebars template
templatejs: null
};
// window
sandbox.window = sandbox;
// create a context for the vm using the sandbox data
var context = vm.createContext(sandbox);
// load Ember into the sandbox
vm.runInContext(opts.emberjs, context, 'ember.js');
//compile the handlebars template inside the vm context
vm.runInContext('templatejs = Ember.Handlebars.precompile(template).toString();', context);
// Clean the template name
if (!root) {
template = basename(template);
} else if (template.indexOf(root) === 0) {
template = template.substring(root.length+1);
}
template = template.replace(fileRegex, '');
output.push('Ember.TEMPLATES["' + template + '"] = Handlebars.template(' + context.templatejs + ');');
}
}
opts.templates.forEach(function(template) {
processTemplate(template, opts.root);
});
output = output.join('');
if (opts.min) {
var ast = uglify.parser.parse(output);
ast = uglify.uglify.ast_mangle(ast);
ast = uglify.uglify.ast_squeeze(ast);
output = uglify.uglify.gen_code(ast);
}
if (opts.output) {
fs.writeFileSync(opts.output, output, 'utf8');
} else {
return output;
}
}
exports.watchDir = function(dir, outfile, ember_path) {
var fs = require('fs')
, file = require('file')
, emberjs = fs.readFileSync(ember_path, 'utf8');;
var regex = /\.handlebars$/;
var compileOnChange = function(event, filename) {
console.log('[' + event + '] detected in ' + (filename ? filename : '[filename not supported]'));
console.log('[compiling] ' + outfile);
exports.do({
templates: [dir],
output: outfile,
emberjs: emberjs,
min: true
});
}
file.walk(dir, function(_, dirPath, dirs, files) {
if(files) {
for(var i = 0; i < files.length; i++) {
var file = files[i];
if(regex.test(file)) {
fs.watch(file, compileOnChange);
console.log('[watching] ' + file);
}
}
}
});
}