webpack 用jquery 提示$未定义
webpack 用jquery 提示$未定义 但是window.jquery有值
配置文件如下
var path = require('path');
var webpack = require('webpack');
//用来抽离css的插件
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: './src/main.js',
output: {
libraryTarget: 'umd',
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
//打包去除外部依赖
externals: {
'vue': {
root: 'Vue',
commonjs: 'vue',
commonjs2: 'vue',
amd: 'vue'
},
'lodash': {
commonjs: 'lodash',
commonjs2: 'lodash',
amd: 'lodash',
root: '_'
},
'jquery': 'window.jQuery',
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
css: ExtractTextPlugin.extract({
use: 'css-loader',
fallback: 'vue-style-loader' // <- this is a dep of vue-loader, so no need to explicitly install if using npm3
})
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'url-loader',
options: {
name: "image/[name].[ext]?[hash]",
limit: 8192
}
},
{
test: /\.(less|css)$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{loader: "css-loader"},
{loader: "less-loader"}
],
})
},
]
},
plugins: [
new ExtractTextPlugin("css/styles.css"),
// new webpack.ProvidePlugin({
// jQuery:'jquery',
// 'window.jQuery': 'jquery',
// 'window.$': 'jquery',
// $: 'jquery',
// }),
],
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
}
},
devServer: {
historyApiFallback: true,
noInfo: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
};
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map';
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
页面中通过<script src="js/jquery-3.2.1.js"></script>引用
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
参考:Webpack using bootstrap - jquery is not defined
看看是不是jQ的版本问题,还有可能是你的jq没有引入