[英]Handlebars.js and Webpack - precompile templates
I'm using Backbone.js
, Handlebars.js
and Webpack
for my website. 我在我的网站上使用
Backbone.js
, Handlebars.js
和Webpack
。 I used to use Require.js
instead of Webpack
. 我曾经使用
Require.js
而不是Webpack
。 I have the following files: 我有以下文件:
template.js template.js
define({
structure: "structure.html",
home: "home.html"
});
webpack.config.js 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 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;
});
I'd like to compile all templates with Handlebars.js
at start of my website. 我想在我的网站开头用
Handlebars.js
编译所有模板。 When I was using Require.js
I was used to doing something like this: 当我使用
Require.js
我习惯做这样的事情:
utils.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;
});
How can I do something like that in utils.js with webpack
? 我怎样才能做到与utils.js类似的东西
webpack
?
Thanks 谢谢
你可以使用这个把手加载器的web包https://github.com/pcardune/handlebars-loader/blob/master/README.md那里给出了完整的信息,但基本上设置加载器来编译模板中的所有html文件文件夹或从.html重命名为.hbs或.handlebars然后在您的模块中简单地要求它们,它们将被编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.