Webpack 4:WOFF,WOFF2,SVG无法加载

时间:2018-07-11 04:40:47

标签: reactjs webpack webpack-dev-server woff woff2

  

./ node_modules / bootstrap / dist / fonts / glyphicons-halflings-regular.woff中的错误1:4   模块解析失败:意外字符''(1:4)   您可能需要适当的加载程序来处理此文件类型。   (此二进制文件省略了源代码)

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

请给我一个解决方案。

2 个答案:

答案 0 :(得分:3)

您可以为此使用webpack url-loader,它将解决您的问题。如果您使用的是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

答案 1 :(得分:3)

       {
          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