ES6 - is there an elegant way to import all named exports but not the default export?

Viewed 20299

I am looking for an elegant way to import all named exports without having to import the default as well.

In one file I am exporting many named constants plus a default:

// myModule.js
const myDefault = 'my default' 
export const named1 = 'named1' 
export const named2 = 'named2'
// many more named exports - otherwise this would not be an issue...
export default myDefault 

In another file I would like to have an elegant way to import all named exports only, without having to import the default:

// anotherFile.js
// this is what I would like to do, but syntax is not supported, right?
import { * as namedOnly } from './myModule'

I do not want to:

// anotherFile.js
import myDefault, * as namedOnly from './myModule' 

because I do not need the default in anotherFile.js and my linting tools bug me about the defined but unused myDefault. Nor do I want to:

// anotherFile.js
import {
  named1,
  named2,
  ... // many more
} from './myModule'

because that's too much typing. I also do not want to object.omit the default:

// anotherFile.js
import omit from 'object.omit'
import * as all from './myModule'
const namedOnly = omit(all, 'default')

Thanks for any help!

2 Answers

ok so i using lodash ! https://lodash.com/docs/4.17.15#omit

import * as MDATA from './Modules.DATA.store';
{ defaultModuleData: _.map(_.omit(MDATA, ['default']), (ModClass) => new ModClass()) }

am not big fan and is not performance friendly but no choice !

Related