【问题标题】:Why does module parse failed: /xxx/MyFont.ttf Unexpected character ''为什么模块解析失败:/xxx/MyFont.ttf Unexpected character ''
【发布时间】:2018-04-28 10:36:24
【问题描述】:

我遇到了这个错误

./src/style/MyFont.ttf 中的错误模块解析失败:/xxx/MyFont.ttf 意外字符 '' (1:0) 您可能需要适当的加载程序来 处理这种文件类型。 (此二进制文件省略了源代码)` 当我像这样在我的less文件中导入我的自定义字体时:

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

我的 webpack 配置如下:

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" },
]

有人知道怎么解决吗?

【问题讨论】:

  • 好像你没有包含.ttf的webpack插件。 :)

标签: webpack less font-face urlloader


【解决方案1】:

将此添加到您的 rules 数组中。

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

您应该使用npm install 安装file-loader。 希望这会对你有所帮助。

【讨论】:

  • 是的,但您似乎没有使用任何加载器。替换它我的代码。我认为,这将工作:)
  • 这是我正在使用的 url 加载器...尝试了您的解决方案,它没有工作
  • 你的 web pack 版本是多少?
猜你喜欢
  • 2018-07-05
  • 1970-01-01
  • 2020-07-17
  • 2018-05-04
  • 2017-04-15
  • 2016-05-30
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
相关资源
最近更新 更多