Skip to content

Commit

Permalink
Merge pull request #245 from chearon/issue-244
Browse files Browse the repository at this point in the history
Issue 244
  • Loading branch information
SlexAxton committed Nov 17, 2015
2 parents 322ae88 + 420ea7b commit 1837ba9
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 55 deletions.
18 changes: 13 additions & 5 deletions README.md
Expand Up @@ -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 );
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 17 additions & 6 deletions demo.html
Expand Up @@ -10,16 +10,27 @@
<!-- If you set the require variable to an object, it automatically is the config :D -->
<script>require = {
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/',

// specifies what module the compiled template functions should ask for
// in their define() dependency list. It could be hbs/handlebars or
// hbs/handlebars.runtime or your own handlebars
handlebarsPath: 'handlebars'
},
// I change the path as to not duplicate the hbs.js and handlebars plugin.
// Normally, just drop it in the same place as require.js and it'll work fine.
// Essentially just ignore this.
paths : {
'hbs' : '../hbs'
// I change the path as to not duplicate the hbs.js and handlebars plugin.
// Normally, just drop it in the same place as require.js and it'll work fine.
// Essentially just ignore this.
'hbs' : '../hbs',

// Compiled templates will be loading 'handlebars' because of the handlebarsPath
// set above so point that to the runtime handlebars. It could be hbs/handlebars
// but that is really only needed by hbs.js, plus requirejs makes you only have
// one reference to an anonymous module
'handlebars': '../hbs/handlebars.runtime'
}
};</script>
<!-- use a common require.js and app injection method. -->
Expand Down
21 changes: 7 additions & 14 deletions demo/app.build.js
Expand Up @@ -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
Expand All @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion demo/template/helpers/myhelper.js
@@ -1,4 +1,4 @@
define(["hbs/handlebars"], function(Handlebars) {
define(["handlebars"], function(Handlebars) {
function myhelper(options) {
return options.fn();
}
Expand Down
2 changes: 1 addition & 1 deletion 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";
Expand Down
36 changes: 12 additions & 24 deletions hbs.js
Expand Up @@ -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;

Expand Down Expand Up @@ -192,7 +183,6 @@ define([
};
var styleList = [];
var styleMap = {};
//>>excludeEnd('excludeHbs')

var config;
var filesToRemove = [];
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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...
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -648,7 +637,6 @@ define([
}

fetchAndRegister(false);
//>>excludeEnd('excludeHbs')
},

onLayerEnd: function () {
Expand Down
2 changes: 0 additions & 2 deletions hbs/json2.js
@@ -1,4 +1,3 @@
//>>excludeStart('excludeAfterBuild', pragmas.excludeAfterBuild)
/*
http://www.JSON.org/json2.js
2011-10-19
Expand Down Expand Up @@ -362,4 +361,3 @@ define(function(){
// otherwise just leave it alone

}).call(this, this);
//>>excludeEnd('excludeAfterBuild')
2 changes: 0 additions & 2 deletions hbs/underscore.js
@@ -1,4 +1,3 @@
//>>excludeStart('excludeAfterBuild', pragmas.excludeAfterBuild)

// Underscore.js 1.3.3
// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
Expand Down Expand Up @@ -1042,4 +1041,3 @@ define(function() {
return _;

});
//>>excludeEnd('excludeAfterBuild')

0 comments on commit 1837ba9

Please sign in to comment.