-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdustjs-require.js
67 lines (64 loc) · 2.23 KB
/
dustjs-require.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
define([
'module',
'./vendor/requirejs-text/text',
'./vendor/q/q'
],
function(module, text, Q) {
var dustModule = module.config().dustModule || 'dustjs-linkedin';
var buildMap = {};
/**
* This little bit of hackery is required to make dustjs play nice with the
* r.js optimizer, which, when loading the dustjs library, will bind the global "this" to
* the node.js environment causing the dust compiler to error out. To avoid this, we do not
* load dustjs as a dependency unless we are sure we are not running a build.
* @private
* @param {!object} req - the passed-in requirejs instance
* @param {!boolean} isBuild - is this script being used from r.js optimizer?
* @returns {Promise}
*/
var getDust = function(req, isBuild){
var d = Q.defer();
if (!isBuild) {
req([dustModule], function(dst){
d.resolve(dst);
});
} else {
d.resolve({});
}
return d.promise;
};
return {
/**
* Implements the plugin API load method. We load our dust
* partials via text! plugin, and then compile them using
* dust. All templates will be stored in dust.cache[path].
*
* @returns {string} template name
*/
load: function(name, req, onload, config){
var extension = name.substring(name.lastIndexOf('.'));
var path = name.slice(0, -(extension.length));
getDust(req, config.isBuild).then(function(dust){
if ( dust.cache && !dust.cache[path] ) {
text.get(req.toUrl(name), function(tpl){
if (config.isBuild) {
// write out the module definition for builds
buildMap[name] = ['define(["',dustModule,'"],function(dust){dust.loadSource(dust.compile(',"'",tpl,"'",', "',path,'")); return "',path,'";});'].join('');
} else {
dust.loadSource(dust.compile(tpl, path));
}
onload(path);
});
} else {
onload(path);
}
});
},
write: function(plugin, name, write){
if (buildMap.hasOwnProperty(name)) {
var fn = buildMap[name];
write.asModule(plugin+'!'+name, fn);
}
}
};
});