Created
August 22, 2013 21:14
-
-
Save fabslab/6312881 to your computer and use it in GitHub Desktop.
RequireJS loader plugin for returning a compiled template. Underscore is used here - swap out for your favourite template library.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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