This issue is related to webpack and not pug-loader.
There is 2 types of require.
The first type is static require.
img(src=require('images/1.png'))
works well because the path to the asset (1.png) will never change and the file path can be known without any execution or compilation of the code.
It's dynamic when a require()
contains an expression rather than a string literal, so the exact module is not known on compile time. This is what you aimed when writing img(src=require(image.thumb))
.
Unfortunately, by design webpack doesn't support a variable as argument, it must be a string or a string concatenation. In this case, webpack will warn the user with Critical dependency: the request of a dependency is an expression
. This is further explained here.
Solution
The idea is to create an expression with our variable.
We can either write :
img(src=require(image.thumb +''))
img(src=require(`{image.thumb}`))
img(src=require('images/' + image.thumb))
But be aware of the bundle size output as the first two will require every files of your projet as webpack will require every files that match .*
. Whereas the last one will only match 'images/.*'