Webpack and fonts: module parse failed

Iakov picture Iakov · Dec 2, 2015 · Viewed 7.8k times · Source

I'm trying to use FontAwesome in my app. I'm using webpack to do it's magic. My config is:

  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' },
    ])
  }

I'm requesting FontAwesome CSS like that:

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

font-awesome.min.css contains this:

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

And for some reason, WebPack tries to parse .woff file with style-loader and gives me error:

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

I'm really lost right now. Any ideas?

Update: I'm completely lost right now. I've decided to fool around with my config and put this line in loaders:

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

And required this file:

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

Got error:

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)

However, when I tried to require my file like this:

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

Everything went smooth. Looks like webpack ignores my loaders?

Answer

tellxp picture tellxp · Apr 8, 2016
  1. it depends on the url used in css.

  2. this error is releated to regex, try to change (\?[a-z0-9]+) to (\?v=[0-9]\.[0-9]\.[0-9]) or (\?[\s\S]+).

Example:

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'
      },
    ]
  }
};