Compare commits

...

3 Commits

Author SHA1 Message Date
Tyler Kellen 9cf8882a27 docs
11 years ago
Tyler Kellen 3ebf5a404d docs
11 years ago
Tyler Kellen 04f77e9d94 offload amd/cjs formatting to node-wrapfor
11 years ago

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

@ -41,7 +41,7 @@ Concatenated files will be joined on this string.
Type: `String`
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
Type: `function`
@ -92,25 +92,30 @@ options: {
}
```
#### amd
Type: `boolean`
Default: false
#### format
Type: `Object`
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
define(function() {
//...//
return this['[template namespace]'];
format: {
type: 'amd',
deps: {
'_': 'lodash'
}
};
define(function(require) {
var _ = require('lodash');
//...//
return this['[template namespace]'];
});
```
Example:
```js
options: {
amd: true
}
```
#### processContent
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)
*This file was generated on Sat Oct 19 2013 14:22:27.*
*This file was generated on Tue Nov 05 2013 12:13:17.*

@ -10,7 +10,7 @@ Concatenated files will be joined on this string.
Type: `String`
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
Type: `function`
@ -61,25 +61,30 @@ options: {
}
```
## amd
Type: `boolean`
Default: false
## format
Type: `Object`
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
define(function() {
//...//
return this['[template namespace]'];
format: {
type: 'amd',
deps: {
'_': 'lodash'
}
};
define(function(require) {
var _ = require('lodash');
//...//
return this['[template namespace]'];
});
```
Example:
```js
options: {
amd: true
}
```
## processContent
Type: `function`

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

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

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

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

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

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

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

Loading…
Cancel
Save