Loading Webpack handlebars images with file-loader

1.8k views Asked by At

Not sure if I'm approaching this correctly, but I'm trying to use handlebars templates with file-loader in Webpack. The issue I'm having is the assets folder I have with images isn't being copied over to the dist folder, and the img src is being resolved with file-loader to pull in images. If there is a post that I couldn't find that would be helpful please share or if there is an article with helpful information I'll take that as well.

Thanks in advance for any help.

Webpack.config

const webpack = require('webpack');

module.exports = {
  entry: "./src/index.js",
  plugins: [
    new webpack.LoaderOptionsPlugin({
      options: {
        handlebarsLoader: {}
      }
    })
  ],
  module: {
    rules: [
      {
        test: /\.handlebars$/,
        loader: "handlebars-loader"
      },
      {
        test: /\.html$/,
        use: ["html-loader"]
      },
      {
        test: /\.(svg|png|jpe?g|gif)$/i,
        use: {
          loader: "file-loader",
          options: {
            name: "[name].[hash].[ext]",
            outputPath: "imgs/",
            useRelativePath: true
          }
        }
      }
    ]
  }
}

Handlebars template

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>{{ htmlWebpackPlugins.options.title }}</title>
</head>

<body>
  {{> templates/title}}
  <img src="./assets/serve.svg" alt="Serve svg" />
</body>

</html>

package.json

  "name": "Webpackolypse",
  "version": "1.0.0",
  "private": true,
  "description": "",
  "main": "./src/index.js",
  "scripts": {
    "start": "webpack-dev-server --config webpack.dev.js --open",
    "build": "webpack --config webpack.prod.js --verbose"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "copy-webpack-plugin": "^5.1.1",
    "css-loader": "^3.2.1",
    "file-loader": "^5.0.2",
    "handlebars-loader": "^1.7.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.9.0",
    "node-sass": "^4.13.0",
    "optimize-css-assets-webpack-plugin": "^5.0.3",
    "sass-loader": "^8.0.0",
    "style-loader": "^1.0.0",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.10.1",
    "webpack-merge": "^4.2.2"
  },
  "dependencies": {
    "clean-webpack-plugin": "^3.0.0",
    "handlebars": "^4.5.3",
    "sass": "^1.24.0"
  }
}
2

There are 2 answers

4
prathameshk73 On BEST ANSWER

You need to install CopyWebpackPlugin to copy assets to dist folder. I was getting the same issue in webpack 4.

Try using this plugin

const CopyWebpackPlugin = require('copy-webpack-plugin');

plugins: [

      new CopyWebpackPlugin([
                {
                    from: './src/assets',
                    to: './assets'
                }
            ])
]

0
Slimane amiar On

In webpack 5 you add inlineRequires: '\/assets/icons\/' option to handlebars-loader

 { 
        test: /\.handlebars$/, 
        loader: "handlebars-loader",
        options: {
          knownHelpersOnly: false,
          inlineRequires: '\/assets/icons\/'
        },
      },

and in your handlebars file you refer the image with relative path <img width="16" src="../assets/icons/face-smile-regular.svg" alt="icon" />