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

I'm looking for an elegant way to import all named exported items without the need for a default import.

In one file, I export many named constants plus the 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 importing 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 :

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

      

because I don't need the default in anotherFile.js

, and my brute force tools fool me certain but unused myDefault

. I also don't want:

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

      

because too much is being printed. I also < don't want 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!

+3


source to share


1 answer


There is no separation between "named" and "default" exports. The default export is a named export, it just has a name default

that is specifically meant for ease of use by certain syntax.

The only way to import all exported keys is



import * as foo from "foo";

      

which will contain the named export default

, if any. If you want to exclude it from your checks, you still have to deal with it in your own logic, as you did with your example omit()

.

+9


source







All Articles