Webpack “OTS parsing error” loading fonts

前端 未结 13 1072
[愿得一人]
[愿得一人] 2020-11-28 20:51

My webpack config specifies that fonts should be loaded using url-loader, and when I try to view the page using Chrome I get the following error:



        
相关标签:
13条回答
  • 2020-11-28 21:20

    The best and easiest method is to base64 encode the font file. And use it in font-face. For encoding, go to the folder having the font-file and use the command in terminal:

    base64 Roboto.ttf > basecodedtext.txt
    

    You will get an output file named basecodedtext.txt. Open that file. Remove any white spaces in that.

    Copy that code and add the following line to the CSS file:

    @font-face {
      font-family: "font-name";
      src: url(data:application/x-font-woff;charset=utf-8;base64,<<paste your code here>>) format('woff');
    }  
    

    Then you can use the font-family: "font-name" in your CSS.

    0 讨论(0)
  • 2020-11-28 21:20

    In my case adding following lines to lambda.js {my deployed is on AWS Lambda} fixed the issue.

     'font/opentype',
     'font/sfnt',
     'font/ttf',
     'font/woff',
     'font/woff2'
    
    0 讨论(0)
  • 2020-11-28 21:23

    I just had the same issue with Font Awesome. Turned out this was caused by a problem with FTP. The file was uploaded as text (ASCII) instead of binary, which corrupted the file. I simply changed my FTP software to binary, re-uploaded the font files, and then it all worked.

    https://css-tricks.com/forums/topic/custom-fonts-returns-failed-to-decode-downloaded-font/ this helped me in the end I had the same issue with FTP transferring files as text

    0 讨论(0)
  • 2020-11-28 21:25

    As asnwered here by @mcortesi if you remove the sourceMaps from the css loader query the css will be built without use of blob and the data urls will be parsed fine

    0 讨论(0)
  • 2020-11-28 21:25

    Since you use url-loader:

    The url-loader works like the file-loader, but can return a DataURL if the file is smaller than a byte limit.

    So another solution to this problem would be making the limit higher enough that the font files are included as DataURL, for example to 100000 which are more or less 100Kb:

    {
      module: {
        loaders: [
          // ...
          {
            test: /\.scss$/,
            loaders: ['style', 'css?sourceMap', 'autoprefixer', 'sass?sourceMap'],
          },
          {
            test: /images\/.*\.(png|jpg|svg|gif)$/,
            loader: 'url-loader?limit=10000&name="[name]-[hash].[ext]"',
          },
          {
            test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
            use: 'url-loader?limit=100000&mimetype=application/font-woff',
          },
          {
            test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
            use: 'url-loader?limit=100000&mimetype=application/font-woff',
          },
          {
            test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
            use: 'url-loader?limit=100000&mimetype=application/octet-stream',
          },
          {
            test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
            use: 'file-loader',
          },
          {
            test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
            use: 'url-loader?limit=100000&mimetype=image/svg+xml',
          },
        ],
      },
    }
    

    Allways taking into account on what the limit number represents:

    Byte limit to inline files as Data URL

    This way you don't need to specify the whole URL of the assets. Which can be difficult when you want Webpack to not only respond from localhost.

    Just one last consideration, this configuration is NOT RECOMMENDED for production. This is just for development easiness.

    0 讨论(0)
  • 2020-11-28 21:28

    For me the problem was my regex expression. The below did the trick to get bootstrap working:

    {
        test: /\.(woff|ttf|eot|svg)(\?v=[a-z0-9]\.[a-z0-9]\.[a-z0-9])?$/,
        loader: 'url-loader?limit=100000'
    },
    
    0 讨论(0)
提交回复
热议问题