无法加载图像Webpack(4.41.0)

我正在尝试使用Webpack加载jpg图像文件,但始终出现以下错误:Uncaught Error: Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): ModuleParseError: Module parse failed: Unexpected character '�' (1:0)

我正在使用文件加载器,所以不知道怎么了...

这是我的webpack.config.js文件:

 module.exports = {
  context: __dirname,entry: {
    frontend: ['./src/index.js','./src/sass/style.scss'],customizer: './src/customizer.js'
  },output: {
    path: path.resolve(__dirname,'public'),filename: '[name]-bundle.js'
  },mode: 'development',devtool: 'cheap-eval-source-map',module: {
    rules: [
    {
      test: /\.(jpe?g|JPG|png|gif)\$/,use: [
        {
          loader: 'file-loader',options: {
            publicPath: 'public/images',name: '[name].[ext]'
         }
       }
     ]
   }

我的图像当前存储在src/images目录中。我可能会缺少什么?

感谢您的帮助!

编辑:这是我用来提取图像的CSS:

#header {
  background: url('../images/header.JPG') no-repeat center center fixed;
  background-size: cover;
}

编辑:这是我的整个webpack.config.js文件:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCssAssetsplugin = require('optimize-css-assets-webpack-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');

module.exports = {
  context: __dirname,module: {
    rules: [
      {
        enforce: 'pre',exclude: /node_modules/,test: /\.jsx$/,loader: 'eslint-loader'
      },{
        test: /\.jsx?$/,loader: 'babel-loader'
      },{
        test: /\.s?css$/,use: [MiniCssExtractPlugin.loader,'css-loader','sass-loader']
      },{
        test: /\.svg$/,loader: 'svg-sprite-loader',options: {
          extract: true,spriteFilename: 'svg-defs.svg'
        }
      },{
        test: /\.(jpe?g|JPG|png|gif)\$/,use: [
          {
            loader: 'file-loader',options: {
              publicPath: 'public/images',name: '[name].[ext]'
            }
          }
        ]
      }
    ]
  },plugins: [
    new StyleLintPlugin(),new MiniCssExtractPlugin({
      filename: '[name].css'
    }),new BrowserSyncPlugin({
      files: '**/*.php',proxy: 'http://mysite.test/'
    })
  ],optimization: {
    minimizer: [new UglifyJsPlugin(),new OptimizeCssAssetsplugin()]
  }
};```
li797979 回答:无法加载图像Webpack(4.41.0)

test: /\.(JPG|gif|svg|gif|png)(\?v=[0-9]\.[0-9]\.[0-9])?$/,include: SRC,use: [{
                loader: 'file-loader'
            }]
             .
             .
             .
,
  • 首先尝试替换这样的sass regex测试:
    test: /\.(sass|scss)$/

  • 如果不起作用,请尝试像这样替换整个规则:

            {
                test: /\.(sass|scss)$/,use: [
                    MiniCssExtractPlugin.loader,{
                        loader: 'css-loader',},{
                        loader: 'sass-loader',} 
                ]
            }
  • 还从MiniCssExtractPlugin中删除{filename: '[name].css'}

在我提到的两次尝试中,保持这种简单:

 plugins:[
...
    new MiniCssExtractPlugin(),...
本文链接:https://www.f2er.com/2712606.html

大家都在问