Merge pull request #7 from tebriel/devel

Added an amdWrapper option and a prettify option
This commit is contained in:
Tyler Kellen 2012-11-27 07:24:21 -08:00
commit 34b878aa51
9 changed files with 150 additions and 3 deletions

View File

@ -35,6 +35,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*"]
@ -71,6 +96,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-internal');
grunt.loadNpmTasks('grunt-contrib-clean');
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.

View File

@ -84,6 +84,40 @@ jst: {
}
}
```
##### options.prettify
Type: ```boolean```
Default: false
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.
```javascript
options: {
prettify: true
}
```
##### options.amdWrapper
Type: ```boolean```
Default: false
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
}
```
### Examples
```js

View File

@ -48,4 +48,39 @@ jst: {
}
}
}
```
```
## options.prettify
Type: ```boolean```
Default: false
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.
```javascript
options: {
prettify: true
}
```
## options.amdWrapper
Type: ```boolean```
Default: false
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
}
```

View File

@ -35,6 +35,7 @@
"grunt-contrib-jshint": "~0.1.0",
"grunt-contrib-nodeunit": "~0.1.0",
"grunt-contrib-internal": "~0.1.0",
"grunt-contrib-clean": "git://github.com/gruntjs/grunt-contrib-clean.git#15b40e46a6f60303ce72215d0808639dcedd31b8",
"grunt": "~0.4.0a",
"grunt-cli": "~0.1.1"
},

View File

@ -42,6 +42,9 @@ module.exports = function(grunt) {
grunt.fail.warn('JST failed to compile.');
}
if (options.prettify) {
compiled = compiled.replace(/\n+/g, '');
}
filename = processName(file);
return nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';';
@ -49,6 +52,15 @@ module.exports = function(grunt) {
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(this.file.dest, output.join('\n\n'));
grunt.log.writeln('File "' + this.file.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");
@ -24,6 +24,18 @@ exports['jst'] = {
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();
}
};
};