Webpack 4:WOFF、WOFF2、SVG 加载失败

Webpack 4: WOFF, WOFF2, SVGs failed to load

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 文件无法加载,我不明白为什么文件加载器无法加载 WOFF、WOFF2 和 SVG。

这是我的 Webpack 4 加载器配置:

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

请给我一个解决方案。

你可以为此使用 webpack url-loader,它会解析你的 problem.If 你正在使用 npm 你可以安装 npm install url-loader --save-dev 并且在你的 webpack.config.js 你可以像这样写模块设置

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

并导入图像 import img from './image.svg'

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

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

       {
          test: /\.woff(2)?$/,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 10000,
                name: './font/[hash].[ext]',
                mimetype: 'application/font-woff'
              }
            }
          ]
        }

它对我有用。你也可以使用 resolve-url-loader

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