如何正确导入font-awesome scss

问题描述 投票:3回答:1

我试图通过scss使用来自font-awesome的图标和webpack 4。 webconfig文件如下所示:

const HtmlWebPackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const autoprefixer = require("autoprefixer");

module.exports = {
  entry: ["./src/index.js"],
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.[hash].js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"]
          }
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader",
            options: {
              minimize: true
            }
          }
        ]
      },
      {
        test: /\.(sass|scss)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "style.css"
            }
          },
          { loader: "extract-loader" },
          {
            loader: "css-loader"
          },
          {
            loader: "postcss-loader",
            options: {
              plugins: () => [autoprefixer({ grid: false })]
            }
          },
          {
            loader: "sass-loader",
            options: {
              includePaths: ["./node_modules"]
            }
          }
        ]
      },
      {
        test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name][hash].[ext]",
              outputPath: "fonts/"
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./public/index.html",
      filename: "./index.html"
    }),
    new CopyWebpackPlugin([
      {
        from: "public"
      }
    ])
  ]
};

我导入scss如下:

@import "@fortawesome/fontawesome-free/scss/fontawesome.scss";  

并使用:

    <a class="mdc-list-item mdc-list-item--selected demo-drawer-list-item" href="#">
        <i class="fas fa-inbox mdc-list-item__graphic"></i>Inbox
    </a>  

表明:

enter image description here

一个矩形而不是一个图标。我究竟做错了什么?完整的例子是在github

html sass webpack-4
1个回答
2
投票

我遇到了同样的问题,将font-awesome scss导入到我的项目中,这对我有用。

@import "~@fortawesome/fontawesome-free/scss/fontawesome"; 
© www.soinside.com 2019 - 2024. All rights reserved.