offload amd/cjs formatting to node-wrapfor
This commit is contained in:
parent
37326b9a58
commit
04f77e9d94
38
Gruntfile.js
38
Gruntfile.js
@ -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'
|
||||
},
|
||||
amd:true
|
||||
format: {
|
||||
type: 'amd'
|
||||
}
|
||||
},
|
||||
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: {
|
||||
templateSettings: {
|
||||
variable: 'obj'
|
||||
},
|
||||
amd:true,
|
||||
namespace:false
|
||||
format: {
|
||||
type: 'amd',
|
||||
deps: {
|
||||
"_": "lodash"
|
||||
}
|
||||
}
|
||||
},
|
||||
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: {
|
||||
|
24
README.md
24
README.md
@ -92,25 +92,23 @@ 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]'];
|
||||
//...//
|
||||
return this['[template namespace]'];
|
||||
});
|
||||
```
|
||||
|
||||
Example:
|
||||
```js
|
||||
options: {
|
||||
amd: true
|
||||
}
|
||||
```
|
||||
|
||||
#### processContent
|
||||
Type: `function`
|
||||
@ -165,4 +163,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:07:26.*
|
||||
|
@ -61,25 +61,23 @@ 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]'];
|
||||
//...//
|
||||
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"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
29
tasks/jst.js
29
tasks/jst.js
@ -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
|
||||
}
|
||||
|
||||
});
|
13
test/expected/format_amd_with_dep.js
Normal file
13
test/expected/format_amd_with_dep.js
Normal 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"];
|
||||
});
|
@ -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…
Reference in New Issue
Block a user