【发布时间】:2019-02-20 10:24:56
【问题描述】:
如何将 jquery 和 jquery.lazy 与 webpack 捆绑在一起? - 每次我尝试这个,我都会得到一个错误。
我的浏览器调试控制台中的错误如下:
TypeError: q is undefined
其余的工作正常。也许你可以看出我做错了什么。
我的 package.json:
{
"name": "mrd-apple",
"version": "1i.0.0",
"description": "mrd Apple",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"jquery": "^3.3.1",
"jquery-lazy": "^1.7.10",
"postcss-loader": "^3.0.0",
"webpack": "^4.29.3"
},
"devDependencies": {
"css-loader": "^2.1.0",
"style-loader": "^0.23.1",
"webpack-cli": "^3.2.3"
}
}
}
我的 webpack.config.js
var webpack = require('webpack');
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
var OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = function(env) {
return {
entry: ['./src/js/index.js'],
output: {
path: __dirname + '/dist',
filename: 'bundle.js'
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "[name].css",
chunkFilename: "[id].css"
}),
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.optimize\.css$/g,
cssProcessor: require('cssnano'),
cssProcessorPluginOptions: {
preset: ['default', { discardComments: { removeAll: true } }],
},
canPrint: true
})
],
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
publicPath: '../'
}
},
"css-loader"
]
}
]
},
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true // set to true if you want JS source maps
}),
new OptimizeCSSAssetsPlugin({})
]
},
}
}
index.js 的内容
// JS
// require('./jquery.lazy');
require('./lightslider');
require('./simple-lightbox');
require('./init');
// CSS
require('./../css/index.css');
【问题讨论】:
-
你在哪里得到这个错误?我的意思是哪个文件抛出了那个错误?
-
在控制台中,当我打开我的网址时。
-
不需要 'jQuery' 依赖,因为 jquery-lazy 依赖于 jquery 并与它一起安装 - github.com/eisbehr-/jquery.lazy/blob/master/package.json。在 github 问题中提出了与您类似的问题 - github.com/eisbehr-/jquery.lazy/issues/195。
-
当我只安装 jquery-lazy 时,也是同样的问题。 - 并且没有定义 jQuery。
-
@yfain,你的 index.js 文件里有什么?
标签: javascript jquery webpack