Compare commits

...

3 Commits

Author SHA1 Message Date
Tyler Kellen
9cf8882a27 docs 2013-11-05 12:13:19 -05:00
Tyler Kellen
3ebf5a404d docs 2013-11-05 12:09:48 -05:00
Tyler Kellen
04f77e9d94 offload amd/cjs formatting to node-wrapfor 2013-11-05 12:08:44 -05:00
10 changed files with 115 additions and 86 deletions

View File

@ -46,7 +46,9 @@ module.exports = function(grunt) {
variable: 'obj' variable: 'obj'
}, },
prettify: true, prettify: true,
amd: true format: {
type: 'amd'
}
}, },
files: { files: {
"tmp/pretty_amd.js": ["test/fixtures/template.html"] "tmp/pretty_amd.js": ["test/fixtures/template.html"]
@ -63,27 +65,47 @@ module.exports = function(grunt) {
"tmp/pretty.js": ["test/fixtures/template.html"] "tmp/pretty.js": ["test/fixtures/template.html"]
} }
}, },
amd_wrapper: { format_amd: {
options: { options: {
templateSettings: { templateSettings: {
variable: 'obj' variable: 'obj'
}, },
amd:true format: {
type: 'amd'
}
}, },
files: { files: {
"tmp/amd_wrapper.js": ["test/fixtures/template.html"] "tmp/format_amd.js": ["test/fixtures/template.html"]
} }
}, },
amd_wrapper_no_ns: { format_amd_with_dep: {
options: { options: {
templateSettings: { templateSettings: {
variable: 'obj' variable: 'obj'
}, },
amd:true, format: {
namespace:false type: 'amd',
deps: {
"_": "lodash"
}
}
}, },
files: { files: {
"tmp/amd_wrapper_no_ns.js": ["test/fixtures/template.html"] "tmp/format_amd_with_dep.js": ["test/fixtures/template.html"]
}
},
format_amd_no_ns: {
options: {
templateSettings: {
variable: 'obj'
},
format: {
type: 'amd'
},
namespace: false
},
files: {
"tmp/format_amd_no_ns.js": ["test/fixtures/template.html"]
} }
}, },
uglyfile: { uglyfile: {

View File

@ -41,7 +41,7 @@ Concatenated files will be joined on this string.
Type: `String` Type: `String`
Default: 'JST' Default: 'JST'
The namespace in which the precompiled templates will be assigned. Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping. When false with amd option set true, templates will be returned directly from the AMD wrapper. The namespace in which the precompiled templates will be assigned. Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping. When false and formatting to amd or cjs module, templates will be returned directly from the AMD wrapper.
#### processName #### processName
Type: `function` Type: `function`
@ -92,25 +92,30 @@ options: {
} }
``` ```
#### amd #### format
Type: `boolean` Type: `Object`
Default: false Default: {}
Wraps the output file with an AMD define function and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly. Supported keys:
* `type`: `'amd'|'cjs'`
* `deps`: `{ depVar: depname }`
Converts the output file to a specified module format (amd or cjs). The compiled template namespace will be exported unless `namespace` has been explicitly set to false, in which case the template function will be returned directly. If a `deps` object has been defined, the dependencies will be included in-line.
```js ```js
define(function() { format: {
//...// type: 'amd',
return this['[template namespace]']; deps: {
'_': 'lodash'
}
};
define(function(require) {
var _ = require('lodash');
//...//
return this['[template namespace]'];
}); });
``` ```
Example:
```js
options: {
amd: true
}
```
#### processContent #### processContent
Type: `function` Type: `function`
@ -165,4 +170,4 @@ Note that the `interpolate: /\{\{(.+?)\}\}/g` setting above is simply an example
Task submitted by [Tim Branyen](http://tbranyen.com) Task submitted by [Tim Branyen](http://tbranyen.com)
*This file was generated on Sat Oct 19 2013 14:22:27.* *This file was generated on Tue Nov 05 2013 12:13:17.*

View File

@ -10,7 +10,7 @@ Concatenated files will be joined on this string.
Type: `String` Type: `String`
Default: 'JST' Default: 'JST'
The namespace in which the precompiled templates will be assigned. Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping. When false with amd option set true, templates will be returned directly from the AMD wrapper. The namespace in which the precompiled templates will be assigned. Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping. When false and formatting to amd or cjs module, templates will be returned directly from the AMD wrapper.
## processName ## processName
Type: `function` Type: `function`
@ -61,25 +61,30 @@ options: {
} }
``` ```
## amd ## format
Type: `boolean` Type: `Object`
Default: false Default: {}
Wraps the output file with an AMD define function and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly. Supported keys:
* `type`: `'amd'|'cjs'`
* `deps`: `{ depVar: depname }`
Converts the output file to a specified module format (amd or cjs). The compiled template namespace will be exported unless `namespace` has been explicitly set to false, in which case the template function will be returned directly. If a `deps` object has been defined, the dependencies will be included in-line.
```js ```js
define(function() { format: {
//...// type: 'amd',
return this['[template namespace]']; deps: {
'_': 'lodash'
}
};
define(function(require) {
var _ = require('lodash');
//...//
return this['[template namespace]'];
}); });
``` ```
Example:
```js
options: {
amd: true
}
```
## processContent ## processContent
Type: `function` Type: `function`

View File

@ -28,7 +28,8 @@
}, },
"dependencies": { "dependencies": {
"lodash": "~1.0.0", "lodash": "~1.0.0",
"grunt-lib-contrib": "~0.5.1" "grunt-lib-contrib": "~0.5.1",
"wrapfor": "~0.1.0"
}, },
"devDependencies": { "devDependencies": {
"grunt-contrib-jshint": "~0.6.0", "grunt-contrib-jshint": "~0.6.0",
@ -43,4 +44,4 @@
"keywords": [ "keywords": [
"gruntplugin" "gruntplugin"
] ]
} }

