Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for generated file to run on node too.

Closes gh-21.
  • Loading branch information...
commit 0b4af708239642f7d358819147c28c7ff90bfcdc 1 parent 1abc957
@thanpolas thanpolas authored tkellen committed
View
3  AUTHORS
@@ -5,4 +5,5 @@ David Mosher (http://blog.davemo.com)
Larry Davis (http://lazd.net/)
Pete Hawkins (http://phawk.co.uk)
Alexander Kong (http://github.com/WebBlow)
-Yusuke Kawasaki (https://github.com/kawanet)
+Yusuke Kawasaki (https://github.com/kawanet)
+Thanasis Polychronakis (http://thanpol.as)
View
4 CHANGELOG
@@ -1,3 +1,7 @@
+v0.5.3rc7:
+ date: 2013-02-01
+ changes:
+ - Add `node` option to produce dual node.js / front-end compiled file.
v0.5.2rc7:
date: 2013-01-29
changes:
View
9 Gruntfile.js
@@ -38,6 +38,15 @@ module.exports = function(grunt) {
'tmp/handlebars.js': ['test/fixtures/_partial.hbs', 'test/fixtures/one.hbs']
}
},
+ compileNode: {
+ options: {
+ namespace: 'JST',
+ node: true
+ },
+ files: {
+ 'tmp/handlebars-node.js': ['test/fixtures/_partial.hbs', 'test/fixtures/one.hbs']
+ }
+ },
wrapcompile: {
options: {
namespace: 'JST',
View
11 README.md
@@ -50,6 +50,14 @@ Default: `false`
Determine if preprocessed template functions will be wrapped in Handlebars.template function.
+#### node
+Type: `Boolean`
+Default: `false`
+
+Enable the compiled file to be required on node.js by preppending and appending proper declarations. You can use the file safely on the front-end.
+
+For this option to work you need to define the `namespace` option.
+
#### amd
Type: `Boolean`
default: `false`
@@ -139,6 +147,7 @@ handlebars: {
## Release History
+ * 2013-01-31   v0.5.3rc7   Add `node` option to produce dual node.js / front-end compiled file.
* 2013-01-28   v0.5.2rc7   Define handlebars as a dependency for AMD option.
* 2013-01-27   v0.5.1rc7   Add AMD compilation option. Add processContent option. Do not generate templates into a namespaces when namespace option is false.
* 2013-01-22   v0.5.0rc7   Updating grunt/gruntplugin dependencies to rc7. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions. Default wrapped option to true.
@@ -155,4 +164,4 @@ handlebars: {
Task submitted by [Tim Branyen](http://tbranyen.com)
-*This file was generated on Tue Jan 29 2013 14:07:32.*
+*This file was generated on Fri Feb 01 2013 13:53:56.*
View
8 docs/handlebars-options.md
@@ -25,6 +25,14 @@ Default: `false`
Determine if preprocessed template functions will be wrapped in Handlebars.template function.
+## node
+Type: `Boolean`
+Default: `false`
+
+Enable the compiled file to be required on node.js by preppending and appending proper declarations. You can use the file safely on the front-end.
+
+For this option to work you need to define the `namespace` option.
+
## amd
Type: `Boolean`
default: `false`
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "grunt-contrib-handlebars",
"description": "Precompile Handlebars templates to JST file.",
- "version": "0.5.2rc7",
+ "version": "0.5.3rc7",
"homepage": "https://github.com/gruntjs/grunt-contrib-handlebars",
"author": {
"name": "Grunt Team",
View
10 tasks/handlebars.js
@@ -95,6 +95,16 @@ module.exports = function(grunt) {
} else {
if (options.namespace !== false) {
output.unshift(nsInfo.declaration);
+
+ if (options.node) {
+ output.unshift('var Handlebars = Handlebars || require(\'Handlebars\');');
+
+ var nodeExport = 'if (typeof exports === \'object\' && exports) {';
+ nodeExport += 'module.exports = ' + nsInfo.namespace + ';}';
+
+ output.push(nodeExport);
+ }
+
}
if (options.amd) {
View
28 test/expected/handlebars-node.js
@@ -0,0 +1,28 @@
+var Handlebars = Handlebars || require('Handlebars');
+
+this["JST"] = this["JST"] || {};
+
+Handlebars.registerPartial("partial", Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ helpers = helpers || Handlebars.helpers; data = data || {};
+
+
+
+ return "<span>Canada</span>";}));
+
+this["JST"]["test/fixtures/one.hbs"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ helpers = helpers || Handlebars.helpers; partials = partials || Handlebars.partials; data = data || {};
+ var buffer = "", stack1, foundHelper, functionType="function", escapeExpression=this.escapeExpression, self=this;
+
+
+ buffer += "<p>Hello, my name is ";
+ foundHelper = helpers.name;
+ if (foundHelper) { stack1 = foundHelper.call(depth0, {hash:{},data:data}); }
+ else { stack1 = depth0.name; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
+ buffer += escapeExpression(stack1) + ". I live in ";
+ stack1 = depth0;
+ stack1 = self.invokePartial(partials.partial, 'partial', stack1, helpers, partials, data);
+ if(stack1 || stack1 === 0) { buffer += stack1; }
+ buffer += "</p>";
+ return buffer;});
+
+if (typeof exports === 'object' && exports) {module.exports = this["JST"];}
View
10 test/handlebars_test.js
@@ -11,6 +11,16 @@ exports.handlebars = {
test.done();
},
+ compileNode: function(test) {
+ 'use strict';
+ test.expect(1);
+
+ var actual = grunt.file.read('tmp/handlebars-node.js');
+ var expected = grunt.file.read('test/expected/handlebars-node.js');
+ test.equal(actual, expected, 'should compile as per compile test and also have node directives prepended and appended');
+
+ test.done();
+ },
wrapcompile: function(test) {
'use strict';
test.expect(1);
Please sign in to comment.
Something went wrong with that request. Please try again.