【发布时间】:2018-07-12 18:25:50
【问题描述】:
我在 webpack 上使用 jquery 时遇到问题。 我的代码:
const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const CompressionPlugin = require("compression-webpack-plugin");
module.exports = {
entry: {
vendor: [
'./src/main/webapp/js/vendor/jquery-3.3.1.min.js',
// './src/main/webapp/js/vendor/fs.js',
'./src/main/webapp/js/vendor/google-adsense.js',
'./src/main/webapp/js/vendor/jquery.menu-aim.min.js',
'./src/main/webapp/js/vendor/jquery.touchSwipe.min.js',
],
app: './src/main/assets/js/desktop/app.js',
mobile: './src/main/assets/js/mobile/app.js',
touch: './src/main/assets/js/touch/app.js',
},
module: {
rules: [{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: require.resolve('jquery'),
loader: 'expose-loader?jQuery!expose-loader?$'
}
],
},
plugins: [
// new CleanWebpackPlugin(['src/main/webapp/assets']),
new webpack.optimize.CommonsChunkPlugin({
name: 'common' // Specify the common bundle's name.
}),
new UglifyJsPlugin({
test: /\.js$/,
sourceMap: process.env.NODE_ENV === "development"
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
],
output: {
filename: '[name].js',
path: path.resolve(__dirname, './src/main/webapp/js')
}
};
上面的代码编译时,控制台会抛出这个错误
vendor.js:1 Uncaught ReferenceError: webpackJsonp is not defined 在 vendor.js:1
当我尝试使用它时
externals: {
jquery: 'jQuery'
}
它抛出
vendor.js:1 Uncaught ReferenceError: jQuery is not defined
at Object.231 (vendor.js:1)
at o (common.js:1)
at Object.228 (vendor.js:1)
at o (common.js:1)
at window.webpackJsonp (common.js:1)
at vendor.js:1
我在我的核心 js 文件 import $ from 'jquery' 中使用 jquery。
我做错了什么 ?有什么帮助吗?谢谢你。
【问题讨论】:
-
@JordiCastilla 上面的链接不能解决我的问题。我还在这个:(。有什么建议吗?
-
你检查所有帖子了吗?恕我直言,您似乎缺少
npm i jquery --save或链接问题中定义的其他一些小部分 -
是的,我试过
npm install jquery --save和npm update等 -
你是如何使用 jQuery的?
标签: javascript webpack ecmascript-6