Webpack无法加载库的样式。

huangapple go评论131阅读模式
英文:

Webpack doesn't load libraries styles

问题

我正在使用webpack和React项目,但是我安装的外部npm包(例如react-slick)的样式没有加载。很明显样式没有加载到DOM中。

这是我的webpack.config.js文件:

  1. const HtmlWebpackPlugin = require("html-webpack-plugin");
  2. const webpack = require("webpack");
  3. module.exports = {
  4. mode: "development",
  5. resolve: {
  6. extensions: [".js", ".jsx"],
  7. },
  8. module: {
  9. rules: [
  10. {
  11. test: /\.css$/i,
  12. use: ["style-loader", "css-loader"],
  13. },
  14. {
  15. test: /\.jsx?$/,
  16. loader: "babel-loader",
  17. },
  18. ],
  19. },
  20. plugins: [
  21. new HtmlWebpackPlugin({
  22. template: "./src/index.html",
  23. }),
  24. new webpack.DefinePlugin({
  25. "process.env": {
  26. NODE_ENV: JSON.stringify("development"),
  27. REACT_APP_API_URL: JSON.stringify(process.env.REACT_APP_API_URL),
  28. },
  29. }),
  30. ],
  31. devServer: {
  32. historyApiFallback: true,
  33. },
  34. externals: {
  35. // global app config object
  36. config: JSON.stringify({
  37. apiUrl: "http://localhost:3000",
  38. }),
  39. },
  40. };

我尝试在配置中添加了不同的加载器,如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

  1. const HtmlWebpackPlugin = require("html-webpack-plugin");
  2. const webpack = require("webpack");
  3. module.exports = {
  4. mode: "development",
  5. resolve: {
  6. extensions: [".js", ".jsx"],
  7. },
  8. module: {
  9. rules: [
  10. {
  11. test: /\.css$/i,
  12. use: ["style-loader", "css-loader"],
  13. },
  14. {
  15. test: /\.jsx?$/,
  16. loader: "babel-loader",
  17. },
  18. ],
  19. },
  20. plugins: [
  21. new HtmlWebpackPlugin({
  22. template: "./src/index.html",
  23. }),
  24. new webpack.DefinePlugin({
  25. "process.env": {
  26. NODE_ENV: JSON.stringify("development"),
  27. REACT_APP_API_URL: JSON.stringify(process.env.REACT_APP_API_URL),
  28. },
  29. }),
  30. ],
  31. devServer: {
  32. historyApiFallback: true,
  33. },
  34. externals: {
  35. // global app config object
  36. config: JSON.stringify({
  37. apiUrl: "http://localhost:3000",
  38. }),
  39. },
  40. };

I tried to add different loaders in the config like: css-loader, style-loader and babel-loader`

答案1

得分: 0

react-slick库本身不带有内置的样式。根据文档所说,你应该使用slick-carousel包来安装它的样式(或者自己编写样式)。

我只需要在样式导入之前删除波浪符号(~):

  1. import "slick-carousel/slick/slick.css";
  2. import "slick-carousel/slick/slick-theme.css";

我没有更改提供的webpack配置,样式按预期加载。

我使用的完整示例代码如下:

  1. import React from "react";
  2. import { createRoot } from "react-dom/client"
  3. import Slider from "react-slick";
  4. import "slick-carousel/slick/slick.css";
  5. import "slick-carousel/slick/slick-theme.css";
  6. const root = createRoot(document.getElementById("app"));
  7. const settings = {
  8. dots: true,
  9. infinite: true,
  10. speed: 500,
  11. slidesToShow: 1,
  12. slidesToScroll: 1
  13. };
  14. const SimpleSlider = () => {
  15. return (
  16. <div>
  17. <h2> Single Item</h2>
  18. <Slider {...settings}>
  19. <div>
  20. <h3>1</h3>
  21. </div>
  22. <div>
  23. <h3>2</h3>
  24. </div>
  25. </Slider>
  26. </div>
  27. );
  28. }
  29. 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:

  1. import &quot;slick-carousel/slick/slick.css&quot;;
  2. import &quot;slick-carousel/slick/slick-theme.css&quot;;

I hadn't changed anything in the provided webpack configuration, and the styles were loaded as expected.

Full example that I used:

  1. import React from &quot;react&quot;;
  2. import { createRoot } from &quot;react-dom/client&quot;
  3. import Slider from &quot;react-slick&quot;;
  4. import &quot;slick-carousel/slick/slick.css&quot;;
  5. import &quot;slick-carousel/slick/slick-theme.css&quot;;
  6. const root = createRoot(document.getElementById(&quot;app&quot;));
  7. const settings = {
  8. dots: true,
  9. infinite: true,
  10. speed: 500,
  11. slidesToShow: 1,
  12. slidesToScroll: 1
  13. };
  14. const SimpleSlider = () =&gt; {
  15. return (
  16. &lt;div&gt;
  17. &lt;h2&gt; Single Item&lt;/h2&gt;
  18. &lt;Slider {...settings}&gt;
  19. &lt;div&gt;
  20. &lt;h3&gt;1&lt;/h3&gt;
  21. &lt;/div&gt;
  22. &lt;div&gt;
  23. &lt;h3&gt;2&lt;/h3&gt;
  24. &lt;/div&gt;
  25. &lt;/Slider&gt;
  26. &lt;/div&gt;
  27. );
  28. }
  29. root.render(&lt;SimpleSlider /&gt;);

huangapple
  • 本文由 发表于 2023年8月9日 00:20:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定