From 531be77138aab9e7d06873d8051236e154050ebf Mon Sep 17 00:00:00 2001 From: Jesse Clark Date: Tue, 14 May 2013 22:26:20 +1000 Subject: [PATCH 1/3] Publish a Node.js require() handler for handlebars files --- lib/handlebars.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/handlebars.js b/lib/handlebars.js index 7d85c2610..7767b7a83 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -21,6 +21,17 @@ Handlebars.create = create; module.exports = Handlebars; // instantiate an instance +// Publish a Node.js require() handler for handlebars files +if ((typeof(require) !== "undefined") && require.extensions) { + var extension = function(module, filename) { + var fs = require("fs"); + var templateString = fs.readFileSync(filename, 'utf8'); + module.exports = Handlebars.compile(templateString); + }; + require.extensions[".handlebars"] = extension; + require.extensions[".hbs"] = extension; +} + // BEGIN(BROWSER) // END(BROWSER) From 229b82bef56961c62538ca30fbd8c120e63a4494 Mon Sep 17 00:00:00 2001 From: Jesse Clark Date: Wed, 15 May 2013 10:38:45 +1000 Subject: [PATCH 2/3] Add unit tests for require() handler --- lib/handlebars.js | 18 +++++++++--------- spec/example_1.handlebars | 1 + spec/example_2.hbs | 1 + spec/qunit_spec.js | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 spec/example_1.handlebars create mode 100644 spec/example_2.hbs diff --git a/lib/handlebars.js b/lib/handlebars.js index 7767b7a83..f82ec3bad 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -21,15 +21,15 @@ Handlebars.create = create; module.exports = Handlebars; // instantiate an instance -// Publish a Node.js require() handler for handlebars files -if ((typeof(require) !== "undefined") && require.extensions) { - var extension = function(module, filename) { - var fs = require("fs"); - var templateString = fs.readFileSync(filename, 'utf8'); - module.exports = Handlebars.compile(templateString); - }; - require.extensions[".handlebars"] = extension; - require.extensions[".hbs"] = extension; +// Publish a Node.js require() handler for .handlebars and .hbs files +if (require.extensions) { + var extension = function(module, filename) { + var fs = require("fs"); + var templateString = fs.readFileSync(filename, "utf8"); + module.exports = Handlebars.compile(templateString); + }; + require.extensions[".handlebars"] = extension; + require.extensions[".hbs"] = extension; } // BEGIN(BROWSER) diff --git a/spec/example_1.handlebars b/spec/example_1.handlebars new file mode 100644 index 000000000..054e96cb8 --- /dev/null +++ b/spec/example_1.handlebars @@ -0,0 +1 @@ +{{foo}} diff --git a/spec/example_2.hbs b/spec/example_2.hbs new file mode 100644 index 000000000..963eab972 --- /dev/null +++ b/spec/example_2.hbs @@ -0,0 +1 @@ +Hello, {{name}}! diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index e2f7d47d0..717aa23c0 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -1475,6 +1475,26 @@ test('GH-375: Unicode line terminators', function() { shouldCompileTo('\u2028', {}, '\u2028'); }); +test('Load .handlebars files with require()', function() { + var template = require("./example_1"); + assert.deepEqual(template, require("./example_1.handlebars")); + + var expected = 'foo\n'; + var result = template({foo: "foo"}); + + equal(result, expected); +}); + +test('Load .hbs files with require()', function() { + var template = require("./example_2"); + assert.deepEqual(template, require("./example_2.hbs")); + + var expected = 'Hello, World!\n'; + var result = template({name: "World"}); + + equal(result, expected); +}); + suite('Utils'); test('escapeExpression', function() { From d649b3581e0794fcdc6aab58d84058b1fe5ee7e5 Mon Sep 17 00:00:00 2001 From: Jesse Clark Date: Wed, 15 May 2013 10:50:01 +1000 Subject: [PATCH 3/3] Only run "require" tests when require() is available --- spec/qunit_spec.js | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index 717aa23c0..fef5166bc 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -1475,26 +1475,6 @@ test('GH-375: Unicode line terminators', function() { shouldCompileTo('\u2028', {}, '\u2028'); }); -test('Load .handlebars files with require()', function() { - var template = require("./example_1"); - assert.deepEqual(template, require("./example_1.handlebars")); - - var expected = 'foo\n'; - var result = template({foo: "foo"}); - - equal(result, expected); -}); - -test('Load .hbs files with require()', function() { - var template = require("./example_2"); - assert.deepEqual(template, require("./example_2.hbs")); - - var expected = 'Hello, World!\n'; - var result = template({name: "World"}); - - equal(result, expected); -}); - suite('Utils'); test('escapeExpression', function() { @@ -1522,3 +1502,27 @@ test('isEmpty', function() { equal(Handlebars.Utils.isEmpty('foo'), false); equal(Handlebars.Utils.isEmpty({bar: 1}), false); }); + +if (typeof(require) !== 'undefined') { + suite('Require'); + + test('Load .handlebars files with require()', function() { + var template = require("./example_1"); + assert.deepEqual(template, require("./example_1.handlebars")); + + var expected = 'foo\n'; + var result = template({foo: "foo"}); + + equal(result, expected); + }); + + test('Load .hbs files with require()', function() { + var template = require("./example_2"); + assert.deepEqual(template, require("./example_2.hbs")); + + var expected = 'Hello, World!\n'; + var result = template({name: "World"}); + + equal(result, expected); + }); +}