简体   繁体   中英

Unable to use es6 `export Module from 'file'` in Babel

I am using ES6 to build a library for JavaScript communication with a server. The server has specific types of JSON-encoded messages that it sends and receives over a WebSocket.

To make sure that all message fields are included and that no extra 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 message types 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 of the message types, it becomes cumbersome to include them all. For this reason, I created an additional file, messages.js , which import s all of the message files and then re- export s them. That way, users can do import * as Message from 'package_name/messages' and access the message types as 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 titled "Aggregating modules"), among other sources), I should be able to use the following as the contents of the messages.js file:

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

However, when I run Babel on any file that does import * as Messages from 'package_name/messages' , I get no result. (It just plain doesn't do anything).

When I change the file to have the following content, everything works as expected:

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

Why doesn't the other way work?

A closer reading of the official spec shows that there are only two ways to use export ... from ... :

 ExportDeclaration : export * FromClause ; export ExportClause FromClause ; ... // More export declarations ExportClause : { } { ExportsList } { ExportsList , } 

All other uses of export do not accept a FromClause . It turns out that the only legal use of from is either in connection with * or with a brace-enclosed list of exports. There is no way to use a default without explicitly referencing it.

Since that is the case, the following file works:

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

I just want to add that I had a very similar problem. Variables/modules I was trying to export directly from another file was coming back as undefined . However, if I broke it up into two steps by importing first and then exporting it back out, it would be fine. The cause of the problem ended up being that I was doing some type of circular importing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM