Troubleshooting: Issue with Webpack 4 failing to load images in HTML

My webpack configuration seems to be loading images from my SCSS files, but not from the HTML files. Additionally, when I run the BUILD command to deliver my production archives, it doesn't create the "img" folder as expected. As a newcomer to webpack 4, I suspect that there are steps missing in my WP config file.

This is my webpack.dev.js

The desired structure for my prod folder would look like this:

dist

|----img

|----css

|----js

a.html

b.html

c.html

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");

module.exports = {
  entry: {
    main: "./src/js/scripts.js"
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "js/[name].[hash].js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.scss$/,
        use: [
          "style-loader",
          MiniCssExtractPlugin.loader,
          "css-loader",
          "postcss-loader",
          "sass-loader"
        ]
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: ["url-loader"]
      }
    ]
  },

  devServer: {
    port: 8080
  },

  plugins: [
    new CleanWebpackPlugin("dist", {}),
    new MiniCssExtractPlugin({
      filename: "css/style.[contenthash].css"
    }),
    new HtmlWebpackPlugin({
      template: "./src/index.html",
      inject: false,
      hash: true,
      filename: "index.html"
    }),
    new HtmlWebpackPlugin({
      template: "./src/actualitat.html",
      inject: false,
      hash: true,
      filename: "actualitat.html"
    }),
    new HtmlWebpackPlugin({
      template: "./src/projectes.html",
      inject: false,
      hash: true,
      filename: "projectes.html"
    })
  ]
};

This is how I'm loading CSS and JS on my HTML files:

    <link
  rel="stylesheet"
  href="<%=htmlWebpackPlugin.files.chunks.main.css %>"
/>

<script src="<%= htmlWebpackPlugin.files.chunks.main.entry %>"></script>

I have a "scripts.js" js file where I import the files like this:

import "../scss/style.scss";
import "../img/searchBar-icon.png";
import "../img/townHall.png";
import "../img/icon-title.png";

UPDATE:

I've updated the way I load images by using the "file-loader" loader:

  {
    test: /\.(png|jpg|gif)$/,
    use: {
      loader: "file-loader",
      options: {
        outputPath: "img/",
        name: "[name][hash].[ext]"
      }
    }
  }

Now all my images are being copied to the "img" folder, which is great. The only issue is that the filenames now have hashes (as intended) and they're not appearing in my HTML files.

Answer №1

Have you attempted the method below to load the images?

<pre>{
  test: /\.(jpe?g|png|gif|ico)$/i,
  use: ["file-loader?name=[name].[hash].[ext]"]
}
</pre>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Preventing ReactJS tooltips from exceeding the boundaries of the screen

Here is a simple demo showcasing blocks with tooltips that appear when hovered over. However, there seems to be an issue with the functionality. The tooltip should ideally be displayed either from the left or right side of the block. To determine the size ...

Create a dynamic effect by adding space between two texts on the page

