diff --git a/Gruntfile.js b/Gruntfile.js index 258c5f9..f410e3c 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -52,6 +52,18 @@ module.exports = function(grunt) { "tmp/pretty_amd.js": ["test/fixtures/template.html"] } }, + pretty_commonjs: { + options: { + templateSettings: { + variable: 'obj' + }, + prettify: true, + commonjs: true + }, + files: { + "tmp/pretty_commonjs.js": ["test/fixtures/template.html"] + } + }, prettify: { options: { templateSettings: { @@ -86,6 +98,29 @@ module.exports = function(grunt) { "tmp/amd_wrapper_no_ns.js": ["test/fixtures/template.html"] } }, + commonjs_wrapper: { + options: { + templateSettings: { + variable: 'obj' + }, + commonjs:true + }, + files: { + "tmp/commonjs_wrapper.js": ["test/fixtures/template.html"] + } + }, + commonjs_wrapper_no_ns: { + options: { + templateSettings: { + variable: 'obj' + }, + commonjs:true, + namespace:false + }, + files: { + "tmp/commonjs_wrapper_no_ns.js": ["test/fixtures/template.html"] + } + }, uglyfile: { options: { templateSettings: { diff --git a/README.md b/README.md index 8273fa5..8a6a26e 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,24 @@ options: { } ``` +#### commonjs +Type: `boolean` +Default: false + +Exports the compiled templates using the CommonJS pattern for Node/Browserify and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly. + +```js +//...// +module.exports = this['[template namespace]']; +``` + +Example: +```js +options: { + commonjs: true +} +``` + #### processContent Type: `function` diff --git a/docs/jst-options.md b/docs/jst-options.md index f4ea625..02bfccd 100644 --- a/docs/jst-options.md +++ b/docs/jst-options.md @@ -81,6 +81,24 @@ options: { } ``` +## commonjs +Type: `boolean` +Default: false + +Exports the template using CommonJS pattern, for Node and Browserify use + +```js +//...// +module.exports = this['[template namespace]']; +``` + +Example: +```js +options: { + commonjs: true +} +``` + ## processContent Type: `function` diff --git a/tasks/jst.js b/tasks/jst.js index 5311480..808869a 100644 --- a/tasks/jst.js +++ b/tasks/jst.js @@ -61,6 +61,9 @@ module.exports = function(grunt) { if (options.amd && options.namespace === false) { return 'return ' + compiled; } + else if (options.commonjs && options.namespace === false){ + return 'module.exports = ' + compiled; + } return nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';'; }); @@ -84,6 +87,18 @@ module.exports = function(grunt) { } output.push("});"); } + else if (options.commonjs) { + if (options.prettify) { + output.forEach(function(line, index) { + output[index] = line; + }); + } + if (options.namespace !== false) { + // Namespace has not been explicitly set to false; the commonjs + // wrapper will return the object containing the template. + output.push("module.exports = " + nsInfo.namespace + ";"); + } + } grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator))); grunt.log.writeln('File ' + chalk.cyan(f.dest) + ' created.'); } diff --git a/test/expected/commonjs_wrapper.js b/test/expected/commonjs_wrapper.js new file mode 100644 index 0000000..7dd404f --- /dev/null +++ b/test/expected/commonjs_wrapper.js @@ -0,0 +1,11 @@ +this["JST"] = this["JST"] || {}; + +this["JST"]["test/fixtures/template.html"] = function(obj) { +var __t, __p = '', __e = _.escape; +__p += '' + +((__t = ( obj.title )) == null ? '' : __t) + +''; +return __p +}; + +module.exports = this["JST"]; \ No newline at end of file diff --git a/test/expected/commonjs_wrapper_no_ns.js b/test/expected/commonjs_wrapper_no_ns.js new file mode 100644 index 0000000..89d6411 --- /dev/null +++ b/test/expected/commonjs_wrapper_no_ns.js @@ -0,0 +1,7 @@ +module.exports = function(obj) { +var __t, __p = '', __e = _.escape; +__p += '' + +((__t = ( obj.title )) == null ? '' : __t) + +''; +return __p +} \ No newline at end of file diff --git a/test/expected/pretty_commonjs.js b/test/expected/pretty_commonjs.js new file mode 100644 index 0000000..68c57fa --- /dev/null +++ b/test/expected/pretty_commonjs.js @@ -0,0 +1,5 @@ +this["JST"] = this["JST"] || {}; + +this["JST"]["test/fixtures/template.html"] = function(obj) {var __t, __p = '', __e = _.escape;__p += '' +((__t = ( obj.title )) == null ? '' : __t) +'';return __p}; + +module.exports = this["JST"]; \ No newline at end of file diff --git a/test/jst_test.js b/test/jst_test.js index 390e0c0..24dfdc0 100644 --- a/test/jst_test.js +++ b/test/jst_test.js @@ -6,7 +6,7 @@ exports['jst'] = { var expect, result; - test.expect(10); + test.expect(13); expect = grunt.file.read("test/expected/jst.js"); result = grunt.file.read("tmp/jst.js"); @@ -48,6 +48,18 @@ exports['jst'] = { result = grunt.file.read("tmp/local_scope.js"); test.equal(expect, result, "should add `with` block when templateSettings.variable is undefined"); + expect = grunt.file.read("test/expected/commonjs_wrapper.js"); + result = grunt.file.read("tmp/commonjs_wrapper.js"); + test.equal(expect, result, "should export the templates using the CommonJS pattern"); + + expect = grunt.file.read("test/expected/commonjs_wrapper_no_ns.js"); + result = grunt.file.read("tmp/commonjs_wrapper_no_ns.js"); + test.equal(expect, result, "should export the templates using the CommonJS pattern and return the function itself with no namespace"); + + expect = grunt.file.read("test/expected/pretty_commonjs.js"); + result = grunt.file.read("tmp/pretty_commonjs.js"); + test.equal(expect, result, "should make the CommonJS wrapper output pretty"); + test.done(); } };