Merge pull request #25 from Alshten/master
Return the function when namespace explicitely set to 'false'
This commit is contained in:
commit
666ff0053e
1
AUTHORS
1
AUTHORS
@ -2,3 +2,4 @@ Tim Branyen (http://tbranyen.com)
|
||||
Tyler Kellen (http://goingslowly.com/)
|
||||
Chris Talkington (http://christalkington.com/)
|
||||
Larry Davis (http://lazd.net/)
|
||||
Adrien Antoine (http://adriantoine.com/)
|
16
Gruntfile.js
16
Gruntfile.js
@ -46,7 +46,7 @@ module.exports = function(grunt) {
|
||||
variable: 'obj'
|
||||
},
|
||||
prettify: true,
|
||||
amdWrapper: true
|
||||
amd: true
|
||||
},
|
||||
files: {
|
||||
"tmp/pretty_amd.js": ["test/fixtures/template.html"]
|
||||
@ -68,12 +68,24 @@ module.exports = function(grunt) {
|
||||
templateSettings: {
|
||||
variable: 'obj'
|
||||
},
|
||||
amdWrapper:true
|
||||
amd:true
|
||||
},
|
||||
files: {
|
||||
"tmp/amd_wrapper.js": ["test/fixtures/template.html"]
|
||||
}
|
||||
},
|
||||
amd_wrapper_no_ns: {
|
||||
options: {
|
||||
templateSettings: {
|
||||
variable: 'obj'
|
||||
},
|
||||
amd:true,
|
||||
namespace:false
|
||||
},
|
||||
files: {
|
||||
"tmp/amd_wrapper_no_ns.js": ["test/fixtures/template.html"]
|
||||
}
|
||||
},
|
||||
uglyfile: {
|
||||
options: {
|
||||
templateSettings: {
|
||||
|
19
tasks/jst.js
19
tasks/jst.js
@ -30,7 +30,10 @@ module.exports = function(grunt) {
|
||||
|
||||
grunt.verbose.writeflags(options, 'Options');
|
||||
|
||||
var nsInfo = helpers.getNamespaceDeclaration(options.namespace);
|
||||
var nsInfo;
|
||||
if (options.namespace !== false) {
|
||||
nsInfo = helpers.getNamespaceDeclaration(options.namespace);
|
||||
}
|
||||
|
||||
this.files.forEach(function(f) {
|
||||
var output = f.src.filter(function(filepath) {
|
||||
@ -58,21 +61,31 @@ module.exports = function(grunt) {
|
||||
}
|
||||
filename = processName(filepath);
|
||||
|
||||
if (options.amd && options.namespace === false) {
|
||||
return 'return ' + compiled;
|
||||
}
|
||||
return nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';';
|
||||
});
|
||||
|
||||
if (output.length < 1) {
|
||||
grunt.log.warn('Destination not written because compiled files were empty.');
|
||||
} else {
|
||||
if (options.namespace !== false) {
|
||||
output.unshift(nsInfo.declaration);
|
||||
if (options.amdWrapper) {
|
||||
}
|
||||
if (options.amd) {
|
||||
if (options.prettify) {
|
||||
output.forEach(function(line, index) {
|
||||
output[index] = " " + line;
|
||||
});
|
||||
}
|
||||
output.unshift("define(function(){");
|
||||
output.push(" return " + nsInfo.namespace + ";" + lf + "});");
|
||||
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("});");
|
||||
}
|
||||
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
|
||||
grunt.log.writeln('File "' + f.dest + '" created.');
|
||||
|
@ -11,4 +11,5 @@ return __p
|
||||
};
|
||||
|
||||
return this["JST"];
|
||||
|
||||
});
|
11
test/expected/amd_wrapper_no_ns.js
Normal file
11
test/expected/amd_wrapper_no_ns.js
Normal file
@ -0,0 +1,11 @@
|
||||
define(function(){
|
||||
|
||||
return function(obj) {
|
||||
var __t, __p = '', __e = _.escape;
|
||||
__p += '<head><title>' +
|
||||
((__t = ( obj.title )) == null ? '' : __t) +
|
||||
'</title></head>';
|
||||
return __p
|
||||
}
|
||||
|
||||
});
|
@ -5,4 +5,5 @@ define(function(){
|
||||
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"];
|
||||
|
||||
});
|
@ -6,7 +6,7 @@ exports['jst'] = {
|
||||
|
||||
var expect, result;
|
||||
|
||||
test.expect(9);
|
||||
test.expect(10);
|
||||
|
||||
expect = grunt.file.read("test/expected/jst.js");
|
||||
result = grunt.file.read("tmp/jst.js");
|
||||
@ -32,6 +32,10 @@ exports['jst'] = {
|
||||
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/amd_wrapper_no_ns.js");
|
||||
result = grunt.file.read("tmp/amd_wrapper_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");
|
||||
|
Loading…
Reference in New Issue
Block a user