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

gia*_*lbi 4 javascript template-engine npm handlebars.js

这是我的文件夹结构:

app
????templates
    ????templ1.hbs
    ????templ2.hbs
    ????templ3.hbs
Run Code Online (Sandbox Code Playgroud)

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

$ handlebars *.hbs -f templ.js
Run Code Online (Sandbox Code Playgroud)

编译因此错误而失败

(...)\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)
Run Code Online (Sandbox Code Playgroud)

怎么了?

gia*_*lbi 9

在使用Handlebars及其预编译器进行一些练习后,我发现解决方案就像我的问题一样简单.

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

$ handlebars ./templates -f templ.js --extension "hbs"
Run Code Online (Sandbox Code Playgroud)

一切都应该工作正常.

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