'...使用 webpack 时未定义 jQuery,但定义了 jQuery



当试图在浏览器中加载绑定的脚本文件时,我得到:

未捕获引用错误:jQuery未定义

这应该来自我捆绑的underscore-min.js中的最后一行,即问题:

}()

这没有任何意义?我不确定源地图文件中是否有什么东西让我误入歧途。我在webpack.config.js:中的配置是这样的

entry: {
main: [ 
'./js/jquery.min.js', 
'./js/other.js', 
'./js/other2.js', 
'./js/underscore-min.js'
]
},     
...
plugins: {
...
new webpack.ProvidePlugin({
jquery: 'jquery',
jQuery: 'jquery',
}),
...
}
...
externals: {
'jquery': 'jQuery',
'jQuery': 'jQuery',
}

如果没有以上内容,大量依赖jQuery的其他软件包就会崩溃。underscore-min.js脚本是否存在导致此问题的独特之处?

只需删除externals: { 'jquery': 'jQuery', 'jQuery': 'jQuery', }

如果您想使用externals

entry: {
main: [ 
'./js/jquery.min.js', 
'./js/other.js', 
'./js/other2.js', 
'./js/underscore-min.js'
]
},     
...
...
externals: {
'jquery': {
root:'jquery',
commonjs2:'jquery',
commonjs:'jquery',
amd:'jquery'
}
},
output:{
...
libraryTarget:"umd"
}

在您的html中,添加script标记<script src="some-resources/jquery.js"></script>

最新更新