Cannot use es6 export module from "file" in Babel

I am using ES6 to create a messaging library with a JavaScript server. The server has certain types of JSON-encoded messages that it sends and receives through WebSocket.

To ensure that all message fields are enabled and that no additional fields are sent to the server, each message type has its own class. Each message is defined in its own file with the following structure:

export default class NameOfMessage extends BaseMessage {
    constructor(values) {
        // Here is code that checks values and sets them as properties
        // so that calling `JSON.stringify(obj)` will return the data
        // to be sent to the server.
    }
}

If the user needs only one or two types of messages in a particular file, they can write import NameOfMessageType1 from 'package_name/messages/type1', import NameOfMessageType2 from 'package_name/messages/type2'etc.

However, if the user needs many types of messages, it becomes cumbersome to include all of them. For this reason, I created an additional file messages.js, which importall message files, and then re- exportthem. In this way, users can execute import * as Message from 'package_name/messages'and access message types like Message.Type1, Message.Type2etc.

According to my reading (including http://www.2ality.com/2014/09/es6-modules-final.html and https://hacks.mozilla.org/2015/08/es6-in-depth-modules / (section "Aggregate modules"), among other sources), I should be able to use the following as the contents of the file messages.js:

export Type1 from './messages/type1';
export Type2 from './messages/type2';
...

, Babel , import * as Messages from 'package_name/messages', . ( ).

, , , :

import Type1 from './messages/type1';
import Type2 from './messages/type2';
export {Type1, Type2};
...

?

+4
2

, export ... from ...:

ExportDeclaration :
    export * FromClause ;
    export ExportClause FromClause ;
    ... // More export declarations

ExportClause :
    { }
    { ExportsList }
    { ExportsList , }

export FromClause. , from *, . default .

, :

export {default as Type1} from './messages/type1';
export {default as Type2} from './messages/type2';
...
+3

, . /, , undefined. , , , , . , .

0

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


All Articles