Merge pull request #8 from tebriel/master
amdWrapper and prettify options
This commit is contained in:
commit
e800693475
31
README.md
31
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```
|
##### 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.
|
||||||
|
@ -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```
|
||||||
|
|
||||||
|
25
grunt.js
25
grunt.js
@ -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*"]
|
||||||
|
12
tasks/jst.js
12
tasks/jst.js
@ -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.");
|
||||||
}
|
}
|
||||||
|
16
test/expected/amd_wrapper.js
Normal file
16
test/expected/amd_wrapper.js
Normal 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
3
test/expected/pretty.js
Normal 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;};
|
8
test/expected/pretty_amd.js
Normal file
8
test/expected/pretty_amd.js
Normal 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"];
|
||||||
|
});
|
@ -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();
|
||||||
}
|
}
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user