1

I have many .css / .jpg / etc files within a folder called "assets", and I'd like to import all of them recursively. Something python-like like:

from './assets' import *;

Instead of writing a list of:

import './assets/img/ad0.jpg';
import './assets/img/ad1.jpg';
import './assets/img/ad2.jpg';
...

Is that possible?

Fabian Schultz
  • 18,138
  • 5
  • 49
  • 56
Ericson Willians
  • 7,606
  • 11
  • 63
  • 114

1 Answers1

2

You can do this in your entry point of your module

function requireAll(r) { 
   r.keys().forEach(r); 
}

requireAll(require.context('./assets', true, /\.jpg$/));

The last parameter of require.context is just a regex so you could include as many file extensions as you want. The second parameter specifies if you want to search recursively.

Chris Woolum
  • 2,854
  • 20
  • 20