很长一段时间想使用猫头鹰传送带,但无法通过npm
webpack
以任何方式连接。
site of npm官员写了信
通过“ webpack.ProvidePlugin” 将jQuery添加到您的 webpack 配置:
const webpack = require('webpack');
//...
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
],
//...
我的 webpack 配置文件中已经有此配置
加载所需的样式表和JS:
import 'owl.carousel/dist/assets/owl.carousel.css';
import 'owl.carousel';
尝试麻木 1
在这样的配置中(仅将css文件更改为scss ),我遇到以下错误
无法读取未定义的属性'fn'
尝试号码 2
也不能像这样包含。
import 'imports?jQuery=jquery!owl.carousel';
我遇到了错误Module not found: Error: Can't resolve 'imports' in 'D:\master\my path '
尝试号码 3
import owlCarousel from "owl.carousel";
类似于第一次尝试的错误
我的 webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const webpack = require('webpack');
let conf = {
entry: {
index: "./src/index.js"
},
output: {
path: path.resolve(__dirname, "./dist"),
filename: "[name]bundle.js",
publicPath: "dist/"
},
devServer: {
overlay:true
},
module: {
rules: [
{
test: /\.js/,
loader:"babel-loader",
},
{
test:/\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: 'css-loader',
options: {
url: false,
minimize: true,
sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
})
}
]
},
plugins: [
new ExtractTextPlugin({
filename: "[name].css"
}),
new HtmlWebpackPlugin({
filename: "index.html",
template: "build/index.html",
hash: true,
chunks: ["index"]
}),
new webpack.ProvidePlugin({
'$': "jquery",
'jQuery': "jquery",
'Popper': 'popper.js',
"Bootstrap": "bootstrap.js"
})
]
};
module.exports = (env, options) => {
let production = options.mode === "production";
conf.devtool = production ? false : "eval-sourcemap";
return conf;
}
在我的项目中,我有2个捆绑包
index.js
import $ from "jquery";
import jQuery from "jquery";
import "../styles/main/main.scss";
import "normalize.scss/normalize.scss";
import "bootstrap/dist/js/bootstrap.bundle.min.js";
//in here including owl-config where I config my owl carousel
import * as owlConfig from "./owl-config";
//after this more js code's
第二个 owl-config.js 我在该文件中所做的所有尝试
我的依赖项
"dependencies": {
"bootstrap": "^4.1.1",
"normalize.scss": "^0.1.0",
"owl.carousel": "^2.2.0",
"jquery": "^3.3.1",
"popper": "^1.0.1"
}
答案 0 :(得分:0)
问题是我没有正确执行文档中写的内容
//...
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
],
//...
我的 webpack.config.js
中包含什么new webpack.ProvidePlugin({
'$': "jquery",
'jQuery': "jquery",
'Popper': 'popper.js',
"Bootstrap": "bootstrap.js"
})
我如何更改和工作
new webpack.ProvidePlugin({
'$': "jquery",
'jQuery': "jquery",
'window.jQuery': 'jquery',
'Popper': 'popper.js',
"Bootstrap": "bootstrap.js"
})