Emberjs Handlebars precompiling

前端 未结 6 2348
庸人自扰
庸人自扰 2020-12-23 14:44

My Emberjs app is running slowly so I wanted to precompile my template to ease the runtime a bit. However I\'m lost on how to proceed. I read http://handlebarsjs.com/precomp

6条回答
  •  渐次进展
    2020-12-23 15:01

    You can precompile in the client's browser, as Thomas Bartelmess stated.

    You can also precompile using handlebars via nodejs (taken from my very own Jakefile):

    var Handlebars = require('handlebars');
    precompile = (function () {
        //Lovingly extracted from Ember's sources.
        var objectCreate = Object.create || function (parent) {
                function F() {}
                F.prototype = parent;
                return new F();
            },
            Compiler = function () {},
            JavaScriptCompiler = function () {};
    
        Compiler.prototype = objectCreate(Handlebars.Compiler.prototype);
        Compiler.prototype.compiler = Compiler;
        JavaScriptCompiler.prototype = objectCreate(Handlebars.JavaScriptCompiler.prototype);
        JavaScriptCompiler.prototype.compiler = JavaScriptCompiler;
        JavaScriptCompiler.prototype.namespace = "Ember.Handlebars";
        JavaScriptCompiler.prototype.initializeBuffer = function () {
            return "''";
        };
        JavaScriptCompiler.prototype.appendToBuffer = function (string) {
            return "data.buffer.push(" + string + ");";
        };
        Compiler.prototype.mustache = function (mustache) {
            if (mustache.params.length || mustache.hash) {
                return Handlebars.Compiler.prototype.mustache.call(this, mustache);
            } else {
                var id = new Handlebars.AST.IdNode(['_triageMustache']);
                if (!mustache.escaped) {
                    mustache.hash = mustache.hash || new Handlebars.AST.HashNode([]);
                    mustache.hash.pairs.push(["unescaped", new Handlebars.AST.StringNode("true")]);
                }
                mustache = new Handlebars.AST.MustacheNode([id].concat([mustache.id]), mustache.hash, !mustache.escaped);
                return Handlebars.Compiler.prototype.mustache.call(this, mustache);
            }
        };
        return function precompile(string) {
            var ast = Handlebars.parse(string);
            var options = {
                knownHelpers : {
                    action : true,
                    unbound : true,
                    bindAttr : true,
                    template : true,
                    view : true,
                    _triageMustache : true
                },
                data : true,
                stringParams : true
            };
    
            var environment = new Compiler().compile(ast, options);
            return new JavaScriptCompiler().compile(environment, options, undefined, true);
        };
    }());
    
    strPrecompiledTemplate = item.handlebarsTemplateFolders.map(function (dir) {
        console.info("\tProcessing " + dir);
        return readdirRecursiveSync(dir).map(function (file) {
            console.info("\t\t" + file);
            var content = fs.readFileSync(file, 'utf-8');
            content = Handlebars.precompile(content);
            file = file.replace(/\.[^\.]+$/, '').replace(/^src\//g, '').substr(dir.length).replace(/^\/+/, '');
            // Pay attention: The wrap in Ember.Handlebars.template() is important!
            return "Ember.TEMPLATES['"+file+"'] = Ember.Handlebars.template("+content+");";
        }).join("\r\n");
    }).join("\r\n");
    

提交回复
热议问题