如何将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');
答案 0 :(得分:-1)
您的问题在于您如何尝试使用require.js
首先,您的package.json存在问题:
webpack
和postcss-loader
都是devDependencies。 uglifyjs-webpack-plugin
,mini-css-extract-plugin
,optimize-css-assets-webpack-plugin
但是,问题是您如何尝试包含这些库。
当将require.js与package.json和webpack一起使用时,您将不会尝试要求.js文件本身,而是会require('npm-package-name')
,或者最好是import NpmPackageName from 'npm-package-name'
;参见:Dependency Management
如果您想通过HTML在页面上加载库,而只是在源代码中引用它,则可以使用webpack外部组件来实现。参见:Webpack Externals
供参考,这是我的文件版本:
package.json:
{
"name": "mrd-apple",
"version": "1.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"
},
"devDependencies": {
"css-loader": "^2.1.0",
"mini-css-extract-plugin": "^0.5.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"postcss-loader": "^3.0.0",
"style-loader": "^0.23.1",
"uglifyjs-webpack-plugin": "^2.1.2",
"webpack": "^4.29.3",
"webpack-cli": "^3.2.3"
}
}
index.js:
// require('jquery');
// require('jquery-lazy');
import $ from 'jquery';
import jqueryLazy from 'jquery-lazy';
注意:我无法产生您提供的确切错误,但可以认为是由于项目设置本身造成的。