grunt-contrib-jst
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -47,2 +47,7 @@ /* | ||
} | ||
}, | ||
uglyfile: { | ||
files: { | ||
"tmp/uglyfile.js": ["test/fixtures/*bad*"] | ||
} | ||
} | ||
@@ -49,0 +54,0 @@ }, |
{ | ||
"name": "grunt-contrib-jst", | ||
"description": "Precompile Handlebars templates to JST file.", | ||
"version": "0.2.1", | ||
"description": "Precompile Underscore templates to JST file.", | ||
"version": "0.2.2", | ||
"homepage": "https://github.com/gruntjs/grunt-contrib-jst", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -42,2 +42,14 @@ # grunt-contrib-jst | ||
##### processName ```function``` | ||
This option accepts a function which takes one argument (the template filepath) and returns a string which will be used as the key for the precompiled template object. The example below stores all templates on the default JST namespace in capital letters. | ||
``` javascript | ||
options: { | ||
processName: function(filename) { | ||
return filename.toUpperCase(); | ||
} | ||
} | ||
``` | ||
##### templateSettings ```object``` | ||
@@ -62,2 +74,7 @@ | ||
} | ||
``` | ||
``` | ||
## Release History | ||
* 2012/08/12 - v0.2.2 - Added processName functionality & escaping single quotes in filenames. | ||
* 2012/08/10 - v0.2.0 - Refactored from grunt-contrib into individual repo. |
@@ -15,11 +15,7 @@ /* | ||
var jst = function(source, filepath, namespace, templateSettings) { | ||
try { | ||
return namespace + "['" + filepath + "'] = " + _.template(source, false, templateSettings).source + ";"; | ||
} catch (e) { | ||
grunt.log.error(e); | ||
grunt.fail.warn("JST failed to compile."); | ||
} | ||
}; | ||
var escapeQuote = function(name) { return name.replace("'","\\'"); }; | ||
// filename conversion for templates | ||
var defaultProcessName = function(name) { return name; }; | ||
grunt.registerMultiTask("jst", "Compile underscore templates to JST file", function() { | ||
@@ -30,2 +26,5 @@ | ||
// assign filename transformation functions | ||
var processName = options.processName || defaultProcessName; | ||
grunt.verbose.writeflags(options, "Options"); | ||
@@ -36,30 +35,32 @@ | ||
var srcFiles; | ||
var taskOutput; | ||
var sourceCode; | ||
var sourceCompiled; | ||
var compiled, srcFiles, src, filename; | ||
var output = []; | ||
var namespace = "this['" + options.namespace + "']"; | ||
var helperNamespace = "this['" + options.namespace + "']"; | ||
this.files.forEach(function(files) { | ||
srcFiles = grunt.file.expandFiles(files.src); | ||
srcFiles.forEach(function(file) { | ||
src = grunt.file.read(file); | ||
this.files.forEach(function(file) { | ||
srcFiles = grunt.file.expandFiles(file.src); | ||
try { | ||
compiled = _.template(src, false, options.templateSettings).source; | ||
} catch (e) { | ||
grunt.log.error(e); | ||
grunt.fail.warn("JST failed to compile."); | ||
} | ||
taskOutput = []; | ||
taskOutput.push(helperNamespace + " = " + helperNamespace + " || {};"); | ||
srcFiles.forEach(function(srcFile) { | ||
sourceCode = grunt.file.read(srcFile); | ||
sourceCompiled = jst(sourceCode, srcFile, helperNamespace, options.templateSettings); | ||
taskOutput.push(sourceCompiled); | ||
filename = escapeQuote(processName(file)); | ||
output.push(namespace+"['"+filename+"'] = "+compiled+";"); | ||
}); | ||
if (taskOutput.length > 0) { | ||
grunt.file.write(file.dest, taskOutput.join("\n\n")); | ||
grunt.log.writeln("File '" + file.dest + "' created."); | ||
if(output.length > 0) { | ||
output.unshift(namespace + " = " + namespace + " || {};"); | ||
grunt.file.write(files.dest, output.join("\n\n")); | ||
grunt.log.writeln("File '" + files.dest + "' created."); | ||
} | ||
}); | ||
}); | ||
}; |
@@ -7,10 +7,16 @@ var grunt = require('grunt'); | ||
test.expect(1); | ||
var expect, result; | ||
var expect = "this['JST'] = this['JST'] || {};\n\nthis['JST']['test/fixtures/template.html'] = function(obj){\nvar __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};\nwith(obj||{}){\n__p+='<head><title>'+\n( title )+\n'</title></head>';\n}\nreturn __p;\n};"; | ||
var result = grunt.file.read("tmp/jst.js"); | ||
test.expect(2); | ||
expect = 'this[\'JST\'] = this[\'JST\'] || {};\n\nthis[\'JST\'][\'test/fixtures/it\\\'s-a-bad-filename.html\'] = function(obj){\nvar __p=\'\';var print=function(){__p+=Array.prototype.join.call(arguments, \'\')};\nwith(obj||{}){\n__p+=\'never name your file like this.\';\n}\nreturn __p;\n};\n\nthis[\'JST\'][\'test/fixtures/template.html\'] = function(obj){\nvar __p=\'\';var print=function(){__p+=Array.prototype.join.call(arguments, \'\')};\nwith(obj||{}){\n__p+=\'<head><title>\'+\n( title )+\n\'</title></head>\';\n}\nreturn __p;\n};'; | ||
result = grunt.file.read("tmp/jst.js"); | ||
test.equal(expect, result, "should compile underscore templates into JST"); | ||
expect = 'this[\'JST\'] = this[\'JST\'] || {};\n\nthis[\'JST\'][\'test/fixtures/it\\\'s-a-bad-filename.html\'] = function(obj){\nvar __p=\'\';var print=function(){__p+=Array.prototype.join.call(arguments, \'\')};\nwith(obj||{}){\n__p+=\'never name your file like this.\';\n}\nreturn __p;\n};'; | ||
result = grunt.file.read("tmp/uglyfile.js"); | ||
test.equal(expect, result, "should escape single quotes in filenames"); | ||
test.done(); | ||
} | ||
}; |
9850
10
126
78