diff --git a/README.md b/README.md index f5e773d..63bcc36 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 ); @@ -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/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 ![Before Build](http://i.imgur.com/YSTI3.jpg) @@ -233,10 +240,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.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 50544a9..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; @@ -252,16 +241,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 +413,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... @@ -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')