const Button = () => { const options = ['test1', 'test2', 'test3']; return ( <div style={{ position: 'absolute', left: '8px', width: 'auto', flexDirection: 'row' ...

Interactive Jquery Slideshow for Mobile Devices

Help needed to arrange images in a slideshow format! The images are currently scattered on the webpage and causing confusion. Thank you in advance for any assistance provided. Below are the HTML, JavaScript, and CSS codes that need to be fixed: HTML < ...

Styling your printed documents in Next.js 10

Within my Next 10 application, I am able to bring in global styles by importing them in _app.js in the following manner: import 'assets/css/app.css' I want to incorporate a print stylesheet as well. Instead of using a @media print { } query with ...

Expanding dropdown with JQuery / Bootstrap datepicker upon clicking

As a newcomer to front-end development, I'm seeking guidance on integrating Bootstrap dropdown menus with a datepicker. The goal is for users to click on the menu and have the datepicker pop up so they can select a date. My ultimate objective is to ca ...

Ensure that a span within a cell table has a height of 100%

Is there a way to have the span element expand to full height? Click here <table class="table table-condensed table-hover table-striped"> <thead> <tr> <th class="shrink"></th> <th class ...

Bootstrap 5 introduces an innovative animated hamburger icon that transforms into an X symbol when clicked

I have encountered an unusual behavior when trying to animate the hamburger button using Bootstrap 5. Previously, with Bootstrap 4, everything worked smoothly. However, in Bootstrap 5, I noticed that the icon initially displays as 'X' and then sw ...

Moving a button to the right side in HTML/CSS without creating any gaps

Currently, I'm working on developing a simple idle browser game. I'm facing a challenge in positioning a button on the right-middle of the box. I attempted using position: relative along with adjusting the bottom and left parameters, but I still ...

The TTF font is not showing up on the webpage when applying the @font-family property

I have been attempting to incorporate a custom font into a website that I am developing using @Font-Face, but unfortunately it is not functioning as expected. Even though I followed the instructions provided in the link above, the font does not seem to be ...

What benefits do unordered lists <ul>s offer in terms of SEO, accessibility, and beyond for website navigation?

When considering SEO and accessibility, is using <ul> tags a beneficial method for creating straightforward navigation menus? For example, I typically test all my websites with Lynx to ensure they are accessible, and it appears that <ul> tags ...

Adding a loading spinner feature to a form submission in ReactJS

Can someone help me implement a loading spinner for my form submission in React? I'm new to this and struggling with it. What I want is, when I click submit, first show the spinner and then load the table. But currently, the spinner shows even before ...

Tips for utilizing .animate() to increase width from a particular direction

Currently working on animating an opening sequence for a project and trying to figure out a way to utilize .animate() in order to have my div "come in" from the right rather than the left, which appears to be the default direction. I'm confident that ...

Selenium Mouse/Pointer Display

Is there a way to visualize the selenium mouse movements during test execution? Can we display a windows cursor image or any kind of indicator like a dot or crosshair? I am currently working on implementing a drag and drop feature using Selenium and Java ...

Revamping the hyperlinks in my collapsible menu extension

Is there a way to modify the links in this accordion drop menu so that they lead to external HTML pages instead of specific areas on the same page? I've tried various methods but can't seem to achieve it without affecting the styles. Currently, i ...

Updating the icon image for navigation in AngularJS

I am looking to update an image upon navigating to /tab1. This can be achieved using 'ng-click' with AngularJS routing. Sample HTML snippet: <div class = "body" ng-controller = "app"> <div class = "column1"> <div cla ...

Sending a PHP object as the value of an HTML radio button

When dealing with a custom object that needs to be passed as a form value in a radio button selection, the usual method involves invoking the toString() function on the object. However, this simply converts the object to a String, which may not be ideal fo ...

Center align the table

<div class="text-center" style="align-items: center; justify-content: center;"> <table style="border: 1rem;width: 100%;margin-left:auto;margin-right:auto;"> <tr><th>Id</th><th>Qu ...

The document type is not compatible with the element "BR" in this location; it is assumed that the "LI" start-tag is missing

How can I create a validated list with HTML 4.01 standards? I tried the following code, but it's giving me an error: "document type does not allow element 'BR' here; assuming missing 'LI' start-tag" <tr> <td colspan="2" ...

Every attempt to convert ColorBitmap to GrayscaleBitmap ends in a crash

I've been working on a code to convert two colored pictures to grayscale and then compare them pixel by pixel. However, my app keeps crashing. Here is the code snippet I'm using: public boolean equals(Bitmap bitmap1, Bitmap bitmap2) { Bi ...

Is it possible to customize the default styling options in Tailwind?

I am currently working on a blog using NextJS, and I have encountered an issue with Tailwind's list style type. It seems that the default styling for list style type is set to list-none, resulting in my <ul> <li> elements not being styled ...