ES6 import in for-of-loop

Viewed 15388

Is there any way to import and export multiple files using for-of-loop (or another loop) in ES6?

const moduleNames = ['NumberUtils', 'StringUtils', 'ArrayUtils', 'MyModule', 'AnotherModule', 'BaseModule']

let modules = {}

for (const moduleName of moduleNames) {
    import module from './' + moduleName
    modules.moduleName = module
}

export modules

Without loop I have to write:

import NumberUtils from './NumberUtils'
import StringUtils from './StringUtils'
import ArrayUtils from './ArrayUtils'
import MyModule from './MyModule'
import AnotherModule from './AnotherModule'
import BaseModule from './BaseModule'

export {
    NumberUtils,
    StringUtils
    ArrayUtils
    MyModule
    AnotherModule
    BaseModule
}
3 Answers

For multiple import files I found this solution:

const files = require.context('../myFolder', true, /(Module|Utils)\.js$/)
Related