0

I've got an object that's generated basing on another object. The object's type is fully known. It looks like this.

const mediaHooks = {
   sm: () => {}, //some func
   md: () => {}, //some func
};

Now, I would like to 'spread' that object before exporting, so its properties count as a top-level export and I can import them like:

import {sm} from './media';

Is it possible?

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
Sun
  • 299
  • 1
  • 8
  • 1
    @Terry default exporting an object isn't the same thing as named exporting all of its properties separately, see e.g. https://stackoverflow.com/q/41283733/3001761 – jonrsharpe Mar 28 '21 at 16:49

2 Answers2

1

No, this is not technically possible.

Instead, consider flipping your approach by using named exports and namespace imports

// media-hooks.js
export const sm = () => {}; //some func
export const md = () => {}; //some func

// app.js
import * as mediaHooks from './media-hooks' ;
import {sm} from './media-hooks';
Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
0

You can use export assignments for a commonJs/AMD like export to get the wished behavior. But this removes the ability to export anything else in that file:

export = mediaHooks;
// can be imported now as import {sm} from './media'; in another file

Everything else you want to export from that file also needs to be contained in the mediaHooks object. If that's not feasible for you, there is no way to spread an object in an export. In that case maybe something like this would be a good enough alternative:

export const {sm, md} = mediaHooks;
Mirco S.
  • 2,510
  • 4
  • 13