Skip to content

Instantly share code, notes, and snippets.

@cmsd2
Created November 14, 2012 15:30
Show Gist options
  • Save cmsd2/4072773 to your computer and use it in GitHub Desktop.
Save cmsd2/4072773 to your computer and use it in GitHub Desktop.
Server-side handlebar template compilation in a requirejs plugin
define(["text","hbs"], function(text,hbs) {
"use strict";
var buildMap = {};
var module = {
load: function(name, parentRequire, load, config) {
parentRequire(["hbs!" + name], function(hbsTemplate) {
var rawText = hbsTemplate({});
buildMap[name] = rawText;
load(rawText);
});
},
write: function(pluginName, moduleName, write) {
if(moduleName in buildMap && buildMap[moduleName]) {
var rawText = buildMap[moduleName];
var escapedText = text.jsEscape(rawText);
write("define('" + pluginName + "!" + moduleName +
"', function() { return '" + escapedText + "';});\n");
}
}
};
return module;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment