Es2015 and override the single export function of the re-exported module

I want to re-export the entire module and redefine only a specific re-export function. But it seems that the export of the override function is not processed when the same function is already re-exported.

( http://www.ecma-international.org/ecma-262/6.0/#sec-module-semantics-static-semantics-early-errors , 'This is a syntax error if ExportedNames of ModuleItemList contains any duplicate entries.')

The motivation for my approach is to minimize the explicit re-export of a very large or long module if I just want to redefine a specific function or method in a re-exported module.

Is there any way to implement my approach in es6 / es2015?

My code is:

module-a.js

export class MyFirstStandardClass { sendMeMessages() { return `hello, I'm a standard implementation`; } } export function talkToMe() { return `standard talking: how are you doing?`; } 

module-b.js

 import * as StandardModule from 'module-a'; export function talkToMe(condition = true) { if (condition) { return `project conditional talking: ${StandardModule.talkToMe()}`; } return `project without a condition!`; } export * from 'module-a'; 

module-c.js

 import * as MyModule from 'module-b'; import React, { Component } from 'react'; export default class App extends Component { componentWillMount() { console.log(MyModule); this.myFirstStandardInstance = new MyModule.MyFirstStandardClass(); } render() { return ( <div> <label> Class </label> <div> { this.myFirstStandardInstance.sendMeMessages() } </div> <label> Function </label> <div> { MyModule.talkToMe(true) } // returns 'standard talking: how are you doing?'; expected 'project conditional talking: standard talking: how are you doing?' </div> </div> ); } } 
+5
source share
2 answers

It seems my first solution should work. According to the ECMAScript specification, local exports must take precedence. ( http://www.ecma-international.org/ecma-262/6.0/#sec-getexportednames )

This is a problem in the Babylon transporter. Additional information: https://github.com/systemjs/systemjs/issues/1031#issuecomment-171262430

The problem in Babylon: https://phabricator.babeljs.io/T6967

+3
source

You can choose which modules from module-a export on a single line. So in module-b.js you can do:

 // export all modules excluding `talkToMe` export { MyFirstStandardClass, someOtherModule } from 'module-a'; export function talkToMe(condition = true) { // ... } 

Or you can export the default object and choose what to exclude / override using Object.assign ():

 import * as StandardModule from 'module-a'; const overridenExports = { talkToMe: function(condition = true) { // ... } } const myModule = Object.assign({}, StandardModule, overridenExports); export default myModule; 

and import by default, for example:

 import MyModule from 'module-b'; 
0
source

Source: https://habr.com/ru/post/1240440/


All Articles