Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RequireJS loader plugin for returning a compiled template. Underscore is used here - swap out for your favourite template library.
// RequireJS loader plugin for returning a compiled template
// Underscore is used here, swap out for your favourite template library
define(['text', 'underscore'], function(textLoader, _) {
var buildMap = {};
return {
load: function(name, req, onload, config) {
onTextLoad = function(content) {
var compiledTemplate = _.template(content);
if (config.isBuild) {
buildMap[name] = compiledTemplate;
}
onload(compiledTemplate);
}
textLoader.load(name, req, onTextLoad, config);
},
// for the optimiser:
write: function(pluginName, moduleName, write) {
if (buildMap.hasOwnProperty(moduleName)) {
var content = textLoader.jsEscape(buildMap[moduleName]);
write.asModule(pluginName + "!" + moduleName, "define(function() { return " + content + "; });\n");
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.