在一个 js 文件中预编译所有 handlebars 模板文件时出错

Error when precompiling all handlebars templates files in one js file

这是我的文件夹结构:

app
└───templates
    ├───templ1.hbs
    ├───templ2.hbs
    └───templ3.hbs

我想在一个 templ.js 文件中编译(预编译)所有 templN.hbs handlebars 模板文件,但是当我尝试通过控制台

$ handlebars *.hbs -f templ.js

编译失败并出现此错误

(...)\AppData\Roaming\npm\node_modules\handlebars\bin\handlebars:120
    throw err;
    ^
 Error: Unable to open template file "*.hbs"
    at (...)\AppData\Roaming\npm\node_modules\handlebars\dist\cjs\precompiler.js:107:25
    at FSReqWrap.oncomplete (fs.js:82:15)

怎么了?

在使用 Handlebars 及其预编译器进行一些练习后,我发现解决方案非常简单,我的问题很愚蠢。

首先最好留在根文件夹中,因此在我的情况下留在 app 文件夹中。然后,要编译所有 .hbs 模板,只需将整个 ./handlebars 文件夹作为输入传递给 handlebars 预编译器,并指定他应该查找的模板扩展名:

$ handlebars ./templates -f templ.js --extension "hbs"

一切正常。

有关预编译器的更多选项,请参阅 http://handlebarsjs.com/precompilation.html