Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added processTemplate option #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions tasks/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('.');
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}
}
});
Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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('};');
}
Expand Down