From 97b492fddb2115d489304137187230490c0f8bed Mon Sep 17 00:00:00 2001 From: KahWee Teng Date: Tue, 18 Feb 2014 01:40:15 +0800 Subject: [PATCH 1/4] Added partials for wrapped and unwrapped functions. --- index.js | 9 +++++ test/expected/_Partial_bare.js | 8 +++++ test/expected/_Partial_bare_unwrapped.js | 8 +++++ test/fixtures/_Partial.hbs | 1 + test/main.js | 45 +++++++++++++++++++++++- 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 test/expected/_Partial_bare.js create mode 100644 test/expected/_Partial_bare_unwrapped.js create mode 100644 test/fixtures/_Partial.hbs diff --git a/index.js b/index.js index 688eda9..6ae15c3 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ module.exports = function(options) { options = extend({ compilerOptions: {}, wrapped: false, + partial: false, outputType: 'bare' // amd, commonjs, node, bare }, options); @@ -25,6 +26,14 @@ module.exports = function(options) { if (options.wrapped) { compiled = 'Handlebars.template('+compiled+')'; } + if (options.partial) { + var nodePath = require('path'); + var partialName = nodePath.basename(path, '.hbs'); + if (partialName.charAt(0) === '_') { + partialName = partialName.substr(1); + } + compiled = 'Handlebars.registerPartial('+JSON.stringify(partialName)+', '+compiled+');'; + } // Handle different output times if (options.outputType === 'amd') { diff --git a/test/expected/_Partial_bare.js b/test/expected/_Partial_bare.js new file mode 100644 index 0000000..fc9b146 --- /dev/null +++ b/test/expected/_Partial_bare.js @@ -0,0 +1,8 @@ +Handlebars.registerPartial("Partial", Handlebars.template(function (Handlebars,depth0,helpers,partials,data) { + this.compilerInfo = [4,'>= 1.0.0']; +helpers = this.merge(helpers, Handlebars.helpers); data = data || {}; + + + + return "Partial template"; + })); \ No newline at end of file diff --git a/test/expected/_Partial_bare_unwrapped.js b/test/expected/_Partial_bare_unwrapped.js new file mode 100644 index 0000000..987e806 --- /dev/null +++ b/test/expected/_Partial_bare_unwrapped.js @@ -0,0 +1,8 @@ +Handlebars.registerPartial("Partial", function (Handlebars,depth0,helpers,partials,data) { + this.compilerInfo = [4,'>= 1.0.0']; +helpers = this.merge(helpers, Handlebars.helpers); data = data || {}; + + + + return "Partial template"; + }); \ No newline at end of file diff --git a/test/fixtures/_Partial.hbs b/test/fixtures/_Partial.hbs new file mode 100644 index 0000000..c2b89db --- /dev/null +++ b/test/fixtures/_Partial.hbs @@ -0,0 +1 @@ +Partial template \ No newline at end of file diff --git a/test/main.js b/test/main.js index 3df57ce..d3e6a07 100644 --- a/test/main.js +++ b/test/main.js @@ -25,6 +25,11 @@ var fileMatchesExpected = function(file, fixtureFilename) { String(file.contents).should.equal(getExpectedString(fixtureFilename)); }; +var fileMatchesExpectedPartial = function(file, fixtureFilename) { + path.basename(file.path).should.equal('_Partial.js'); + String(file.contents).should.equal(getExpectedString(fixtureFilename)); +}; + describe('gulp-handlebars', function() { describe('handlebarsPlugin()', function() { @@ -43,7 +48,7 @@ describe('gulp-handlebars', function() { }); var invalidTemplate = getFixture('Invalid.hbs'); - + stream.on('error', function(err) { err.should.be.an.instanceOf(Error); err.message.should.equal("Parse error on line 1:\n" + @@ -172,5 +177,43 @@ describe('gulp-handlebars', function() { stream.end(); }); + it('should compile unwrapped bare partials', function(done) { + var stream = handlebarsPlugin({ + partial: true, + outputType: 'bare', + wrapped: false + }); + + var partialTemplate = getFixture('_Partial.hbs'); + + stream.on('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.contents); + fileMatchesExpectedPartial(newFile, '_Partial_bare_unwrapped.js'); + done(); + }); + stream.write(partialTemplate); + stream.end(); + }); + + it('should compile wrapped bare partials', function(done) { + var stream = handlebarsPlugin({ + partial: true, + outputType: 'bare', + wrapped: true + }); + + var partialTemplate = getFixture('_Partial.hbs'); + + stream.on('data', function(newFile) { + should.exist(newFile); + should.exist(newFile.contents); + fileMatchesExpectedPartial(newFile, '_Partial_bare.js'); + done(); + }); + stream.write(partialTemplate); + stream.end(); + }); + }); }); From e97ceec8c15ba187a4834574b320ffa847c37309 Mon Sep 17 00:00:00 2001 From: KahWee Teng Date: Wed, 19 Feb 2014 01:19:43 +0800 Subject: [PATCH 2/4] Revert 18fc209..97b492f This rolls back to commit 18fc20960ccae1478a030374717bf10f7c8322b1. --- index.js | 9 ----- test/expected/_Partial_bare.js | 8 ----- test/expected/_Partial_bare_unwrapped.js | 8 ----- test/fixtures/_Partial.hbs | 1 - test/main.js | 45 +----------------------- 5 files changed, 1 insertion(+), 70 deletions(-) delete mode 100644 test/expected/_Partial_bare.js delete mode 100644 test/expected/_Partial_bare_unwrapped.js delete mode 100644 test/fixtures/_Partial.hbs diff --git a/index.js b/index.js index 6ae15c3..688eda9 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,6 @@ module.exports = function(options) { options = extend({ compilerOptions: {}, wrapped: false, - partial: false, outputType: 'bare' // amd, commonjs, node, bare }, options); @@ -26,14 +25,6 @@ module.exports = function(options) { if (options.wrapped) { compiled = 'Handlebars.template('+compiled+')'; } - if (options.partial) { - var nodePath = require('path'); - var partialName = nodePath.basename(path, '.hbs'); - if (partialName.charAt(0) === '_') { - partialName = partialName.substr(1); - } - compiled = 'Handlebars.registerPartial('+JSON.stringify(partialName)+', '+compiled+');'; - } // Handle different output times if (options.outputType === 'amd') { diff --git a/test/expected/_Partial_bare.js b/test/expected/_Partial_bare.js deleted file mode 100644 index fc9b146..0000000 --- a/test/expected/_Partial_bare.js +++ /dev/null @@ -1,8 +0,0 @@ -Handlebars.registerPartial("Partial", Handlebars.template(function (Handlebars,depth0,helpers,partials,data) { - this.compilerInfo = [4,'>= 1.0.0']; -helpers = this.merge(helpers, Handlebars.helpers); data = data || {}; - - - - return "Partial template"; - })); \ No newline at end of file diff --git a/test/expected/_Partial_bare_unwrapped.js b/test/expected/_Partial_bare_unwrapped.js deleted file mode 100644 index 987e806..0000000 --- a/test/expected/_Partial_bare_unwrapped.js +++ /dev/null @@ -1,8 +0,0 @@ -Handlebars.registerPartial("Partial", function (Handlebars,depth0,helpers,partials,data) { - this.compilerInfo = [4,'>= 1.0.0']; -helpers = this.merge(helpers, Handlebars.helpers); data = data || {}; - - - - return "Partial template"; - }); \ No newline at end of file diff --git a/test/fixtures/_Partial.hbs b/test/fixtures/_Partial.hbs deleted file mode 100644 index c2b89db..0000000 --- a/test/fixtures/_Partial.hbs +++ /dev/null @@ -1 +0,0 @@ -Partial template \ No newline at end of file diff --git a/test/main.js b/test/main.js index d3e6a07..3df57ce 100644 --- a/test/main.js +++ b/test/main.js @@ -25,11 +25,6 @@ var fileMatchesExpected = function(file, fixtureFilename) { String(file.contents).should.equal(getExpectedString(fixtureFilename)); }; -var fileMatchesExpectedPartial = function(file, fixtureFilename) { - path.basename(file.path).should.equal('_Partial.js'); - String(file.contents).should.equal(getExpectedString(fixtureFilename)); -}; - describe('gulp-handlebars', function() { describe('handlebarsPlugin()', function() { @@ -48,7 +43,7 @@ describe('gulp-handlebars', function() { }); var invalidTemplate = getFixture('Invalid.hbs'); - + stream.on('error', function(err) { err.should.be.an.instanceOf(Error); err.message.should.equal("Parse error on line 1:\n" + @@ -177,43 +172,5 @@ describe('gulp-handlebars', function() { stream.end(); }); - it('should compile unwrapped bare partials', function(done) { - var stream = handlebarsPlugin({ - partial: true, - outputType: 'bare', - wrapped: false - }); - - var partialTemplate = getFixture('_Partial.hbs'); - - stream.on('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.contents); - fileMatchesExpectedPartial(newFile, '_Partial_bare_unwrapped.js'); - done(); - }); - stream.write(partialTemplate); - stream.end(); - }); - - it('should compile wrapped bare partials', function(done) { - var stream = handlebarsPlugin({ - partial: true, - outputType: 'bare', - wrapped: true - }); - - var partialTemplate = getFixture('_Partial.hbs'); - - stream.on('data', function(newFile) { - should.exist(newFile); - should.exist(newFile.contents); - fileMatchesExpectedPartial(newFile, '_Partial_bare.js'); - done(); - }); - stream.write(partialTemplate); - stream.end(); - }); - }); }); From d3d003034824794bea537c901c33ca9cc1e17782 Mon Sep 17 00:00:00 2001 From: KahWee Teng Date: Wed, 19 Feb 2014 02:27:47 +0800 Subject: [PATCH 3/4] Add documentation on compiling partial for the browser using gulp-wrap. --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d2155a0..4fecdb9 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,29 @@ gulp.task('templates', function(){ }); ``` +## Compiling to a partial for the browser + +[gulp-wrap] can be used to compile templates for the browser. Just pipe the output of gulp-handlebars to gulp-declare: + +```javascript +var handlebars = require('gulp-handlebars'); +var wrap = require('gulp-wrap'); + +gulp.task('templates', function() { + gulp.src(['client/templates/_*.hbs']) + .pipe(handlebars()) + .pipe(wrap('Handlebars.registerPartial(<%= processPartialName(file.relative) %>, Handlebars.template(<%= contents %>))', {}, { + 'imports': { + 'processPartialName': function(fileName) { + return JSON.stringify( + require('path').basename(fileName, '.js').substr(1) + ); + } + } + })) + .pipe(gulp.dest('build/js/')); +}); +``` ## API @@ -75,4 +98,5 @@ Compiler options to pass to `Handlebars.precompile()`. [npm-url]: https://npmjs.org/package/gulp-handlebars [npm-image]: https://badge.fury.io/js/gulp-handlebars.png -[gulp-declare]: https://github.com/lazd/gulp-declare \ No newline at end of file +[gulp-declare]: https://github.com/lazd/gulp-declare +[gulp-wrap]: https://github.com/adamayres/gulp-wrap \ No newline at end of file From e1d07ed340a64ad1bcb25089ed32ff1e68735d33 Mon Sep 17 00:00:00 2001 From: KahWee Teng Date: Wed, 19 Feb 2014 07:52:57 +0800 Subject: [PATCH 4/4] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4fecdb9..3294fdd 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ gulp.task('templates', function(){ ## Compiling to a namespace for the browser -[gulp-declare] can be used to compile templates for the browser. Just pipe the output of gulp-handlebars to gulp-declare: +`[gulp-declare]` can be used to compile templates for the browser. Just pipe the output of gulp-handlebars to gulp-declare: ```javascript var handlebars = require('gulp-handlebars'); @@ -44,7 +44,7 @@ gulp.task('templates', function(){ ## Compiling to a partial for the browser -[gulp-wrap] can be used to compile templates for the browser. Just pipe the output of gulp-handlebars to gulp-declare: +`[gulp-wrap]` can be used to compile templates for the browser. Just pipe the output of gulp-handlebars to gulp-declare: ```javascript var handlebars = require('gulp-handlebars'); @@ -99,4 +99,4 @@ Compiler options to pass to `Handlebars.precompile()`. [npm-image]: https://badge.fury.io/js/gulp-handlebars.png [gulp-declare]: https://github.com/lazd/gulp-declare -[gulp-wrap]: https://github.com/adamayres/gulp-wrap \ No newline at end of file +[gulp-wrap]: https://github.com/adamayres/gulp-wrap