Why does module parse failed: /xxx/MyFont.ttf Unexpected character ''

北城以北 提交于 2019-12-11 02:27:38

问题


I encountered this error

ERROR in ./src/style/MyFont.ttf Module parse failed: /xxx/MyFont.ttf Unexpected character '' (1:0) You may need an appropriate loader to handle this file type. (Source code omitted for this binary file)` when I import my custom font in my less file like this:

@font-face {
    font-family: "MyFont";
    src: url("./MyFont.ttf") format("truetype"); 
}

my webpack config is as follow:

rules: [
  {
    test: /\.jsx?$/,
    exclude: /node_modules/,
    loader: 'babel-loader',
    query: babelQuery
  },{
    test: /\.css$/,
    use: [
      'style-loader',
      'css-loader'
    ]
  },
  {
    test: /\.less$/i,
    use: ExtractTextPlugin.extract([ 'css-loader', 'less-loader' ])
  },
  { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" },
]

Anyone knows how to solve it?


回答1:


Add this to your rules array.

{
  test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
  loader: 'file-loader?name=assets/[name].[hash].[ext]'
}

and you should install the file-loader using npm install. Hope this will help you.



来源:https://stackoverflow.com/questions/47303407/why-does-module-parse-failed-xxx-myfont-ttf-unexpected-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!