0

我在让引导图标与 webpack 一起工作时遇到了很多麻烦:

获得以下内容:

ERROR in ./node_modules/bootstrap-icons/font/bootstrap-icons.css 1:0
Module parse failed: Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> @font-face {
|   font-family: "bootstrap-icons";
|   src: url("./fonts/bootstrap-icons.woff2?856008caa5eb66df68595e734e59580d") format("woff2"),
 @ ./src/index.js 3:0-50

使用 webpack 规则:

      {
        test: /\.((png)|(eot)|(woff)|(woff2)|(ttf)|(svg)|(gif))(\?((v=\d+\.\d+\.\d+)|(\w*)))?$/,
        use: { loader: "file-loader?name=/[hash].[ext]" }
      },
      ...

      {
        test: /\.(sa|sc|c)ss$/,
        exclude: /node_modules/,
        use: [
          "style-loader",
          MiniCssExtractPlugin.loader,
          "css-loader",
          "postcss-loader",
          "sass-loader"
        ]
      }

和 .js 作为

import "./scss/main.scss";
import "bootstrap-icons/font/bootstrap-icons.css";

我已经尝试了我能找到的一切。我遵循了这个教程的每一行,但仍然无法让它工作: https ://odan.github.io/2021/01/07/webpack-bootstrap-icons.html

webpack: "5.52.1",
"bootstrap-icons": "^1.5.0",
"file-loader": "^6.2.0",
4

2 回答 2

0

我有同样的问题,我通过阅读文档解决了它。我后来发现的问题是 webpack 不会用 purgecss 清除 bootstrap 未使用的图标。

  {
    test: /\.(woff|woff2|eot|ttf|otf)$/i,
    type: "asset/inline",
  },
于 2021-09-18T16:37:30.423 回答
0

将 Bootstrap Icons 1.72 与 Webpack 5.65 一起使用

  1. 安装以下库(如果尚未安装):
npm install bootstrap-icons
npm install --save-dev mini-css-extract-plugin css-loader postcss-loader sass-loader autoprefixer sass

  1. 添加以下 Webpack 规则:
// Put this at the top of index.js or other entry files
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

// Rule for processing the Bootstrap icons
{
    test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/i,
    type: 'asset/resource',
    generator: {
        //filename: 'fonts/[name]-[hash][ext][query]'
        filename: 'fonts/[name][ext][query]'
    }
},

...

// Rule for processing .css and .scss files
{
    test: /\.s?css$/,
    use: [
        // Save the CSS as a separate file to allow caching                            
        MiniCssExtractPlugin.loader,
        {
            // Translate CSS into CommonJS modules
            loader: 'css-loader',
        },
        {
            // Run postcss actions
            loader: 'postcss-loader',
            options: {
                postcssOptions: {
                    plugins: [
                        function () {
                            return [ require('autoprefixer') ];
                        }
                    ],
                },
            },
        },
        {
            loader: 'sass-loader',
            options: {
                sassOptions: {
                    outputStyle: "compressed",
                }
            }
        }
    ],
},
  1. 将以下内容添加到main.scss. 请注意引导图标后的 .css 扩展名。如果省略扩展名,将导致错误。
@import 'bootstrap-icons/font/bootstrap-icons.css';
  1. 通过将其添加到 HTML 文件来测试图标。
<i class="bi-alarm" style="font-size: 3rem;"></i>
于 2021-12-23T16:41:14.367 回答