From 4a4a57b56c86cece0236945d67f57fb72e012264 Mon Sep 17 00:00:00 2001 From: iamwillburn Date: Wed, 15 Jul 2015 11:05:47 +0100 Subject: [PATCH 1/2] Updates handlebars.js Made changes to support the output of node-like (and brunch-like) modules. Use by setting the option 'node_like' to 'true'. --- tasks/handlebars.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/tasks/handlebars.js b/tasks/handlebars.js index 68f0cce..c8c6bb2 100644 --- a/tasks/handlebars.js +++ b/tasks/handlebars.js @@ -47,11 +47,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() { @@ -77,6 +78,8 @@ module.exports = function(grunt) { var processPartialName = options.processPartialName || defaultProcessPartialName; var processAST = options.processAST || defaultProcessAST; var useNamespace = options.namespace !== false; + // Adding a 'process' function so that we can wrap each individual file + // within a module. // assign compiler options var compilerOptions = options.compilerOptions || {}; @@ -100,13 +103,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 +117,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); @@ -162,6 +165,11 @@ module.exports = function(grunt) { templates.push(nsInfo.namespace + '[' + JSON.stringify(filename) + '] = ' + compiled + ';'); } else if (options.commonjs === true) { templates.push(compiled + ';'); + } else if (options.node_like === true) { + var prefix = 'require.register("' + filename + '", function(exports, require, module) {\n' + + 'module.exports = '; + var suffix = '\n});' + templates.push(prefix + compiled + suffix); } else { templates.push(compiled); } @@ -192,7 +200,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 +228,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('};'); } From 00a3a54d661f8cf9aa39c251fa85314b102ad156 Mon Sep 17 00:00:00 2001 From: iamwillburn Date: Wed, 15 Jul 2015 15:52:09 +0100 Subject: [PATCH 2/2] Updates handlebars.js Added a 'processTemplate' option that accepts a function. The intention is that it works in a similar way to the 'process' option from 'grunt-contrib-concat', allowing us to wrap the template in anything we want (or do anything we wish to with the compiled output). --- tasks/handlebars.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tasks/handlebars.js b/tasks/handlebars.js index c8c6bb2..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('.'); @@ -77,9 +80,8 @@ 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; - // Adding a 'process' function so that we can wrap each individual file - // within a module. // assign compiler options var compilerOptions = options.compilerOptions || {}; @@ -162,16 +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 + ';'); - } else if (options.node_like === true) { - var prefix = 'require.register("' + filename + '", function(exports, require, module) {\n' - + 'module.exports = '; - var suffix = '\n});' - templates.push(prefix + compiled + suffix); + templates.push(processTemplate(compiled + ';', filename)); } else { - templates.push(compiled); + templates.push(processTemplate(compiled, filename)); } } });