Search code examples
webpackpugpug-loader

img(src=require(...)) generates ugly html


I have written pug-file with the next content:

img(src=require("../../assets/icons/icon.png"))

And try to build it to html by Webpack by using the next webpack config:

  module: {
    rules: [
      {
        test: /\.pug$/i,
        use: {
          loader: 'pug-loader', 
          options: {
            pretty: true
          },
        },
      },

....

      {
        test: /\.(png|jpg|svg|jpeg|gif)$/,
        exclude: [
         /Montserrat-Bold.svg$/,
         /Montserrat-Regular.svg$/,
         /Quicksand-Regular.svg$/,
         ],
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name].[ext]",
              outputPath: "assets/icons/",
              publicPath: '../assets/icons/'
            }
          }
        ]
      },
.....

As a result it builds into

<img src="{&quot;default&quot;:&quot;../assets/icons/icon.png&quot;}">

And image does not load when I open html-page with this content.

What should I do to build img(src=require("../../assets/icons/icon.png")) into <img src="../assets/icons/icon.png"> ?


Solution

  • It need to add esModule: false to file-loader options:

      use: [
          {
            loader: "file-loader",
            options: {
              name: "[name].[ext]",
              outputPath: "assets/icons/",
              publicPath: '../assets/icons/',
              esModule: false,
            }
          }
        ]
    

    I found this answer here: https://ru.stackoverflow.com/questions/1091138/webpack-2-pug-%D0%BD%D0%B5-%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%D0%B5%D1%82-require