Permalink
Browse files

initial import

  • Loading branch information...
0 parents commit 3e06608bddbe5a3bf2001b56f8dbfaba4765c971 @gabrielgrant committed Sep 15, 2012
Showing with 1,721 additions and 0 deletions.
  1. +15 −0 README.md
  2. +36 −0 bin/ember-precompile
  3. +65 −0 index.js
  4. +37 −0 package.json
  5. +18 −0 vendor/ember-1.0.pre.min.js
  6. +1,550 −0 vendor/handlebars-1.0.0.beta.6.js
@@ -0,0 +1,15 @@
+Precompile Handlebars templates for Ember.js
+
+Install
+-------
+
+ npm install -g ember-precompile
+
+Usage
+-----
+
+This module has a similar interface to the Handlebars precompiler
+
+ ember-precompile template... [-f OUTPUT_FILE]
+
+If output file is omitted, the compiled template will be printed to stdout.
@@ -0,0 +1,36 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+var argv = require('optimist').argv;
+
+var compileEmberTemplate = require('ember-precompile');
+
+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(compileEmberTemplate(fileName));
+ });
+}
+
+main();
@@ -0,0 +1,65 @@
+var fs = require('fs');
+var path = require('path');
+var vm = require('vm');
+
+var HANDLEBARSJS = fs.readFileSync(__dirname + '/vendor/handlebars-1.0.0.beta.6.js', 'utf8')
+var EMBERJS = fs.readFileSync(__dirname + '/vendor/ember-1.0.pre.min.js', 'utf8')
+
+module.exports = function (file) {
+ //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)
+
+ // extract the compiled template from the vm context and return it,
+ // adding template to Ember.TEMPLATES when it is required
+ var fileName = path.basename(file)
+ var namedTemplateJs = 'Ember.TEMPLATES["' +
+ fileName.replace(/.handlebars/, '') +
+ '"] = Ember.Handlebars.template(' + context.templatejs + ');'
+
+ return namedTemplateJs;
+}
+
@@ -0,0 +1,37 @@
+{
+ "name" : "ember-precompile",
+ "version" : "0.1.3",
+ "description" : "Precompile Ember.js templates with a Handlebars-like UI",
+ "main" : "./index.js",
+
+ "keywords" : [
+ "Ember.js",
+ "Handlebars.js",
+ "template"
+ ],
+
+ "bin": {
+ "ember-precompile": "./bin/ember-precompile"
+ },
+
+ "dependencies": {
+ "optimist": "0.3.4"
+ },
+
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/gabrielgrant/node-ember-precompile"
+ },
+
+ "author" : "Gabriel Grant <g@briel.ca>",
+
+ "contributors" : [{
+ "name": "Jeff Dean",
+ "email": "jeff@zilkey.com"
+ }, {
+ "name": "Peter Wagenet",
+ "email": "peter.wagenet@gmail.com"
+ }, {
+ "name": "Garth Williams"
+ }]
+}

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 3e06608

Please sign in to comment.