2

I am using Webpack 5 and loading fonts and other styles from a .less file. However, the fonts/styles do not seem to be loading, and I'm trying to figure out why.

bootstrap.less

@import (less) '~bootstrap/dist/css/bootstrap.css';

@font-face {
  font-family: 'Gibson';
  src: url('~fonts/gibson/gibson-semibold.otf') format('opentype');
  font-weight: bold;
}

@font-face {
  font-family: 'Gibson';
  src: url('~fonts/gibson/gibson-regular.otf') format('opentype');
  font-weight: normal;
}

Config

{
  module: {
    rules: [
      {
        test: /\.otf$/,
        include: path.resolve(__dirname, './src/fonts'),
        use: {
            loader: 'file-loader',
            options: {
                name: '[name].[ext]',
                outputPath: 'fonts/',
                esModule: false
            },
        },
    },
      {
        test: /\.less$/,
        use: [
          { loader: 'style-loader' },
          {
            loader: 'css-loader',
            options: {
              url: false,
            },
          },
          { loader: 'less-loader' },
        ],
      },
      {
        test: /\.css$/,
        use: [
          { loader: 'style-loader' },
          {
            loader: 'css-loader',
            options: {
              modules: true,
              url: false,
            },
          },
        ],
      },
    ],
  },
};

I'm using React on Rails with Webpacker, and bootstrap.css is located under node_modules/bootstrap/dist/css. the two .otf font files are located under public/packs/assets/fonts/gibson and also under app/assets/fonts/gibson.

What am I missing? I can see that webpack is recognizing the font files at least:

...
odules by path ./app/assets/ 579 KiB
    modules by path ./app/assets/stylesheets/application/*.less 76.7 KiB 62 modules
    modules by path ./app/assets/stylesheets/*.less 502 KiB 4 modules
    modules by path ./app/assets/images/ 362 bytes 4 modules
    modules by path ./app/assets/fonts/gibson/*.otf 173 bytes 2 modules
webpack 5.68.0 compiled successfully in 7608 ms

However, I do not see the fonts loaded on the site itself. In my main application.jsx I have imported like so: import 'stylesheets/bootstrap.less';

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
jeanmw
  • 446
  • 2
  • 17
  • I've tried adding and removing the `url: false` option for the less test css loader. removing the `url:false` option shows the fonts as loaded, but still doesn't actually display them on the site. Adding the `url:false` option no longer shows the fonts compiling. – jeanmw Feb 09 '22 at 19:44
  • I have also tried using `url-loader` – jeanmw Feb 09 '22 at 22:02

1 Answers1

0

I was able to fix this issue by adding:

*{font-family:Gibson;}

to my bootstrap.less file. I'm really uncertain this is the best way to do this, but it does work.

jeanmw
  • 446
  • 2
  • 17