sleepy_daze
sleepy_daze

Reputation: 551

Cannot load image webpack (4.41.0)

I am trying to load a jpg image file using Webpack, but I keep getting the following error: Uncaught Error: Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): ModuleParseError: Module parse failed: Unexpected character '�' (1:0)

I'm using file-loader, so I'm not sure what's wrong...

This is my webpack.config.js file:

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

My image is currently stored in a src/images directory. What could I be missing?

Thanks for your help!

Edit: Here is the CSS I'm using to pull in the image:

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

Edit: Here is my entire webpack.config.js file:

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,
  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: [
      {
        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()]
  }
};```

Upvotes: 2

Views: 2072

Answers (2)

Ayman Morsy
Ayman Morsy

Reputation: 1429

  • First try to replace your sass regex test like this :
    test: /\.(sass|scss)$/

  • if it didn't work try replacing your whole rule like this :

            {
                test: /\.(sass|scss)$/,
                use: [
                    MiniCssExtractPlugin.loader, 
                    {
                        loader: 'css-loader',
                    }, 
                    {
                        loader: 'sass-loader',
                    } 
                ]
            }
  • also remove {filename: '[name].css'} from MiniCssExtractPlugin

with the two tries I mentioned keep it simple like that :

 plugins:[
...
    new MiniCssExtractPlugin(),
...

Upvotes: 0

nima moradi
nima moradi

Reputation: 2628

test: /\.(JPG|gif|svg|gif|png)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
            include: SRC,
            use: [{
                loader: 'file-loader'
            }]
             .
             .
             .

Upvotes: 1

Related Questions