Skip to content Skip to sidebar Skip to footer

Html-loader + File-loader Not Bundling The Correct Image Source

I'm planning to use Webpack for a project and I'm setting up my workflow with Html-loader + file-loader to get a production html file with dynamic src for the images, as Colt Steel

Solution 1:

If file-loader verions is 5.0 then. Adding another option on file-loader as "esModule: false" will solve this problem.

{
    test:/\.(png|jpe?g|gif|svg)$/i,
    use: [
      {
        loader:'file-loader',
        options: {
          name:'[name].[hash].[ext]',
          publicPath:'assets',
          outputPath:'assets/img',
          esModule:false
        }
      }
    ]
  }

Solution 2:

If you are using webpack 5 or later, the file-loader is depricated. File loader documentation You could use the module asset/resource instead. Remove your file-loader rule and add the following rule to your module in webpack.common.js

module: {
    rules: [
        {
            test: /\.(png|jpe?g|gif|svg)$/i,
            type: 'asset/resource'
        }
    ]
},

then add assetModuleFilename in the output in your webpack.prod.js

output: {
    assetModuleFilename: "assets/img/[name].[hash][ext]"
}

and keep your html-loader rule in the webpack.common.js as it is.

References Asset Modules

Post a Comment for "Html-loader + File-loader Not Bundling The Correct Image Source"