diff --git a/tasks/handlebars.js b/tasks/handlebars.js index 68f0cce..b9a59c4 100644 --- a/tasks/handlebars.js +++ b/tasks/handlebars.js @@ -22,6 +22,9 @@ module.exports = function(grunt) { // filename conversion for templates var defaultProcessName = function(name) { return name; }; + // process templates before concatenating + var defaultProcessTemplate = function(content, filename) { return content; }; + // filename conversion for partials var defaultProcessPartialName = function(filepath) { var pieces = _.last(filepath.split('/')).split('.'); @@ -47,11 +50,12 @@ module.exports = function(grunt) { // In case only one namespace has been declared it will only return it. if (declarations.length === 1) { return declarations[0]; + } else { + // we only need to take any declaration to extract the global namespace. + // Another option might be find the shortest declaration which is the global one. + var matches = declarations[0].match(/(this\[[^\[]+\])/g); + return matches[0]; } - // We only need to take any declaration to extract the global namespace. - // Another option might be find the shortest declaration which is the global one. - var matches = declarations[0].match(/(this\[[^\[]+\])/g); - return matches[0]; }; grunt.registerMultiTask('handlebars', 'Compile handlebars templates and partials.', function() { @@ -76,6 +80,7 @@ module.exports = function(grunt) { var processName = options.processName || defaultProcessName; var processPartialName = options.processPartialName || defaultProcessPartialName; var processAST = options.processAST || defaultProcessAST; + var processTemplate = options.processTemplate || defaultProcessTemplate; var useNamespace = options.namespace !== false; // assign compiler options @@ -100,13 +105,12 @@ module.exports = function(grunt) { // Just get the namespace info for a given template var getNamespaceInfo = _.memoize(function(filepath) { - if (!useNamespace) { - return undefined; - } + if (!useNamespace) {return undefined;} if (_.isFunction(options.namespace)) { return nsdeclare(options.namespace(filepath), nsDeclareOptions); + } else { + return nsdeclare(options.namespace, nsDeclareOptions); } - return nsdeclare(options.namespace, nsDeclareOptions); }); // iterate files, processing partials and templates separately @@ -115,8 +119,9 @@ module.exports = function(grunt) { if (!grunt.file.exists(filepath)) { grunt.log.warn('Source file "' + filepath + '" not found.'); return false; + } else { + return true; } - return true; }) .forEach(function(filepath) { var src = processContent(grunt.file.read(filepath), filepath); @@ -159,11 +164,11 @@ module.exports = function(grunt) { if (nsInfo.declaration) { declarations.push(nsInfo.declaration); } - templates.push(nsInfo.namespace + '[' + JSON.stringify(filename) + '] = ' + compiled + ';'); + templates.push(processTemplate(nsInfo.namespace + '[' + JSON.stringify(filename) + '] = ' + compiled + ';', filename)); } else if (options.commonjs === true) { - templates.push(compiled + ';'); + templates.push(processTemplate(compiled + ';', filename)); } else { - templates.push(compiled); + templates.push(processTemplate(compiled, filename)); } } }); @@ -192,7 +197,7 @@ module.exports = function(grunt) { } else if (typeof options.amd === 'string') { output.unshift('define([\'' + options.amd + '\'], function(Handlebars) {'); } else if (typeof options.amd === 'function') { - output.unshift('define([\'' + options.amd(filename, ast, compiled) + '\'], function(Handlebars) {'); + output.unshift("define(['" + options.amd(filename, ast, compiled) + "'], function(Handlebars) {"); } else if (Array.isArray(options.amd)) { // convert options.amd to a string of dependencies for require([...]) var amdString = ''; @@ -220,7 +225,7 @@ module.exports = function(grunt) { if (useNamespace) { output.push('return ' + nsInfo.namespace + ';'); } - // Export the templates object for CommonJS environments. + output.unshift('module.exports = function(Handlebars) {'); output.push('};'); }