Last active
August 29, 2015 14:11
-
-
Save garth/dffa6c01867e6500309e to your computer and use it in GitHub Desktop.
Grunt task to pre-compile HTMLBars templates
This file contains 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
'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); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for ember 1.10.0-beta.2