-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not use require with variable in mixin #122
Comments
I suspect that the problem is the use of //- works well
img(src=require('../path/to/image.png')) However, things get buggy when using a variable : - var path = '../path/to/image.png'
img(src=require(path)) which can outputs :
Is this problem linked to |
I made a minimal bug repo there https://github.com/Serrulien/bug-repo-pug-loader-require-with-variable As a template, I used : //- faultless
//- img(src=require('./logo.png'))
//- faulty
- var path = './logo.png'
img(src=require(path)) Then I looked at
Let's focus on the img tag (second one is the faulty one) :
I managed to fix the faulty case by casting
So, one quick and dirty fix would be to add |
I found a straightforward workaround. We just have to use attribute interpolation with the argument of myTemplate.pug : - const path = './logo.png'
//- buggy
img(src=require(path))
//- works
img(src=require('./logo.png'))
img(src=require(`${path}`))
img(src=require('' + path)) |
I know it's too late, but other people who are looking for the answer about using variables with pug-loader will appreciate. The problem is that require works in pug-loader as in browser. So require(var) is a dynamic require. The solution is to partially provide path for webpack. - let img = 'image.png';
img(src=require('../images' + img)) |
One more problem. //'@img': path.resolve(__dirname, '../../src/assets/img/')
- let a = '1-1.png';
img(src=require(`@img/${a}`)); But when I build the project (webpack=> file-loader), this code copies ALL the images from the @img/ folder. If you do not use the variable in require then only 1-1.png will be copied. Even if I leave empty file-loader settings or write require ('@ img / 1-1.png) .default |
Yes, this is correctly usage in the case with a dynamic require. alias: {
Images: path.resolve(__dirname, 'src/assets/images/')
} - const files = ['image1.jpeg', 'image2.jpeg', 'image3.jpeg']
each file in files
img(src=require(`Images/${file}`)) More usage examples of |
Error:
The text was updated successfully, but these errors were encountered: