Marcelo Alarcon
Marcelo Alarcon

Reputation: 401

Unable to load background image from Css file Webpack5

I'm trying to put an image as background in a div but it doesn't recognize it because webpack changes the path to http://localhost:8080/gaia.png instead of http://localhost:8080/assets/images/gaia.png The image is displayed correctly inside an tag but it doesn't load if I want to use it from the Css file. I'm sure the problem is the file path but I don't know how to fix it. enter image description here

enter image description here

index.js

import './assets/style/style.css'
import './assets/images/gaia.png'
import './assets/fonts/CascadiaCode.ttf'

const fun = () => {
  console.log('hey')
}
fun();

webpack.config.js

const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require('path');

module.exports = {
  mode: 'development',
  devtool: 'inline-source-map',
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'src', 'index.html')
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ["babel-loader"]
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
        generator: {
          filename: '[name][ext][query]',
          outputPath: 'assets/images/',
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
        generator: {
          filename: '[name][ext][query]',
          outputPath: 'assets/fonts/',
        },
      },
    ],
  },
};

style.css

.container {
  width: 300px;
  height: 300px;
  background-image: url("../images/gaia.png");
  background-size: auto;
}

Upvotes: 0

Views: 286

Answers (2)

Marcelo Alarcon
Marcelo Alarcon

Reputation: 401

I fixed the problem by installing a specific loader for Html images

npm install --save-dev html-loader image-webpack-loader

I followed this tutorial: Optimizing Static HTML And Images With Webpack

Here is my webpackconfig.js file:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
  mode: 'development',
  devtool: 'inline-source-map',
  devServer: {
    static: './dist',
  },
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'src', 'index.html'),
    }),
  ],
  module: {
    rules: [
      {
        test: /\.css/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.html$/i,
        use: 'html-loader',
      },
      {
        test: /\.(png|jpg)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'images/[name]-[hash][ext]',
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'fonts/[name]-[hash][ext]',
        },
      },
    ],
  },
};

I also created a Webpack Template that you can use with the default configuration on my GitHub: Webpack Template Repo

Upvotes: 0

sancelot
sancelot

Reputation: 2063

delete outputpath and set it up as :

generator: {
         filename: 'assets/images/[hash][ext][query]'
       }
     }

Upvotes: 1

Related Questions