Easy way to precompile Emberjs Handlebar templates with nodejs?

前端 未结 8 1715
耶瑟儿~
耶瑟儿~ 2021-02-01 09:59

I\'m enjoying emberjs a lot and would like to take the next step in a couple of my small, mobile apps and precompile my Ember/Handlebars templates as part of my build process.

8条回答
  •  情歌与酒
    2021-02-01 10:04

    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.
      • Copy the contents of the ember handlebars into a file. This file is located at the emberjs repo: https://github.com/emberjs/ember.js/blob/master/packages/handlebars/lib/main.js
      • Within the code, find the one instance of window.Handlebars = Handlebars; and remove it.
      • Append the contents of the Ember template compiler and handlebar overrides. The file is located at the emberjs repo: https://github.com/emberjs/ember.js/blob/master/packages/ember-handlebars/lib/ext.js
      • Find all instances of Ember.create and change to Object.create.
    • 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.

提交回复
热议问题