Webpack和字体:模块解析失败

时间:2015-12-02 12:16:19

标签: css webpack webfonts

我正在尝试在我的应用中使用FontAwesome。我正在使用webpack来做它的魔力。我的配置是:

  resolve: {
    // you can now require('myfile') instead of require('myfile.cjsx')
    extensions: ['', '.js', '.jsx', '.cjsx', '.coffee']
  },
  module: {
    loaders: commonLoaders.concat([
      { test: /\.css$/, loader : 'style-loader!css-loader' },
      { test: /\.(ttf|eot|svg|woff(2))(\?[a-z0-9]+)?$/, loader : 'file-loader' },
      { test: /\.cjsx$/, loaders: ['react-hot', 'coffee', 'cjsx']},
      { test: /\.coffee$/, loader: 'coffee' },
      { test: /\.jsx$|\.js$/, loader: 'jsx-loader?harmony' },
    ])
  }

我正在请求FontAwesome CSS:

require "../../styles/font-awesome.min.css";

font-awesome.min.css包含:

@font-face {
  font-family: 'FontAwesome';
  src: url('../fonts/fontawesome-webfont.woff') format('woff');
  font-weight: normal;
  font-style: normal;
}

由于某种原因,WebPack尝试使用样式加载器解析.woff文件并给出错误:

ERROR in ./src/fonts/fontawesome-webfont.woff
Module parse failed: /var/www/app/src/fonts/fontawesome-webfont.woff Line 1: Unexpected token ILLEGAL
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
 @ ./~/css-loader!./src/styles/font-awesome.min.css 2:73-117

我现在真的迷路了。有什么想法吗?

更新 我现在完全迷失了。我决定愚弄我的配置并把这行放在装载机中:

{ test: /\.eot$/, loader : 'file' },

并且需要这个文件:

require "../../fonts/fontawesome-webfont.eot";

收到错误:

ERROR in ./src/fonts/fontawesome-webfont.eot
Module parse failed: /var/www/app/src/fonts/fontawesome-webfont.eot Line 2: Unexpected token ILLEGAL
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)

然而,当我试图要求我的文件时这样:

require "file!../../fonts/fontawesome-webfont.eot";

一切顺利。看起来像webpack忽略了我的加载器?

2 个答案:

答案 0 :(得分:6)

  1. 取决于css中使用的网址。

  2. 此错误与正则表达式相关联,尝试将(\?[a-z0-9]+)更改为(\?v=[0-9]\.[0-9]\.[0-9])(\?[\s\S]+)

  3. 示例:

    https://github.com/gowravshekar/font-awesome-webpack

    module.exports = {
           module: {
             loaders: [
             // the url-loader uses DataUrls.
             // the file-loader emits files.
               { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
               { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }
            ]
          }
        };
    

    https://github.com/shakacode/font-awesome-loader

    module.exports = {
      module: {
        loaders: [
          // the url-loader uses DataUrls.
          // the file-loader emits files.
          {
            test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
            // Limiting the size of the woff fonts breaks font-awesome ONLY for the extract text plugin
            // loader: "url?limit=10000"
            loader: "url"
          },
          {
            test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
            loader: 'file'
          },
        ]
      }
    };
    

答案 1 :(得分:2)

前几天我通过LESS来源添加font-awesome基本上

npm install --save less-loader
bower install --save components-font-awesome

然后我要求像这样的字体很棒

require('bower_components/components-font-awesome/less/font-awesome.less')

最后在webpack.config.js我添加了加载器模块

var path = require('path')

module.exports = {

    ...

    , module: {
        loaders: [
            {test: /\.less$/, loader: "style!css!less"},
            {test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff'},
            {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
            {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
            {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'}
        ]
    } 
};

我知道它与.css不一样,但我相信这很简单。希望它有所帮助。