UNPKG

1.45 kBJavaScriptView Raw
1
2'use strict';
3
4module.exports = function (dust, renderer, config) {
5 var compileOnDemandCache = {};
6
7 // Override dust load mechanism to support namespaced templates
8 // and to not blow up if the template is not found
9 dust.onLoad = function (name, options, callback) {
10 var getTemplate = function (namespace) {
11 if (namespace.length === 0) {
12 return dust.cache[name];
13 }
14 namespace.push(name);
15 return dust.cache[namespace.join('__')] || getTemplate(namespace.slice(0, -2));
16 };
17
18 var ns = (options && options.namespace) ? options.namespace : '';
19 // DEPRECATED: If the namespace starts with the name of the host application, trim it
20 ns = renderer.TEMPLATE_CACHE_KEY_PREFIX + '__' + ns.replace(/^shunter-[^_]+__/, '');
21
22 var template = getTemplate(ns.split('__'));
23 if (template) {
24 callback(null, template);
25 } else if (config.argv['compile-on-demand'] && !compileOnDemandCache[name]) {
26 renderer.compileOnDemand(name);
27 compileOnDemandCache[name] = true;
28 dust.onLoad(name, options, callback);
29 } else {
30 config.log.warn('Template not found ' + name);
31 callback(null, '');
32 }
33 };
34
35 dust.helpers.assetPath = function (chunk, context, bodies, params) {
36 var path = renderer.assetPath(context.resolve(params.src));
37 if (!path) {
38 return chunk;
39 }
40 return chunk.write(path);
41 };
42
43 dust.config.whitespace = typeof config === 'object' &&
44 typeof config.argv === 'object' &&
45 config.argv['preserve-whitespace'];
46};