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
  • 16,616
  • 4
  • 47
  • 54
Ericson Willians
  • 7,216
  • 11
  • 56
  • 107

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.

Slick86
  • 2,748
  • 19
  • 20