Skip to content

Instantly share code, notes, and snippets.

@garth
Last active August 29, 2015 14:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garth/dffa6c01867e6500309e to your computer and use it in GitHub Desktop.
Save garth/dffa6c01867e6500309e to your computer and use it in GitHub Desktop.
Grunt task to pre-compile HTMLBars templates
'use strict';
var fs = require('fs');
var path = require('path');
var templateCompiler = require('./bower_components/ember/ember-template-compiler');
var templatesPath = 'templates'
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('./package.json'),
compileHtmlBars: {
build: {
templateBasePath: templatesPath,
src: [templatesPath + '**/*.hbs'],
dest: 'templates.js'
}
}
});
grunt.registerMultiTask('compileHtmlBars', 'Pre-compile HTMLBars tempates', function() {
var done = this.async();
this.files.forEach(function (file) {
var stream = fs.createWriteStream(path.join(__dirname, file.dest), {
encoding: 'utf8'
});
stream.once('open', function (fd) {
grunt.log.writeln('Pre-compiling ' + file.src.length + ' handlebars templates...');
// process each template
file.src.forEach(function (f) {
// load the template
var template = fs.readFileSync(path.join(__dirname, f), {
encoding: 'utf8'
});
var name = f.replace(new RegExp('^' + file.templateBasePath), '').replace(/\.hbs$/, '');
// compile the template
stream.write('Ember.TEMPLATES["' + name + '"] = Ember.HTMLBars.template(');
stream.write(templateCompiler.precompile(template) + ');\n\n');
});
stream.end(done);
});
});
});
};
@garth
Copy link
Author

garth commented Dec 24, 2014

Updated for ember 1.10.0-beta.2

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