The export ___ from "module"
statements are a very useful mechanism for
building up "package" modules in a declarative way. In the ES6 spec, we can:
export {x} from "mod"
export {x as v} from "mod"
export * from "mod"
.These three export-from statements are easy to understand if you understand the semantics of the similar looking import statements.
However, there are other import statements which would have very useful export-from forms.
export v from "mod"
export * as ns from "mod"
Import Statement Form | [[ModuleRequest]] | [[ImportName]] | [[LocalName]] |
---|---|---|---|
import v from "mod"; |
"mod" |
"default" |
"v" |
import * as ns from "mod"; |
"mod" |
"*" |
"ns" |
import {x} from "mod"; |
"mod" |
"x" |
"x" |
import {x as v} from "mod"; |
"mod" |
"x" |
"v" |
Export Statement Form | [[ModuleRequest]] | [[ImportName]] | [[LocalName]] | [[ExportName]] |
---|---|---|---|---|
export var v; |
null |
null |
"v" |
"v" |
export default function f(){}; |
null |
null |
"f" |
"default" |
export default function(){}; |
null |
null |
"*default*" |
"default" |
export default 42; |
null |
null |
"*default*" |
"default" |
export {x} ; |
null |
null |
"x" |
"x" |
export {x as v} ; |
null |
null |
"x" |
"v" |
export {x} from "mod" ; |
"mod" |
"x" |
null |
"x" |
export {x as v} from "mod" ; |
"mod" |
"x" |
null |
"v" |
export * from "mod" ; |
"mod" |
"*" |
null |
null |
Export Statement Form | [[ModuleRequest]] | [[ImportName]] | [[LocalName]] | [[ExportName]] |
---|---|---|---|---|
export v from "mod"; |
"mod" |
"default" |
null |
"v" |
export * as ns from "mod"; |
"mod" |
"*" |
null |
"ns" |
There's a common pattern of the export-from statements in that they mirror a similar import statement, but with the only difference being not altering local scope.
As an example:
;
Is very similar to:
;;
However, v
is not introduced as a name in the local scope.
The two proposed additions follow this same pattern:
// proposed: export v from "mod";;;
// proposed: export * as ns from "mod";;;
Using the terminology of Table 42, the export-from form can be created from the import-then-export form by setting export-from's [[ExportName]] to import's [[LocalName]] and export-from's [[LocalName]] to null.
Statement Form | [[ModuleRequest]] | [[ImportName]] | [[LocalName]] | [[ExportName]] |
---|---|---|---|---|
import v from "mod"; |
"mod" |
"default" |
"v" |
|
export v from "mod"; |
"mod" |
"default" |
null |
"v" |
import * as ns from "mod"; |
"mod" |
"*" |
"ns" |
|
export * as ns from "mod"; |
"mod" |
"*" |
null |
"ns" |
import {x} from "mod"; |
"mod" |
"x" |
"x" |
|
export {x} from "mod"; |
"mod" |
"x" |
null |
"x" |
import {x as v} from "mod"; |
"mod" |
"x" |
"v" |
|
export {x as v} from "mod"; |
"mod" |
"x" |
null |
"v" |
import * from "mod" |
"mod" |
null |
null (many) |
|
export * from "mod"; |
"mod" |
"*" |
null |
null (many) |