Michal
Michal

Reputation: 2083

Export all constants from another file

I would like to create index.js file where all constants and default exports from another files will be exported. For example:

userActions.js:

export const login = (username, password) => { ... }
export const logout = () => { ... }

User.js:

export default class User { ... }

Book.js:

export default class Book { ... }

index.js

import { login, logout } from './userActions'
import Book from './Book'
import User from './User'

export { login, logout, Book, User }

And then, it will be possible this in any file:

import { login, Book, User } from './index.js'

Unfortunately, I often add new user actions and I don't want to change index.js every time. So I would like use this in index.js:

export * from './userActions'

Somewhere in another file (everything works)...

import * as allExports from './index.js'
console.log(allExports) // { login: ƒ, logout: ƒ, __esModule: true }

But if I add default exports to the named exports (index.js):

import Book from './Book'
import User from './User'
export * from './userActions'
export { Book, User }

There is only first default export (somewhere in another file):

import * as allExports from './index.js'
console.log(allExports) // { Book: f, __esModule: true }
// Expected output is: { Book: f, User: f, login: ƒ, logout: ƒ, __esModule: true }

Is there any solution for this?

Upvotes: 2

Views: 6158

Answers (1)

Ori Drori
Ori Drori

Reputation: 192507

You can use export { default as name } from './resource' to convert defaults to named exports:

export { default as Book } from './Book'
export { default as User } User from './User'
export * from './userActions'

Upvotes: 2

Related Questions