From ac6580aa2cd5c92ea4dc2f3f4676f1e7569b7511 Mon Sep 17 00:00:00 2001 From: Chris Moultrie Date: Mon, 26 Nov 2012 13:01:28 -0500 Subject: [PATCH] Added an amdWrapper option and a prettify option --- README.md | 33 ++++++++++++++++++++++++++++++++- docs/options.md | 32 +++++++++++++++++++++++++++++++- grunt.js | 27 ++++++++++++++++++++++++++- tasks/jst.js | 12 ++++++++++++ test/expected/amd_wrapper.js | 16 ++++++++++++++++ test/expected/pretty.js | 3 +++ test/expected/pretty_amd.js | 8 ++++++++ test/jst_test.js | 16 ++++++++++++++-- 8 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 test/expected/amd_wrapper.js create mode 100644 test/expected/pretty.js create mode 100644 test/expected/pretty_amd.js diff --git a/README.md b/README.md index b4a86d4..49a1e91 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,37 @@ options: { } ``` +##### prettify ```boolean``` + +When doing a quick once-over of your compiled template file, it's nice to see +an easy-to-read format that has one line per template. This will accomplish +that. + +Example: +```javascript +options: { + prettify: true +} +``` + +##### amdWrapper ```boolean``` + +With Require.js and a pre-compiled template.js you want the templates to be +wrapped in a define. This will wrap the output in: +``` javascript +define(function() { + //Templates + return this["NAMESPACE"]; +}); +``` + +Example: +``` javascript +options: { + amdWrapper: true +} +``` + ##### 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. @@ -82,4 +113,4 @@ jst: { -- -*Task submitted by [Tim Branyen](http://github.com/tbranyen).* \ No newline at end of file +*Task submitted by [Tim Branyen](http://github.com/tbranyen).* diff --git a/docs/options.md b/docs/options.md index 6d0e500..f7ed04d 100644 --- a/docs/options.md +++ b/docs/options.md @@ -22,6 +22,36 @@ options: { namespace: 'JST' } ``` +##### prettify ```boolean``` + +When doing a quick once-over of your compiled template file, it's nice to see +an easy-to-read format that has one line per template. This will accomplish +that. + +Example: +```javascript +options: { + prettify: true +} +``` + +##### amdWrapper ```boolean``` + +With Require.js and a pre-compiled template.js you want the templates to be +wrapped in a define. This will wrap the output in: +``` javascript +define(function() { + //Templates + return this["NAMESPACE"]; +}); +``` + +Example: +``` javascript +options: { + amdWrapper: true +} +``` ##### processName ```function``` @@ -54,4 +84,4 @@ jst: { } } } -``` \ No newline at end of file +``` diff --git a/grunt.js b/grunt.js index faad08a..2de31f7 100644 --- a/grunt.js +++ b/grunt.js @@ -44,6 +44,31 @@ module.exports = function(grunt) { "tmp/jst.js": ["test/fixtures/template.html"] } }, + pretty_amd: { + options: { + prettify: true, + amdWrapper: true + }, + files: { + "tmp/pretty_amd.js": ["test/fixtures/template.html"] + } + }, + prettify: { + options: { + prettify: true + }, + files: { + "tmp/pretty.js": ["test/fixtures/template.html"] + } + }, + amd_wrapper: { + options: { + amdWrapper:true + }, + files: { + "tmp/amd_wrapper.js": ["test/fixtures/template.html"] + } + }, uglyfile: { files: { "tmp/uglyfile.js": ["test/fixtures/*bad-filename*"] @@ -86,4 +111,4 @@ module.exports = function(grunt) { // By default, lint and run all tests. grunt.registerTask('default', 'lint test'); -}; \ No newline at end of file +}; diff --git a/tasks/jst.js b/tasks/jst.js index c755604..e2cf1de 100644 --- a/tasks/jst.js +++ b/tasks/jst.js @@ -44,12 +44,24 @@ module.exports = function(grunt) { grunt.fail.warn("JST failed to compile."); } + if (options.prettify) { + compiled = compiled.replace(/\n+/g, ''); + } filename = processName(file); output.push(nsInfo.namespace+"["+JSON.stringify(filename)+"] = "+compiled+";"); }); if(output.length > 0) { output.unshift(nsInfo.declaration); + if (options.amdWrapper) { + if (options.prettify) { + output.forEach(function(line, index) { + output[index] = " " + line; + }); + } + output.unshift("define(function(){"); + output.push(" return " + nsInfo.namespace + ";\n});"); + } grunt.file.write(files.dest, output.join("\n\n")); grunt.log.writeln("File '" + files.dest + "' created."); } diff --git a/test/expected/amd_wrapper.js b/test/expected/amd_wrapper.js new file mode 100644 index 0000000..ae75be8 --- /dev/null +++ b/test/expected/amd_wrapper.js @@ -0,0 +1,16 @@ +define(function(){ + +this["JST"] = this["JST"] || {}; + +this["JST"]["test/fixtures/template.html"] = function(obj){ +var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')}; +with(obj||{}){ +__p+=''+ +( title )+ +''; +} +return __p; +}; + + return this["JST"]; +}); \ No newline at end of file diff --git a/test/expected/pretty.js b/test/expected/pretty.js new file mode 100644 index 0000000..cf23f33 --- /dev/null +++ b/test/expected/pretty.js @@ -0,0 +1,3 @@ +this["JST"] = this["JST"] || {}; + +this["JST"]["test/fixtures/template.html"] = function(obj){var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};with(obj||{}){__p+=''+( title )+'';}return __p;}; \ No newline at end of file diff --git a/test/expected/pretty_amd.js b/test/expected/pretty_amd.js new file mode 100644 index 0000000..5896a50 --- /dev/null +++ b/test/expected/pretty_amd.js @@ -0,0 +1,8 @@ +define(function(){ + + this["JST"] = this["JST"] || {}; + + this["JST"]["test/fixtures/template.html"] = function(obj){var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};with(obj||{}){__p+=''+( title )+'';}return __p;}; + + return this["JST"]; +}); \ No newline at end of file diff --git a/test/jst_test.js b/test/jst_test.js index 4b78829..5ecd50c 100644 --- a/test/jst_test.js +++ b/test/jst_test.js @@ -6,7 +6,7 @@ exports['jst'] = { var expect, result; - test.expect(4); + test.expect(7); expect = grunt.file.read("test/expected/jst.js"); result = grunt.file.read("tmp/jst.js"); @@ -23,7 +23,19 @@ exports['jst'] = { expect = grunt.file.read("test/expected/ns_nested.js"); // same as previous test result = grunt.file.read("tmp/ns_nested_this.js"); test.equal(expect, result, "should define parts of nested namespaces, ignoring this."); + + expect = grunt.file.read("test/expected/pretty.js"); + result = grunt.file.read("tmp/pretty.js"); + test.equal(expect, result, "should make the output be 1 line per template, making the output less ugly"); + expect = grunt.file.read("test/expected/amd_wrapper.js"); + result = grunt.file.read("tmp/amd_wrapper.js"); + test.equal(expect, result, "should wrap the template with define for AMD pattern"); + + expect = grunt.file.read("test/expected/pretty_amd.js"); + result = grunt.file.read("tmp/pretty_amd.js"); + test.equal(expect, result, "should make the AMD wrapper output pretty"); + test.done(); } -}; \ No newline at end of file +};