2016-09-26 70 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升並導入。