为handlebas编译器指定一个文件

specify one file for handlebars compiler

本文关键字:一个 文件 handlebas 编译器      更新时间:2023-09-26

我正在尝试在webstorm中设置一个监视文件。

我想将一个文件预编译到我的主templates.js聚合文件中。

这可能吗?

如果我使用/path/to/hello.tpl -f templates.js选项,它只会覆盖templates.js文件中的所有其他模板。

如果我使用/path/to/templates > templates.js,这意味着当我只修改一个模板时,我必须预编译filewatcher中的每个模板。

编辑:

如果传递一个目录,并将其输出重定向到一个文件,则可以在一个文件中获得所有预编译的模板。handlebars -e tpl ./templates/ > ./js/templates.js

这将预编译所有./templates/*.tpl文件,并将它们保存到./js/templates.js,然后可以将其包含在页面中。

以下是有关该技术的更多信息:http://berzniz.com/post/24743062344/handling-handlebars-js-like-a-pro

有一个nodejs插件,它可以满足您的需求。请试试这个,看看它是否适合你。

https://github.com/cif/handlebar-rider

我的解决方案是稍微修改手把模块本身,以便在node_modules''handbars''bin''handbars中找到processTemplate函数并更改

if (extension.test(path) || fs.statSync(path).isDirectory()) {
    processTemplate(path, root || template);
  }

if (extension.test(path) || /'.hbr$/.test(path)|| fs.statSync(path).isDirectory()) {
    processTemplate(path, root || template);
  }

然后它获取所有的模板文件(我给了它一个hbr扩展名),并将它们放在-f标志指定的文件中。