Webpack “OTS parsing error” loading fonts

前端 未结 13 1180
[愿得一人]
[愿得一人] 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: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.

提交回复
热议问题