-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhb.js
68 lines (57 loc) · 2.03 KB
/
hb.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
/**
* @license
* RequireJS Handlebars Plugin | v0.0.2
* Author: Hans Magnus Inderberg | MIT License
* Inspired by the RequireJS Hogan Plugin
*/
define(['handlebars', 'text'], function(handlebars, text) {
var DEFAULT_EXTENSION = '.hb';
var _buildMap = {};
function registerPartial(filePath, template) {
var fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
if (fileName.charAt(0) === '_') {
handlebars.registerPartial(filePath.replace(/\//g, '.'), template);
}
}
function load(filePath, req, onLoad, config) {
var hbConfig = config.hb || {};
var fileName = filePath;
if (hbConfig && hbConfig.templateExtension != null) {
fileName += hbConfig.templateExtension;
} else {
fileName += DEFAULT_EXTENSION;
}
text.get(req.toUrl(fileName), function(data) {
if (config.isBuild) {
_buildMap[filePath] = handlebars.precompile(data);
}
var template = handlebars.compile(data);
registerPartial(filePath, template);
onLoad(template);
});
}
var _buildTemplate = handlebars.compile(
[
'define("{{pluginName}}!{{moduleName}}", ["handlebars"], function(handlebars) {',
' var t = handlebars.template({{{fn}}});',
' var partialFunction = {{{partialFunction}}};',
' partialFunction("{{moduleName}}", t);',
' return t;',
'});\n'
].join("\n"));
function write(pluginName, moduleName, writeModule) {
if (moduleName in _buildMap) {
var fn = _buildMap[moduleName];
writeModule(_buildTemplate({
pluginName: pluginName,
moduleName: moduleName,
partialFunction: registerPartial.toString(),
fn: fn
}));
}
}
return {
load: load,
write: write
};
});