Added an amdWrapper option and a prettify option

This commit is contained in:
Chris Moultrie 2012-11-26 13:01:28 -05:00
parent 9e40075353
commit ac6580aa2c
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``` ##### 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. 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.

View File

@ -22,6 +22,36 @@ options: {
namespace: 'JST' 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``` ##### processName ```function```

View File

@ -44,6 +44,31 @@ module.exports = function(grunt) {
"tmp/jst.js": ["test/fixtures/template.html"] "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: { uglyfile: {
files: { files: {
"tmp/uglyfile.js": ["test/fixtures/*bad-filename*"] "tmp/uglyfile.js": ["test/fixtures/*bad-filename*"]

View File

@ -44,12 +44,24 @@ module.exports = function(grunt) {
grunt.fail.warn("JST failed to compile."); grunt.fail.warn("JST failed to compile.");
} }
if (options.prettify) {
compiled = compiled.replace(/\n+/g, '');
}
filename = processName(file); filename = processName(file);
output.push(nsInfo.namespace+"["+JSON.stringify(filename)+"] = "+compiled+";"); output.push(nsInfo.namespace+"["+JSON.stringify(filename)+"] = "+compiled+";");
}); });
if(output.length > 0) { if(output.length > 0) {
output.unshift(nsInfo.declaration); 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.file.write(files.dest, output.join("\n\n"));
grunt.log.writeln("File '" + files.dest + "' created."); 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; var expect, result;
test.expect(4); test.expect(7);
expect = grunt.file.read("test/expected/jst.js"); expect = grunt.file.read("test/expected/jst.js");
result = grunt.file.read("tmp/jst.js"); result = grunt.file.read("tmp/jst.js");
@ -24,6 +24,18 @@ exports['jst'] = {
result = grunt.file.read("tmp/ns_nested_this.js"); result = grunt.file.read("tmp/ns_nested_this.js");
test.equal(expect, result, "should define parts of nested namespaces, ignoring this."); 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(); test.done();
} }
}; };