Webpack can't load fonts (ttf)

前端 未结 4 1682
心在旅途
心在旅途 2021-02-20 05:56

Currently I have 3 fonts that I want to add to my React project:a, a light, a bold.
My file structure:

/src
├── /fonts/
│   ├── /A.ttf
│   ├── /A-         


        
4条回答
  •  心在旅途
    2021-02-20 06:33

    Webpack requires a font loader to load font files present in your project. you are using a file loader to load fonts. Change

    {
          test: /\.(ttf|eot|woff|woff2)$/,
          loader: 'file-loader',
          options: {
          name: 'fonts/[name].[ext]'
     }
    

    to

     {
          test: /\.ttf$/,
          use: [
            {
              loader: 'ttf-loader',
              options: {
                name: './font/[hash].[ext]',
              },
            },
          ]
      }
    

    by installing a font loader in your project like TTF Loader from NPM.

提交回复
热议问题