forked from BenoitZugmeyer/requirejs-hbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hbs.js
37 lines (29 loc) · 1.09 KB
/
hbs.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
define(["handlebars"], function (Handlebars) {
var templateExtension = ".hbs";
return {
pluginBuilder: "./hbs-builder",
// http://requirejs.org/docs/plugins.html#apiload
load: function (name, parentRequire, onload, config) {
// Get the template extension.
var ext = (config.hbs && config.hbs.templateExtension ? config.hbs.templateExtension : templateExtension);
var self = this;
// In browsers use the text-plugin to the load template. This way we
// don't have to deal with ajax stuff
parentRequire(["text!" + name + ext], function (raw) {
parentRequire(self.extractDependencies(raw), function () {
// Just return the compiled template
onload(Handlebars.compile(raw));
});
});
},
extractDependencies: function (content) {
var dependencies = [];
content.replace(/\{\{!require\s+([\S\s]+?)\}\}/g, function (_, list) {
list.replace(/\s*([^\s,]+)\s*,?/g, function (_, dependency) {
dependencies.push(dependency);
});
});
return dependencies;
}
};
});