-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhbs-builder.js
54 lines (43 loc) · 1.77 KB
/
hbs-builder.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
define(["handlebars-compiler"], function (Handlebars) {
var buildMap = {},
templateExtension = ".hbs";
var fs = nodeRequire("fs");
var vm = nodeRequire("vm");
return {
// http://requirejs.org/docs/plugins.html#apiload
load: function (name, parentRequire, onload, config) {
// Load the Handlebars library using the path provided by the configuration.
if (!Handlebars) {
var handlebarsPath = parentRequire.toUrl((config.hbs && config.hbs.compilerPath) || "handlebars");
// Add the extension if not present.
if (handlebarsPath.indexOf(".js", handlebarsPath.length - 3) < 0) {
handlebarsPath += ".js";
}
var context = {};
vm.runInNewContext(fs.readFileSync(handlebarsPath), context, handlebarsPath);
Handlebars = context.Handlebars;
}
// Get the template extension.
var ext = (config.hbs && config.hbs.templateExtension ? config.hbs.templateExtension : templateExtension);
// Use node.js file system module to load the template.
// Sorry, no Rhino support.
var fsPath = parentRequire.toUrl(name + ext);
buildMap[name] = fs.readFileSync(fsPath).toString();
parentRequire(["handlebars"], function () {
onload();
});
},
// http://requirejs.org/docs/plugins.html#apiwrite
write: function (pluginName, name, write) {
var compiled = Handlebars.precompile(buildMap[name]);
// Write out precompiled version of the template function as AMD
// definition.
write(
"define('hbs!" + name + "', ['handlebars'], function(Handlebars){ \n" +
"Handlebars = Handlebars || this.Handlebars;\n" +
"return Handlebars.template(" + compiled.toString() + ");\n" +
"});\n"
);
}
};
});