How to get full path from require.context?

Viewed 290

I want to include images to my website. I know that I can use file-loader and simply import image's path. But this method is not suitable for all cases. For example, sometimes I need to require dozens of photos, and these photo selection can variate from build to build.

As I understand in my case I should use require.context, because this way it can import all photos from specified directory. But here's a problem

import ava from '@assets/photos/ava.jpg';
console.log(ava);
// /static/assets/photos/ava.jpg
console.log(require.context('@assets/photos', false, /ava.jpg$/).keys());
// ["./ava.jpg"]

require.context gives only the name of the file. Is there a way to get full path?


I know I could use these names and add to each of them /static/assets/photos/ (and actually, this is how it works now), but this is seems to be a crutch.

0 Answers
Related