Skip to content

Instantly share code, notes, and snippets.

@thlorenz
Last active December 10, 2015 15:28
Show Gist options
  • Save thlorenz/4454557 to your computer and use it in GitHub Desktop.
Save thlorenz/4454557 to your computer and use it in GitHub Desktop.
A browserify plugin that precompiles handlebars templates .
function handlebarify(bundle) {
var precompile = require('handlebars').precompile;
function handlebarsPlugin(body, file) {
return 'var Handlebars = require(\'handlebars\');\nmodule.exports = ' + precompile(body) + ';';
}
// browserify doesn't allow passing an array of extensions, so we register each separately
bundle.register('.handlebars', handlebarsPlugin);
bundle.register('.hbs', handlebarsPlugin);
}
// use as follows
var bundled = browserify({ debug: true })
.use(handlebarify)
.addEntry('myentry.js')
.bundle();
@thlorenz
Copy link
Author

thlorenz commented Jan 4, 2013

Copying @domenic's comment from the anonymous gist:

Domenic:

Ember encourages ".handlebars" extensions; might as well register those too. See e.g. https://github.com/wycats/handlebars.js/issues/174

@thlorenz
Copy link
Author

thlorenz commented Jan 4, 2013

Ok, I'll add that in and possibly will include a way to have your helpers included, although they could also be required as separate modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment