0

How do I dynamically grab all of the files in a folder within a src folder in webpack? For example, say I have a directory structure like this:

src/
  packages/
    package.js
    file-a.js
    file-b.js
    file-c.js

I want to call all the file-*.js files from the packages folder at once. Right now, package.js looks like this:

export * from './file-a.js'
export * from './file-b.js'
export * from './file-c.js'

How do I dynamically export all of the file-*.js files from package.js, without having to go in and add a new line every time I add a new file.js file?

symlink
  • 10,968
  • 6
  • 26
  • 48
  • One of the main benefits of ES6 exports is the ability to do static analysis on the exports for things like dead code elimination (a.k.a. tree shaking), and importing every file in a folder might break this. That being said, you can use this: https://stackoverflow.com/questions/29421409/how-to-load-all-files-in-a-directory-using-webpack-without-require-statements – 101arrowz Nov 16 '21 at 22:20

0 Answers0