5

I have a file modules.js that exports all default exports from my modules by their module name (following ES6 exporting/importing in index file):

export { default as a } from './a/a'
export { default as b } from './b/b'
...
export { default as y } from './y/y'
export { default as z } from './z/z'

In my app.js, I want to import this module bundle.

I want the exported variables from a...z to be imported from module.js so I can use them in app.js.

Obviously I don't want to do

import { a, b, c, d, ... , x, y, z } from './modules/modules'

as that is very verbose and whenever I would add a module in module.js, I would also have to change the import in app.js.

I've tried

import * from './modules/modules'

but the variables are not available in app.js.

Question: How do I simply import all named exports under their export name?

Example module ./modules/a/a.js:

export default function a() {
  // whatever
}
Community
  • 1
  • 1
connexo
  • 53,704
  • 14
  • 91
  • 128
  • You can do something like this `import m from './modules/modules'` and access the variable as `m.a` or `m.b`... I think it wouldn't cost a lot. – Mohit Sep 26 '18 at 11:51
  • @Mohit That might not cost alot, but is it not possible to import without the necessity for an owner object? – connexo Sep 26 '18 at 11:59
  • There is no additional cost to having a module namespace object from `import * as foo from ...`. In fact, by having it, engines can tell where variables are coming from ahead of time and thus better optimize your code. – loganfsmyth Sep 26 '18 at 21:26
  • @connexo As far as I know, there is no any possible way of what you are expecting in the current version of ECMAScript 6 – Mohit Sep 27 '18 at 13:09

1 Answers1

5

Just try import * as sampleModule from './modules/modules'

You can utilize all the exported function from module using sampleModule variable.

import * as sampleModule from './modules/modules'

import './modules/modules'