Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap icons and Webpack 5 - You may need an appropriate loader to handle this file type

I'm having a lot of trouble getting bootstrap icons to work with webpack:

Getting the following:

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

With webpack rules:

      {
        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"
        ]
      }

And .js as

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

I've tried everything I could find. I followed every line of this turoial, and still can'f get it to work: 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",
like image 336
Isidoro Avatar asked Oct 12 '25 03:10

Isidoro


2 Answers

Here is the cleanest way I found to make Boostrap Icons work with Webpack 5 (and scss):

Step 1: install Bootstrap icons with NPM:

npm install --save bootstrap-icons

Step 2: add a rule in your webpack.config.js to copy all woff2 files in the output directory:

module.exports = {
    module: {
        rules: [{
            test: /\.woff2?$/,
            type: "asset/resource",
        }]
    }
}

Step 3: in your index.scss import boostrao icon scss overrifing font directory:

$bootstrap-icons-font-dir: "~bootstrap-icons/font/fonts";
@import "~bootstrap-icons/font/bootstrap-icons.scss";

Step 4: use icons in yout HTML:

<i class="bi-alarm"></i>
like image 197
Gian Marco Avatar answered Oct 16 '25 06:10

Gian Marco


I had the same problem, i kind of solved it by reading the documentation. The problem that i figured afterwards was that webpack doesnt purge bootstrap's unusued icons with purgecss.

  {
    test: /\.(woff|woff2|eot|ttf|otf)$/i,
    type: "asset/inline",
  },
like image 29
Bes Avatar answered Oct 16 '25 05:10

Bes