Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override an export from another library es6

Given a javascript library (let's say supportlibrary) which has 100 named exports, I want to create my own compat-library which exports all named exports from supportlibrary but override a single named export with another.

For now, I can export all 99 named exports manually, but this would be a tedious job. I rather would have something like:

import {SupportComponent as ExcludedSupportComponent,...rest} from 'supportlibrary';
import SupportComponent from './MySupportComponent';

export {
    ...rest,
    SupportComponent
}

Is something like this possible using es6 / tc39-stage-x functionality? or is this only possible with CommonJs?

like image 913
Dennie de Lange Avatar asked Feb 23 '18 15:02

Dennie de Lange


People also ask

How do I reexport JavaScript?

To re-export values from another file in JavaScript, make sure to export the name exports as export {myFunction, myConstant} from './another-file. js and the default export as export {default} from './another-file. js' . The values can be imported from the file that re-exported them.

What is Esmodule?

ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse. The CommonJS module system, on the other hand, is built into Node.

What is export default in es6?

Export default is used when there is only one export to be made from a particular file and when importing this one element, we don't have to worry about giving the same name to our import. This combination of export and import allows us to implement modularity.


1 Answers

You should be able to do

export * from 'supportlibrary';
export {default as SupportComponent} from './MySupportComponent';

to re-export all of the exports from 'supportlibrary', then export one additional named property that will take precedence over the export * version.

like image 124
loganfsmyth Avatar answered Sep 28 '22 09:09

loganfsmyth