2016-06-09 56 views
0

我正在爲我的網站使用Backbone.js,Handlebars.jsWebpack。 我曾經使用Require.js而不是Webpack。我有以下文件:Handlebars.js和Webpack - 預編譯模板

template.js

define({ 
    structure:  "structure.html", 
    home:   "home.html" 

}); 

webpack.config.js

resolve: { 
     extensions: ['', '.js', '.json'], 

     alias: { 
      root: path.resolve(__dirname), 
      "router": path.resolve(__dirname) + "/www/js/router", 
      "templates": path.resolve(__dirname) + "/www/js/templates", 
      "handlebars": path.resolve(__dirname) + "/node_modules/handlebars/dist/handlebars", 
     "templates_html": path.resolve(__dirname) + "/www/templates" 
     }  
    }, 

view.js

define(['jquery', 'underscore', 'backbone', "utils" ], 
     function($, _, Backbone, Utils) { 

    var View = Utils.Page.extend({ 

     constructorName: "View", 
     id: "home", 

     initialize: function() { 
      this.template = Utils.templates.home; // this is what I need 

     }, 

     render: function() { 
      this.$el.html( this.template(this.model.toJSON()) ); 
      return this; 
     }, 

    }); 

    return View; 

}); 

我想在我的網站開始時用Handlebars.js編譯所有模板。 當我使用Require.js我是用來做這樣的事情:

utils.js

define(['jquery', 'underscore', 'backbone', 'handlebars', 'templates'], 
     function($, _, Backbone, Handlebars, Templates) { 

    var Utils = { 
     templates: {} 
    }; 

    for (var t in Templates) { 
     Templates[t] = "text!" + Templates[t]; 
    } 

    require(_.values(Templates), function() { 

     var fragments = _.object(_.keys(Templates), arguments); 

     // precompile all the fragments 
     for (var t in fragments) { 
      Utils.templates[t] = Handlebars.compile(fragments[t]); /* <Handlebars> */ 
     } 

    }); 
    return Utils; 
}); 

我怎麼可以做utils.jswebpack類似的東西?

感謝

回答