Created
September 18, 2012 06:58
-
-
Save dmhaiduchonak/3741698 to your computer and use it in GitHub Desktop.
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
var fs = require('fs'); | |
var vm = require('vm'); | |
var log = console.log; | |
var emberjs = fs.readFileSync('client/code/libs/ember-1.0.pre.min.js', 'utf8'); | |
var handlebarsjs = fs.readFileSync('client/code/libs/handlebars-1.0.0.beta.6.js', 'utf8'); | |
var system = require('../system'); | |
var uglifyjs = require('uglify-js'); | |
exports.init = function(root, config) { | |
return { | |
name: 'Ember.js', | |
process: function(template, path, id) { | |
var templatesDir = 'client/static/assets/main'; | |
var fileName = id; | |
var file = templatesDir + '/' + fileName; | |
//dummy jQuery | |
var jQuery = function() { return jQuery; }; | |
jQuery.ready = function() { return jQuery; }; | |
jQuery.inArray = function() { return jQuery; }; | |
jQuery.jquery = "1.7.2"; | |
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: 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 Ember into the sandbox | |
vm.runInContext(handlebarsjs, context, 'handlebars.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 namedTemplateJs = 'Ember.TEMPLATES["' + fileName.replace(/.html/, '') + '"] = Handlebars.template(' + context.templatejs + ');'; | |
//extract the compiled template from the vm context and save to .js file | |
fs.writeFileSync(file+'.js', minifyJS(namedTemplateJs), 'utf8'); | |
log((' Created and compiled Handlebars file ' + file + '.js').grey); //.substr(ss.root.length) | |
return '<script src="/assets/main/'+fileName+'.js" type="text/javascript"></script>'; | |
} | |
}; | |
}; | |
minifyJS = function(originalCode) { | |
var ast, jsp, pro; | |
jsp = uglifyjs.parser; | |
pro = uglifyjs.uglify; | |
ast = jsp.parse(originalCode); | |
ast = pro.ast_mangle(ast); | |
ast = pro.ast_squeeze(ast); | |
return pro.gen_code(ast) + ';'; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment