【发布时间】:2017-07-03 15:44:35
【问题描述】:
有没有办法编译模板目录并使用Webpack存储在数组对象中?
说明:我现在正在使用车把模板列表。我在gulp 中使用handlebars compiler 预编译模板列表。
gulp.src('client/src/templates/**/*.hbs')
.pipe(gulp_handlebars({
handlebars: handlebars,
compilerOptions:{
knownHelpers: helpers,
knownHelpersOnly:true}
}))
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'appname.templates',
noRedeclare: true,
processName: function(filePath) {
return declare.processNameByPath(filePath.replace('client/src/templates/', ''));
}
}));
然后我将通过appname.templates 数组访问模板。它工作正常。
现在,我正在转向 Webpack。如果我使用 handlebars-loader , 它允许我按名称要求每个模板,例如
var template = require("./file.handlebars");
有没有办法将一个目录中的所有模板作为数组获取,例如
var templates = require("./*.handlebars");
【问题讨论】:
-
也许npmjs.com/package/glob-loader是你想要的?
-
@bejado - 指向 glob 加载器的 Tx。是的,它加载文件的目录,但我还需要应用把手加载器。有没有办法同时调用。另外我不确定如何在代码中导入。如果您有任何建议,请告诉我。
标签: javascript webpack handlebars.js