Webpack无法加载库的样式。

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

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 &quot;slick-carousel/slick/slick.css&quot;; 
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:

import React from &quot;react&quot;;
import { createRoot } from &quot;react-dom/client&quot;
import Slider from &quot;react-slick&quot;;
import &quot;slick-carousel/slick/slick.css&quot;; 
import &quot;slick-carousel/slick/slick-theme.css&quot;;

const root = createRoot(document.getElementById(&quot;app&quot;));

const settings = {
    dots: true,
    infinite: true,
    speed: 500,
    slidesToShow: 1,
    slidesToScroll: 1
  };

const SimpleSlider = () =&gt; {
    return (
        &lt;div&gt;
          &lt;h2&gt; Single Item&lt;/h2&gt;
          &lt;Slider {...settings}&gt;
            &lt;div&gt;
              &lt;h3&gt;1&lt;/h3&gt;
            &lt;/div&gt;
            &lt;div&gt;
              &lt;h3&gt;2&lt;/h3&gt;
            &lt;/div&gt;
          &lt;/Slider&gt;
        &lt;/div&gt;
      );
}

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:

确定