Easy way to precompile Emberjs Handlebar templates with nodejs?

人走茶凉 提交于 2019-12-02 18:31:20

I've written a grunt plugin called grunt-ember-handlebars that does exactly this. It pretty much mimics Garth's script, with one important difference:

It uses lib/headless-ember.js and lib/ember.js, which are maintained (at least for now) by ember.js to precompile default templates. If you don't want to use grunt, you can extract the relevant code from the precompile_handlebars helper in tasks/ember-handlebars.js.

Found a good enough solution to my problem that seems easy enough to maintain that I'll consider my problem solved.

Here's how I solved things:

  • Grab the minimal amount of code I need to precompile the ember templates.
  • Outline of the simple precompile procedure:
    • Load up the code with a var Ember = require('./my_ember_precompiler').Ember.
    • Get your templates as strings and compile them with var templateString = Ember.Handlebars.precompile(str).toString().
    • This will be different from app to app, but Ember seems to require registration of precompiled templates. After loading, for every created template, we need to register our templates. Basically wrap templateString in a call to Handlebars.template() and make sure this wrapped function is added to the Ember.TEMPLATES object.

The above is painless when it's automated in a script.

I've published a version of ember-precompiler with a similar interface to the handlebars command line utility. You can install it from NPM:

npm install -g ember-precompile

and then run it as:

ember-precompile template... [-f OUTPUT_FILE]

It does essentially what you describe (and what the gist versions do too): mock out the missing components needed to run Ember and Handlebars, compile the templates and add them to Ember.TEMPLATES.

Take a look at the npm package Ember-Runner

The following gist contains a nodejs build script that will precompile all .handlebars files in a given folder:

https://gist.github.com/1622236

I wrote an official precompiler npm module for anyone else who might be wanting to do this w/ a recent version of ember.js

https://npmjs.org/package/ember-template-compiler

It's simple to install and use (example below)

npm install ember-template-compiler

var compiler = require('ember-template-compiler');
var template = fs.readFileSync('foo.handlebars').toString();
var input = compiler.precompile(template).toString();
var output = "Ember.TEMPLATES['foo'] = Ember.Handlebars.template(" + input + ");";

Look at the code for the official ember-rails gem at https://github.com/emberjs/ember-rails

While it's not a node.js project, it does show you how to precompile the templates using the Rails 3.1+ asset pipeline, and it includes all the necessary Javascript code that you would need to do it in Node without having to hack together a solution that you'd have to maintain on your own.

More specifically, look at vendor/assets/javascripts/ember-precompiler.js and lib/ember-rails/hjs_template.rb

I'm far from an expert on Node (obviously, Rails is more my thing).. but I think those two files should point you in the right direction. Basically, you're going to want to concatenate ember-precompiler.js (which acts as a "shim" for lack of a better word) with ember.js and then call EmberRails.precompile to compile your templates.

window object can be mocked by jsdom

    var jsdom = require("jsdom").jsdom;
    global.document = jsdom("<html><head></head><body></body></html>");
    global.window = document.createWindow();
    global.$ = global.jQuery = window.$ = window.jQuery = require("jquery");
    global.Handlebars = window.Handlebars = require('handlebars');
    global.Application = window.Application = {};
    require('ember.js');

and now you can run anything from Ember including Ember.Handlebars.compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!