forked from at0g/nunjucks-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime-shim.js
46 lines (38 loc) · 1.33 KB
/
runtime-shim.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
module.exports = function (nunjucks, env, obj, dependencies){
var oldRoot = obj.root;
obj.root = function( env, context, frame, runtime, ignoreMissing, cb ) {
var oldGetTemplate = env.getTemplate;
env.getTemplate = function (name, ec, parentName, ignoreMissing, cb) {
if( typeof ec === "function" ) {
cb = ec = false;
}
var _require = function (name) {
try {
// add a reference to the already resolved dependency here
return dependencies[name];
}
catch (e) {
if (frame.get("_require")) {
return frame.get("_require")(name);
}
else {
console.warn('Could not load template "%s"', name);
}
}
};
var tmpl = _require(name);
frame.set("_require", _require);
if( ec ) tmpl.compile();
cb( null, tmpl );
};
oldRoot(env, context, frame, runtime, ignoreMissing, function (err, res) {
env.getTemplate = oldGetTemplate;
cb( err, res );
});
};
var src = {
obj: obj,
type: 'code'
};
return new nunjucks.Template(src, env);
};