Merge pull request #8 from tebriel/master

amdWrapper and prettify options
This commit is contained in:
Tyler Kellen 2012-11-27 07:24:05 -08:00
commit e800693475
8 changed files with 142 additions and 5 deletions

View File

@ -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).*
*Task submitted by [Tim Branyen](http://github.com/tbranyen).*

View File

@ -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: {
}
}
}
```
```

View File

@ -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');
};
};

View File

@ -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.");
}

View File

@ -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+='<head><title>'+
( title )+
'</title></head>';
}
return __p;
};
return this["JST"];
});

3
test/expected/pretty.js Normal file
View File

@ -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+='<head><title>'+( title )+'</title></head>';}return __p;};

View File

@ -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+='<head><title>'+( title )+'</title></head>';}return __p;};
return this["JST"];
});

View File

@ -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();
}
};
};