tc39 / proposal-export-default-from

Proposal to add `export v from "mod";` to ECMAScript.
https://tc39.github.io/proposal-export-default-from/
306 stars 12 forks source link

Support `export * as ns from "mod";` #3

Closed demurgos closed 6 years ago

demurgos commented 6 years ago

In the current spec and proposal, the symmetry between import and export is not complete. In particular, import * as ns from "mod"; does not have an equivalent export * as ns from "mod";.

The following table is presented at the bottom of the README:

Statement Form [[ModuleRequest]] [[ImportName]] [[LocalName]] [[ExportName]]
... ... ... ... ...
import * as ns from "mod"; "mod" "*" "ns"
export * from "mod"; "mod" "*" null null (many)

It seems to imply a symmetry, but there is none. The export * from "mod" is the only form where the information about the names is non-local and depends on the exported names of another module (you can't know what is actually exported unless you look into "mod"). The equivalent form would be an import * from "mod"; importing many names. This kind of form can cause breaking changes if the dependencies are updated and export new names that collide. Requiring a local name on import (as ns) prevents these collisions. There should be a symmetric safe export allowing you to export the the mod namespace under a single name: export * as ns from "mod".

Reexporting a whole module is common if you want to create a library with a single entry point that reexports the value of its internal modules.

Here is an existing example:

import * as errors from "./errors";
export {errors};

It should be possible to rewrite it without introducing a local name:

export * as errors from "./errors";

Here is a real world example that could be simplified.

ljharb commented 6 years ago

That is already a needs-consensus PR: https://github.com/tc39/ecma262/pull/1174

demurgos commented 6 years ago

Thanks for the link.