Skip to content

Instantly share code, notes, and snippets.

@gabrielgrant
Forked from zilkey/ember-precompile.js
Created September 15, 2012 04:11
Show Gist options
  • Save gabrielgrant/3726323 to your computer and use it in GitHub Desktop.
Save gabrielgrant/3726323 to your computer and use it in GitHub Desktop.
Precompile .handlebars templates with node js
var fs = require('fs');
var path = require('path');
var vm = require('vm');
var argv = require('optimist').argv;
function compileHandlebarsTemplate(file, onComplete) {
//dummy jQuery
var jQuery = function () { return jQuery }
jQuery.ready = function () { return jQuery }
jQuery.inArray = function () { return jQuery }
jQuery.jquery = "1.7.1"
jQuery.event = { fixHooks: {} }
//dummy DOM element
var element = {
firstChild: function () { return element },
innerHTML: function () { return element }
}
var sandbox = {
// DOM
document: {
createRange: false,
createElement: function () { return element }
},
// Console
console: console,
// jQuery
jQuery: jQuery,
$: jQuery,
// handlebars template to compile
template: fs.readFileSync(file, 'utf8'),
// compiled handlebars template
templatejs: null
}
// window
sandbox.window = sandbox
// create a context for the vm using the sandbox data
var context = vm.createContext(sandbox)
// load Handlebars and Ember into the sandbox
vm.runInContext(handlebarsjs, context, 'ember.js')
vm.runInContext(emberjs, context, 'ember.js')
//compile the handlebars template inside the vm context
vm.runInContext('templatejs = Ember.Handlebars.precompile(template).toString()', context)
var fileName = path.basename(file)
var namedTemplateJs = 'Ember.TEMPLATES["' +
fileName.replace(/.handlebars/, '') +
'"] = Ember.Handlebars.template(' + context.templatejs + ');'
return namedTemplateJs;
//extract the compiled template from the vm context and save to .js file,
//implicitely adding template to Ember.TEMPLATES when it is required
}
var handlebarsjs = fs.readFileSync('polldemo/polldemo/polls/static/polls/lib/handlebars-1.0.0.beta.6.js', 'utf8')
var emberjs = fs.readFileSync('polldemo/polldemo/polls/static/polls/lib/ember-1.0.pre.js', 'utf8')
var templatesDir = 'polldemo/polldemo/polls/static/polls/templates'
var usage = '\n \
Precompile handlebar templates for Ember.js.\n \
Usage: ' + argv.$0 + ' template...\n\n \
Options:\n \
-f, --output Output File\n'
function main(){
if ((argv.h)||(argv.help)) {
console.log(usage);
process.exit(0);
}
var inputFiles = argv._;
var outputFile = argv.output || argv.f;
if (outputFile) {
// create a blank output file
fs.writeFileSync(outputFile, '', 'utf8');
function write(output){
fs.appendFileSync(outputFile, output + '\n', 'utf8');
}
}
else {
var write = console.log;
}
inputFiles.forEach(function(fileName){
write(compileHandlebarsTemplate(fileName));
});
}
main();
@gabrielgrant
Copy link
Author

This cleaned up version works more similarly to the original Handlebars.js compile script: so long as you're just using the -f/--output flag it should function as a drop-in replacement.

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