ModuleImport

Sébastien Cevey seb.cevey at guardian.co.uk
Fri Jun 20 02:36:32 PDT 2014


Reading Marius' email, I realised what I find confusing in the newly
proposed syntax that uses `*' to import the default export.

The `*' symbol universally represents a glob of "everything", but when
used to import from a module that has multiple exports, you won't get
everything, you will get either the single default export (if there is
one) or nothing.

```js
// letters.js
export var a = 1;
export var b = 2;

// main.js
import {a, b} from "letters"; // ok
import * as letters from "letters"; // not what you think
```

Note that `{ * }' would be even more confusing.

Would `default' be a clearer keyword for import, so import and export
logically match?

```js
import default as Dict from "dict";
```


As a final note, and at the risk of erring in the world of speculation
that Brendan fears, are we just sleepwalking towards pushing people to
work around the whole debate with the "universal":

```js
// letters.js
export var a = 1;
export var b = 2;
export default {a: a, b: b};

// main.js
import {a, b} from "letters"; // ok
import * as letters from "letters"; // ok
```

Sorry to add more data to this already loaded thread. I guess it shows
how much people care about finding the best, most intuitive solution
to this whole issue.


On 20 Jun 2014 09:49, "Marius Gundersen" <gundersen at gmail.com> wrote:
>
> > ES6 favors the single/default export style, and gives the sweetest syntax to importing the default. Importing named exports can and even should be slightly less concise.
>
> It seems like ES6 favors single import (`import singleThing from "module"`) but prefers multiple export (`export var a = 5;`), with single/default export being a special case of named/multiple export. In the spec[1] there are 5 ways to export named things, but only one way to export a default value. The focus in the spec is therefore on multiple export, while single export is a special case using a keyword to differentiate itself. This means that a module which exports only a single (named) thing is not a single-export module, and cannot be imported using the single-import syntax:
>
> ```js
> //moduleA.js
> export class A{
>   //...
> }
> //app.js
> import A from "moduleA"; //wont work, even though moduleA only has a single export
> ```
>
> This will likely lead to confusion, since the ES6 module system does not actually favor single export modules, but default export modules. If the ES6 module system is trying to push developers towards single exporting modules, then the syntax for export should prefer single export, without requiring a keyword for it to actually be a single export module. Multiple export should be the special case, requiring a keyword or new syntax, and throwing if a module exports multiple times without the extra keyword.
>
> Unfortunately I don't have any suggestions for how this syntax could work without deviating very far from the current spec. It seems easier to get the spec to favor multiple export (which, to me, it looks like it was originally designed to favor). I don't have any preference either way, I just want it to favor one, both on the export and import side.
>
> Marius Gundersen
>
> [1](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-exports)
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>

-- 

------------------------------
Visit theguardian.com. On your mobile and tablet, download the Guardian 
iPhone and Android apps theguardian.com/guardianapp and our tablet editions 
theguardian.com/editions.  Save up to 57% by subscribing to the Guardian 
and Observer - choose the papers you want and get full digital access. 
 Visit subscribe.theguardian.com

This e-mail and all attachments are confidential and may also be 
privileged. If you are not the named recipient, please notify the sender 
and delete the e-mail and all attachments immediately. Do not disclose the 
contents to another person. You may not use the information for any 
purpose, or store, or copy, it in any way.  Guardian News & Media Limited 
is not liable for any computer viruses or other material transmitted with 
or as part of this e-mail. You should employ virus checking software.
 
Guardian News & Media Limited is a member of Guardian Media Group plc. Registered 
Office: PO Box 68164, Kings Place, 90 York Way, London, N1P 2AP.  Registered 
in England Number 908396




More information about the es-discuss mailing list