lundi 9 mai 2016

Ember.js compiling on the fly and require minify with 2.4.0

I have an Ember.js application where, rather than precompiling templates before deployment, we compile them in place on app startup. (The app isn't very big so the speed loss is negligible.) We aren't using Ember-CLI or ES6, just require.js with the text extension. We uglify for release but debug without any build.

With Ember 1.13.11, the following works great, even when the code is minified. During minification, the ember-template-compiler is absorbed into the single app.js, and it is the minified-in version that is used in the following, which is our only reference to the template compiler anywhere in our code:

(function() { // hide templates list from global namespace
  var templates =    [
        "ember-template-compiler",
        "require/text!templates/index.htm",
        "require/text!templates/components/mxr-graph.htm",
        ...more of them...
  ];
  // Content of "templates" is duplicated here because uglify build 
  // has a minimal parser and can only see arrays of constants in define.
  // Otherwise, we'd just go define (templates, function(){...}), 
  // which would be very elegant - but no such luck.
  define([
    "ember-template-compiler",
    "require/text!templates/index.htm",
    "require/text!templates/components/mxr-graph.htm",
    "require/text!templates/components/mxr-grid.htm",
    ...more of them...
  ], function () {
    for (var f = 1, fLen = arguments.length; f < fLen; f++) {
        var name = templates[f].replace('require/text!templates/','').replace('.htm','');
        Ember.TEMPLATES[name] = Ember.Handlebars.compile(arguments[f]);
        }
  });
})();

When I do the same thing with Ember 2.4.5, instead of using the minified-in version, it tries to load ember-template-compiler.js from a URL and fails, even though I can see the minified compiler code in the app.js.

I've made no change, except swapping the ember-prod.js and ember-template-compiler.js files for the new version and reminifying. Since this module is also rolled into app.js, I know app.js is already loaded when this is called. Is there something special I need to do with Ember 2.x to make it pick up the module that require.js should already know about?




Aucun commentaire:

Enregistrer un commentaire