From b7b78aa0fea5aeb2aef550556abf0aedbc4558c1 Mon Sep 17 00:00:00 2001 From: Caleb Hearon Date: Sun, 25 Oct 2015 14:55:17 -0400 Subject: [PATCH 1/6] a couple indentation fixes --- hbs.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/hbs.js b/hbs.js index 50544a9..b13283c 100644 --- a/hbs.js +++ b/hbs.js @@ -252,16 +252,16 @@ define([ // TODO :: use the parser to do this! function findPartialDeps( nodes , metaObj) { - var res = []; - if ( nodes && nodes.body ) { - res = recursiveNodeSearch( nodes.body, [] ); - } + var res = []; + if ( nodes && nodes.body ) { + res = recursiveNodeSearch( nodes.body, [] ); + } - if(metaObj && metaObj.partials && metaObj.partials.length){ - _(metaObj.partials).forEach(function ( partial ) { - res.push(partial); - }); - } + if(metaObj && metaObj.partials && metaObj.partials.length){ + _(metaObj.partials).forEach(function ( partial ) { + res.push(partial); + }); + } return _.unique(res); } @@ -424,7 +424,7 @@ define([ } function fetchAndRegister(langMap) { - fetchText(path, function(text, path) { + fetchText(path, function(text, path) { var readCallback = (config.isBuild && config[onHbsReadMethod]) ? config[onHbsReadMethod]: function(name,path,text){return text} ; // for some reason it doesn't include hbs _first_ when i don't add it here... From 0d509cd8c063e666fa8057d96a6f15cb5172b14d Mon Sep 17 00:00:00 2001 From: Caleb Hearon Date: Sun, 25 Oct 2015 15:20:30 -0400 Subject: [PATCH 2/6] get rid of pragmas and encourage the use of stubModules instead. The plugin (hbs.js) now uses the full compiler while the example has generated templates and helpers call for 'handlebars' which maps to hbs/handlebars.runtime. They have to reference separate handlebars or requirejs would not be able to build --- demo.html | 23 +++++++++++++++++------ demo/app.build.js | 21 +++++++-------------- demo/template/helpers/myhelper.js | 2 +- demo/template/helpers/yeller.js | 2 +- hbs.js | 16 ++-------------- hbs/json2.js | 2 -- hbs/underscore.js | 2 -- 7 files changed, 28 insertions(+), 40 deletions(-) diff --git a/demo.html b/demo.html index 474d9b1..0f873bc 100755 --- a/demo.html +++ b/demo.html @@ -10,16 +10,27 @@ diff --git a/demo/app.build.js b/demo/app.build.js index 34b9674..f63a21b 100755 --- a/demo/app.build.js +++ b/demo/app.build.js @@ -8,20 +8,11 @@ // inlining ftw inlineText: true, - pragmasOnSave: { - //removes Handlebars.Parser code (used to compile template strings) set - //it to `false` if you need to parse template strings even after build - excludeHbsParser : true, - // kills the entire plugin set once it's built. - excludeHbs: true, - // removes handlebars and json2 - excludeAfterBuild: true - }, - exclude: ["handlebars"], - include: ["handlebars.runtime"], + stubModules: ['hbs', 'hbs/underscore', 'hbs/json2', 'hbs/handlebars'], paths: { - "hbs": "../hbs" + "hbs": "../hbs", + "handlebars": "../hbs/handlebars.runtime" // if your project is already using underscore.js and you want to keep // the hbs plugin even after build (excludeHbs:false) you should set the // "hbs/underscore" path to point to the shared location like @@ -30,10 +21,12 @@ locale: "en_ca", - // default plugin settings, listing here just as a reference hbs : { + // default plugin settings, listing here just as a reference templateExtension : 'hbs', - helperDirectory : "template/helpers/" + helperDirectory : "template/helpers/", + + handlebarsPath: "handlebars" }, modules: [ diff --git a/demo/template/helpers/myhelper.js b/demo/template/helpers/myhelper.js index bed3d04..d32e213 100644 --- a/demo/template/helpers/myhelper.js +++ b/demo/template/helpers/myhelper.js @@ -1,4 +1,4 @@ -define(["hbs/handlebars"], function(Handlebars) { +define(["handlebars"], function(Handlebars) { function myhelper(options) { return options.fn(); } diff --git a/demo/template/helpers/yeller.js b/demo/template/helpers/yeller.js index 82b20be..da92c9a 100755 --- a/demo/template/helpers/yeller.js +++ b/demo/template/helpers/yeller.js @@ -1,4 +1,4 @@ -define(["hbs/handlebars"], function ( Handlebars ){ +define(["handlebars"], function ( Handlebars ){ function yeller ( context, options ) { // Assume it's a string for simplicity. return context + "!!!!!!oneone!!one1"; diff --git a/hbs.js b/hbs.js index b13283c..7bd39e7 100644 --- a/hbs.js +++ b/hbs.js @@ -9,16 +9,7 @@ /*jslint evil: true, strict: false, plusplus: false, regexp: false */ /*global require: false, XMLHttpRequest: false, ActiveXObject: false, define: false, process: false, window: false */ -define([ -//>>excludeStart('excludeHbs', pragmas.excludeHbs) - 'hbs/handlebars', 'hbs/underscore', 'hbs/json2' -//>>excludeEnd('excludeHbs') -], function ( -//>>excludeStart('excludeHbs', pragmas.excludeHbs) - Handlebars, _, JSON -//>>excludeEnd('excludeHbs') -) { - //>>excludeStart('excludeHbs', pragmas.excludeHbs) +define(['hbs/handlebars', 'hbs/underscore', 'hbs/json2'], function (Handlebars, _, JSON) { function precompile(string, _unused, options) { var ast, environment; @@ -192,7 +183,6 @@ define([ }; var styleList = []; var styleMap = {}; - //>>excludeEnd('excludeHbs') var config; var filesToRemove = []; @@ -213,7 +203,6 @@ define([ version: '3.0.3', load: function (name, parentRequire, load, _config) { - //>>excludeStart('excludeHbs', pragmas.excludeHbs) config = config || _config; var compiledName = name + customNameExtension; @@ -582,7 +571,7 @@ define([ var handlebarsPath = (config.hbs && config.hbs.handlebarsPath) ? config.hbs.handlebarsPath : 'hbs/handlebars'; text = '/* START_TEMPLATE */\n' + - 'define('+tmplName+"['hbs','"+handlebarsPath+"'"+depStr+helpDepStr+'], function( hbs, Handlebars ){ \n' + + 'define('+tmplName+"['"+handlebarsPath+"'"+depStr+helpDepStr+'], function( Handlebars ){ \n' + 'var t = Handlebars.template(' + prec + ');\n' + "Handlebars.registerPartial('" + name + "', t);\n"; @@ -648,7 +637,6 @@ define([ } fetchAndRegister(false); - //>>excludeEnd('excludeHbs') }, onLayerEnd: function () { diff --git a/hbs/json2.js b/hbs/json2.js index 02a87e8..9b45d9a 100755 --- a/hbs/json2.js +++ b/hbs/json2.js @@ -1,4 +1,3 @@ -//>>excludeStart('excludeAfterBuild', pragmas.excludeAfterBuild) /* http://www.JSON.org/json2.js 2011-10-19 @@ -362,4 +361,3 @@ define(function(){ // otherwise just leave it alone }).call(this, this); -//>>excludeEnd('excludeAfterBuild') diff --git a/hbs/underscore.js b/hbs/underscore.js index cf6e2bc..0ce45a2 100755 --- a/hbs/underscore.js +++ b/hbs/underscore.js @@ -1,4 +1,3 @@ -//>>excludeStart('excludeAfterBuild', pragmas.excludeAfterBuild) // Underscore.js 1.3.3 // (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc. @@ -1042,4 +1041,3 @@ define(function() { return _; }); -//>>excludeEnd('excludeAfterBuild') From e5c0dd20ee79ce6995a5a4a052833672ec253472 Mon Sep 17 00:00:00 2001 From: Caleb Hearon Date: Sun, 25 Oct 2015 15:32:32 -0400 Subject: [PATCH 3/6] update README w note on using handlebarsPath for builds --- README.md | 11 ++++++----- demo/app.build.js | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f5e773d..2513d52 100755 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Just put your helpers in `templates/helpers/*` and they'll automagically get pul I find that many helpers are good helpers in regular code as well, so the following is a good practice: ```javascript -define('templates/helpers/roundNumber', ['hbs/handlebars'], function ( Handlebars ) { +define('templates/helpers/roundNumber', ['handlebars'], function ( Handlebars ) { function roundNumber ( context, options ) { // Simple function for example return Math.round( context ); @@ -233,10 +233,11 @@ require.config({ templateExtension: "html" // Set the extension automatically appended to templates // ('hbs' by default) - handlebarsPath: // Custom path to handlebars ('hbs/handlebars' by default) - 'some/path/to/handlebars' // Value could simply be 'handlebars' as long as key - // 'handlebars' is defined in require's paths object - // such as 'handlebars': 'some/path/to/handlebars' + handlebarsPath: // Custom path to handlebars for compiled templates + 'some/path/to/handlebars' // ('hbs/handlebars' by default). Could simply be 'handlebars' + // as long as it is defined in paths. If you are stubbing out + // the plugin in an optimized build, use this to point to + // the runtime hbs (see demo/app.build.js) compileOptions: {} // options object which is passed to Handlebars compiler } diff --git a/demo/app.build.js b/demo/app.build.js index f63a21b..25d1867 100755 --- a/demo/app.build.js +++ b/demo/app.build.js @@ -4,7 +4,7 @@ dir: "../demo-build", optimizeCss: "standard", - // optimize: "none", + optimize: "none", // inlining ftw inlineText: true, From 445f0b6f7e954ad78626a235db9c338f15cb2a8a Mon Sep 17 00:00:00 2001 From: Caleb Hearon Date: Sun, 25 Oct 2015 16:16:09 -0400 Subject: [PATCH 4/6] add a more detailed description of the optimized build --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 2513d52..d332c30 100755 --- a/README.md +++ b/README.md @@ -174,6 +174,13 @@ Note: All of these go away after a build, as they just take up space with data t As long as all of your paths match up, this should precompile all of your templates and include them in the build. +You can stub out the hbs plugin in the build using `[stubModules](https://github.com/jrburke/r.js/blob/master/build/example.build.js#L316)` the same way you would for other plugins that inline the final function. Your helpers and compiled templates still need to load Handlebars though, so you'll need to make sure they load the runtime version of Handlebars included in this repo as `hbs/handlebars.runtime.js`. You just need to do 2 things for that in your build config: + +1. Make sure hbs.handlebarsPath resolves to hbs/handlebars.runtime.js +2. Make sure your helpers load the same file + +See the [example build configuration](https://github.com/chearon/require-handlebars-plugin/blob/master/demo/app.build.js) for a way to do that by setting `handlebarsPath` to `handlebars`, having the helpers load `handlebars`, and setting `handlebars` to hbs/handlebars.runtime in the paths.config + ## Before Build ![Before Build](http://i.imgur.com/YSTI3.jpg) From bfe86535100bc20b4cddfc5ac448549e5b5983dc Mon Sep 17 00:00:00 2001 From: Caleb Hearon Date: Sun, 25 Oct 2015 16:18:22 -0400 Subject: [PATCH 5/6] oops, re-comment optimize=none that was accidentally committed --- demo/app.build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/app.build.js b/demo/app.build.js index 25d1867..f63a21b 100755 --- a/demo/app.build.js +++ b/demo/app.build.js @@ -4,7 +4,7 @@ dir: "../demo-build", optimizeCss: "standard", - optimize: "none", + // optimize: "none", // inlining ftw inlineText: true, From 420ea7bd6bc2fa8efced1edaf05ca0d5fe6d03c3 Mon Sep 17 00:00:00 2001 From: Caleb Hearon Date: Sun, 25 Oct 2015 16:19:39 -0400 Subject: [PATCH 6/6] Fix a couple of typos in README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d332c30..63bcc36 100755 --- a/README.md +++ b/README.md @@ -174,12 +174,12 @@ Note: All of these go away after a build, as they just take up space with data t As long as all of your paths match up, this should precompile all of your templates and include them in the build. -You can stub out the hbs plugin in the build using `[stubModules](https://github.com/jrburke/r.js/blob/master/build/example.build.js#L316)` the same way you would for other plugins that inline the final function. Your helpers and compiled templates still need to load Handlebars though, so you'll need to make sure they load the runtime version of Handlebars included in this repo as `hbs/handlebars.runtime.js`. You just need to do 2 things for that in your build config: +You can stub out the hbs plugin in the build using [stubModules](https://github.com/jrburke/r.js/blob/master/build/example.build.js#L316) the same way you would for other plugins that inline the final function. Your helpers and compiled templates still need to load Handlebars though, so you'll need to make sure they load the runtime version of Handlebars included in this repo as `hbs/handlebars.runtime.js`. You just need to do 2 things for that in your build config: 1. Make sure hbs.handlebarsPath resolves to hbs/handlebars.runtime.js 2. Make sure your helpers load the same file -See the [example build configuration](https://github.com/chearon/require-handlebars-plugin/blob/master/demo/app.build.js) for a way to do that by setting `handlebarsPath` to `handlebars`, having the helpers load `handlebars`, and setting `handlebars` to hbs/handlebars.runtime in the paths.config +See the [example build configuration](https://github.com/SlexAxton/require-handlebars-plugin/blob/master/demo/app.build.js) for a way to do that by setting `handlebarsPath` to `handlebars`, having the helpers load `handlebars`, and setting `handlebars` to hbs/handlebars.runtime in the paths.config ## Before Build