Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pvmsikrsna/666651045d43a61ec4e3 to your computer and use it in GitHub Desktop.
Save pvmsikrsna/666651045d43a61ec4e3 to your computer and use it in GitHub Desktop.
GULP task for compiling HTMLBARS templates for use in the browser
var htmlbars = require("gulp-htmlbars");
var tap = require("gulp-tap");
var concat = require("gulp-concat");
var getTemplateNameFromPath = function(path){
// if exist replace \ with /
while( path.indexOf("\\") !== -1 ){
path = path.replace("\\", "/");
}
var splitPath = path.split("/");
var filenameWithExtension = splitPath[splitPath.length-1];
var folderNameInWhichFileResides = splitPath[splitPath.length-2];
var lastDotIndex = filenameWithExtension.lastIndexOf(".");
var filenameWithoutExtension = filenameWithExtension.substring(0, lastDotIndex);
var finalTemplateName = filenameWithoutExtension;
if( folderNameInWhichFileResides === "components" ){
finalTemplateName = "components/" + finalTemplateName;
}
return finalTemplateName;
};
gulp.task("compileTemplates", function(){
gulp.src("app/templates/**/*.hbs")
.pipe(htmlbars({
isHTMLBars: true,
templateCompiler: require("./bower_components/ember/ember-template-compiler")
}))
.pipe(tap(function(file){
var templateName = getTemplateNameFromPath(file.path.toString());
var currentFile = file.contents.toString();
currentFile = currentFile.replace("export default", "Ember.TEMPLATES['" + templateName + "'] = ");
file.contents = new Buffer( currentFile );
}))
.pipe(concat("compiledTemplates.js"))
.pipe(gulp.dest("tmp"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment