英文:
Webpack doesn't load libraries styles
问题
我正在使用webpack和React项目,但是我安装的外部npm包(例如react-slick)的样式没有加载。很明显样式没有加载到DOM中。
这是我的webpack.config.js文件:
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
module.exports = {
mode: "development",
resolve: {
extensions: [".js", ".jsx"],
},
module: {
rules: [
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
},
{
test: /\.jsx?$/,
loader: "babel-loader",
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html",
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("development"),
REACT_APP_API_URL: JSON.stringify(process.env.REACT_APP_API_URL),
},
}),
],
devServer: {
historyApiFallback: true,
},
externals: {
// global app config object
config: JSON.stringify({
apiUrl: "http://localhost:3000",
}),
},
};
我尝试在配置中添加了不同的加载器,如css-loader、style-loader和babel-loader。
英文:
`I am using webpack with a React project, and the external npm packages that I install don't get their styles loaded (e.g react-slick). It looks clearly that the styles are not loading and going into the DOM.
This is my webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
module.exports = {
mode: "development",
resolve: {
extensions: [".js", ".jsx"],
},
module: {
rules: [
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
},
{
test: /\.jsx?$/,
loader: "babel-loader",
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html",
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("development"),
REACT_APP_API_URL: JSON.stringify(process.env.REACT_APP_API_URL),
},
}),
],
devServer: {
historyApiFallback: true,
},
externals: {
// global app config object
config: JSON.stringify({
apiUrl: "http://localhost:3000",
}),
},
};
I tried to add different loaders in the config like: css-loader, style-loader and babel-loader`
答案1
得分: 0
react-slick库本身不带有内置的样式。根据文档所说,你应该使用slick-carousel包来安装它的样式(或者自己编写样式)。
我只需要在样式导入之前删除波浪符号(~):
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
我没有更改提供的webpack配置,样式按预期加载。
我使用的完整示例代码如下:
import React from "react";
import { createRoot } from "react-dom/client"
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
const root = createRoot(document.getElementById("app"));
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1
};
const SimpleSlider = () => {
return (
<div>
<h2> Single Item</h2>
<Slider {...settings}>
<div>
<h3>1</h3>
</div>
<div>
<h3>2</h3>
</div>
</Slider>
</div>
);
}
root.render(<SimpleSlider />);
英文:
The react-slick library doesn't come with styles built-in. As the documentation says, you should install its styles with the slick-carousel package (or write your own).
I just had to remove the tilde symbol(~) before the style imports:
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
I hadn't changed anything in the provided webpack configuration, and the styles were loaded as expected.
Full example that I used:
import React from "react";
import { createRoot } from "react-dom/client"
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
const root = createRoot(document.getElementById("app"));
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1
};
const SimpleSlider = () => {
return (
<div>
<h2> Single Item</h2>
<Slider {...settings}>
<div>
<h3>1</h3>
</div>
<div>
<h3>2</h3>
</div>
</Slider>
</div>
);
}
root.render(<SimpleSlider />);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论