Webpack 4: WOFF, WOFF2, SVGs failed to load
Asked Answered
A

2

9

ERROR in ./node_modules/bootstrap/dist/fonts/glyphicons-halflings-regular.woff 1:4 Module parse failed: Unexpected character '' (1:4) You may need an appropriate loader to handle this file type. (Source code omitted for this binary file)

WOFF files are failing to load and I am not getting an idea to why file-loader is failing to load WOFF, WOFF2 and SVG.

Here is my Webpack 4 loaders config:

module: {
        rules: [
            {
                //tell webpack to use jsx-loader for all *.jsx files
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            },
            {
                test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
                exclude: /node_modules/,
                loader: "file-loader"
            },
            {
                test: /\.(eot|ttf)$/,
                loader: "file-loader",
            },
            {
                test: /\.html$/,
                exclude: /node_modules/,
                loader: 'html-loader'
            },
            {
                test: /\.scss$/,
                loaders: ["style-loader", "css-loader", "sass-loader"]
            }
        ]
    }

Please suggest a solution to me.

Amero answered 11/7, 2018 at 4:40 Comment(0)
J
16

You can user webpack url-loader for that and it will resolve your problem.If you are using npm you can install npm install url-loader --save-dev and in your webpack.config.js you can write module settings like this

   {test: /\.(jpg|jpeg|png|woff|woff2|eot|ttf|svg)$/,loader: 'url-loader?limit=100000'}

and import images like import img from './image.svg'

Github : https://github.com/webpack-contrib/url-loader

NPM : https://www.npmjs.com/package/url-loader

Jointed answered 11/7, 2018 at 5:6 Comment(0)
C
8
       {
          test: /\.woff(2)?$/,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 10000,
                name: './font/[hash].[ext]',
                mimetype: 'application/font-woff'
              }
            }
          ]
        }

It worked for me. And also you can use resolve-url-loader

https://www.npmjs.com/package/resolve-url-loader

Carton answered 11/7, 2018 at 5:7 Comment(1)
Thanks for your answer.Amero

© 2022 - 2024 — McMap. All rights reserved.