我刚刚开始使用Webpack并将一个较旧的应用程序从一组grunt任务迁移到使用webpack。到目前为止,它看起来非常精细,但我无法弄清楚如何让chart.js在这种情况下工作。
Chart.js正试图像这样注册一个全局变量Chart
:
(function(){
"use strict";
//Declare root variable - window in the browser, global on the server
var root = this,
previous = root.Chart;
//Occupy the global variable of Chart, and create a simple base class
var Chart = function(context){
// some stuff
}
root.Chart = Chart;
这确实会破坏,因为在webpack中运行时,this
是undefined
,而不是窗口或全局。
它引发的错误在
行root.Chart = Chart;
并说:
Uncaught TypeError: Cannot read property 'Chart' of undefined
有没有机会,我可以让Chart.js运行而无需修补它的源代码?实际上它在整个地方使用this
技巧,我希望以后能够更新chart.js。
答案 0 :(得分:0)
尝试使用imports-loader来调整依存关系,如下所示:
使用npm install imports-loader
并将其添加到您的Webpack配置中的加载程序中:
{
test: require.resolve('chart.js'),
use: 'imports-loader?this=>window'
}