2016-09-26 72 views
0

我预编译模板使用以下命令:把手预编译模板无法导入

handlebars -m -e html ./templates/ > ./templates/templates.js 

然后我尝试导入生成的文件在我的javascript:

import './templates/templates.js'; 

会抛出一个错误:

Uncaught ReferenceError: Handlebars is not defined 

此错误与完整或运行时间一起出现,仅包含Handlebars。我使用Handlebars 4.0.5并将一个项目与Webpack捆绑在一起。
你能帮忙吗?

回答

0

您的预编译模板包含对全局对象Handlebars的引用。如果你打开文件,你会看到:

(function() { var template = Handlebars.template,

有一个-c标志使用CommonJS的编译,所以用

handlebars templates.hbs -f templates-compiled.js -c handlebars/handlebars.runtime

你会在编译的文件得到这样的:

var Handlebars = require("handlebars/handlebars.runtime");

,但我不知道的WebPack将如何DEA升并导入。