View File

@ -11,6 +11,7 @@
module.exports = function(grunt) { module.exports = function(grunt) {
var _ = require('lodash'); var _ = require('lodash');
var wrapfor = require('wrapfor');
// filename conversion for templates // filename conversion for templates
var defaultProcessName = function(name) { return name; }; var defaultProcessName = function(name) { return name; };
@ -61,8 +62,8 @@ module.exports = function(grunt) {
} }
filename = processName(filepath); filename = processName(filepath);
if (options.amd && options.namespace === false) { if (options.namespace === false) {
return 'return ' + compiled; return compiled;
} }
return nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';'; return nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';';
}); });
@ -73,21 +74,17 @@ module.exports = function(grunt) {
if (options.namespace !== false) { if (options.namespace !== false) {
output.unshift(nsInfo.declaration); output.unshift(nsInfo.declaration);
} }
if (options.amd) { output = output.join(grunt.util.normalizelf(options.separator));
if (options.prettify) {
output.forEach(function(line, index) { if (options.format) {
output[index] = " " + line; var wrapFn = wrapfor[options.format.type];
}); var wrapOpts = {
} deps: options.format.deps,
output.unshift("define(function(){"); exports: nsInfo?nsInfo.namespace:false
if (options.namespace !== false) { };
// Namespace has not been explicitly set to false; the AMD output = wrapFn(output, wrapOpts);
// wrapper will return the object containing the template.
output.push(" return " + nsInfo.namespace + ";");
}
output.push("});");
} }
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator))); grunt.file.write(f.dest, output);
grunt.log.writeln('File "' + f.dest + '" created.'); grunt.log.writeln('File "' + f.dest + '" created.');
} }
}); });

View File

@ -1,5 +1,4 @@
define(function(){ define(function(require){
this["JST"] = this["JST"] || {}; this["JST"] = this["JST"] || {};
this["JST"]["test/fixtures/template.html"] = function(obj) { this["JST"]["test/fixtures/template.html"] = function(obj) {
@ -9,7 +8,5 @@ __p += '<head><title>' +
'</title></head>'; '</title></head>';
return __p return __p
}; };
return this["JST"];
return this["JST"];
}); });

View File

@ -1,5 +1,4 @@
define(function(){ define(function(require){
return function(obj) { return function(obj) {
var __t, __p = '', __e = _.escape; var __t, __p = '', __e = _.escape;
__p += '<head><title>' + __p += '<head><title>' +
@ -7,5 +6,4 @@ __p += '<head><title>' +
'</title></head>'; '</title></head>';
return __p return __p
} }
}); });

View File

@ -0,0 +1,13 @@
define(function(require){
var _ = require('lodash');
this["JST"] = this["JST"] || {};
this["JST"]["test/fixtures/template.html"] = function(obj) {
var __t, __p = '', __e = _.escape;
__p += '<head><title>' +
((__t = ( obj.title )) == null ? '' : __t) +
'</title></head>';
return __p
};
return this["JST"];
});

View File

@ -1,9 +0,0 @@
define(function(){
this["JST"] = this["JST"] || {};
this["JST"]["test/fixtures/template.html"] = function(obj) {var __t, __p = '', __e = _.escape;__p += '<head><title>' +((__t = ( obj.title )) == null ? '' : __t) +'</title></head>';return __p};
return this["JST"];
});

View File

@ -19,27 +19,27 @@ exports['jst'] = {
expect = grunt.file.read("test/expected/ns_nested.js"); expect = grunt.file.read("test/expected/ns_nested.js");
result = grunt.file.read("tmp/ns_nested.js"); result = grunt.file.read("tmp/ns_nested.js");
test.equal(expect, result, "should define parts of nested namespaces"); test.equal(expect, result, "should define parts of nested namespaces");
expect = grunt.file.read("test/expected/ns_nested.js"); // same as previous test expect = grunt.file.read("test/expected/ns_nested.js"); // same as previous test
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"); expect = grunt.file.read("test/expected/pretty.js");
result = grunt.file.read("tmp/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"); 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"); expect = grunt.file.read("test/expected/format_amd.js");
result = grunt.file.read("tmp/amd_wrapper.js"); result = grunt.file.read("tmp/format_amd.js");
test.equal(expect, result, "should wrap the template with define for AMD pattern"); test.equal(expect, result, "should wrap the template with define for AMD pattern");
expect = grunt.file.read("test/expected/amd_wrapper_no_ns.js"); expect = grunt.file.read("test/expected/format_amd_with_dep.js");
result = grunt.file.read("tmp/amd_wrapper_no_ns.js"); result = grunt.file.read("tmp/format_amd_with_dep.js");
test.equal(expect, result, "should wrap the template with define for AMD pattern and load dependencies");
expect = grunt.file.read("test/expected/format_amd_no_ns.js");
result = grunt.file.read("tmp/format_amd_no_ns.js");
test.equal(expect, result, "should wrap the template with define for AMD pattern and return the function itself with no namespace"); test.equal(expect, result, "should wrap the template with define for AMD pattern and return the function itself with no namespace");
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");
expect = grunt.file.read("test/expected/process_content.js"); expect = grunt.file.read("test/expected/process_content.js");
result = grunt.file.read("tmp/process_content.js"); result = grunt.file.read("tmp/process_content.js");
test.equal(expect, result, "should convert file content"); test.equal(expect, result, "should convert file content");