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) {
}
}
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 import
all message files, and then re- export
them. In this way, users can execute import * as Message from 'package_name/messages'
and access message types like Message.Type1
, Message.Type2
etc.
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};
...
?