使用webpack时,加载.otf字体文件的适当方法是什么?我已经根据以下示例看到了很多示例,但我多次尝试在webpack.config.js中包含一个规则,但均未成功:

{ test: /\.(eot|svg|ttf|otf|woff)$/, use: 'file-loader' }
//or
{ test: /\.(eot|svg|ttf|otf|woff)$/, use: 'url-loader' }
//or
{ test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/, use: 'file-loader' }
//...etc

我已经在webpack.config.js中为其他文件类型设置了以下文件,它们可以正常运行:
module.exports = {
    //...
      module: {
        rules: [
          { test: /\.(js)$/, use: 'babel-loader' },
          { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]},
          { test: /\.(jpe?g|png|gif|svg|xml)$/i, use: 'file-loader'}
        ]
      },
    //...
}

尽管我作了种种尝试为.otf文件添加另一个规则/案例的尝试,但我始终会收到以下错误:



我在根目录的fonts文件夹中添加了.otf文件,在index.css中,我具有:
@font-face {
  font-family: 'name-of-my-font';
  src: url('fonts/name-of-my-font.otf'),
  format('opentype');
}

有没有人遇到过类似的问题并找到了解决方案?

谢谢,
奥斯汀

每个我的webpack.config.js文件的更多评论请求,这是我的整个webpack.config.js:
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './app/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index_bundle.js',
    publicPath: '/'
  },
  module: {
    rules: [
      { test: /\.(js)$/, use: 'babel-loader' },
      { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]},
      { test: /\.(jpe?g|png|gif|svg|xml)$/i, use: 'file-loader' }
    ]
  },
  devServer: {
    historyApiFallback: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'app/index.html'
    })
  ]
};

最佳答案

我加了

  {
    test: /\.(woff|woff2|eot|ttf|otf)$/,
    loader: "file-loader"
  }

到我的webpack.config.js。
我想我需要将类型明确链接到文件加载器。我正在使用默认的VS2017 Angular项目。

关于javascript - 在Webpack中加载.otf字体文件的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45044573/

10-09 17:54
查看更多