Skip to content

Instantly share code, notes, and snippets.

@lukehoban
Created October 16, 2012 18:11
Show Gist options
  • Save lukehoban/3900992 to your computer and use it in GitHub Desktop.
Save lukehoban/3900992 to your computer and use it in GitHub Desktop.
ES6 module syntax alternatives
Things I want to be able to do:
a) Bind a name to a remote module
b) Alias a name for a module
c) Alias a name for a member of a remote module
d) Put all names from inside a module in lexical scope
e) Put all names from inside a remote module in lexical scope
f) Put a few names from inside a module in lexical scope
g) Define a local module
h) Export names from lexical scope
i) Re-export names from other modules
j) Re-export all names from another module
Note that (d) and (e) are likely to be removed from ES6. Per @dherman comments below, (d) is possibly a non-goal while (e) is just a postponed goal.
// #1 – Current (A)
a) import 'a' as a
b) module b = X.Y.Z
c) import { X: c} from ‘ide/code’
d) import * from X.Y.Z
e) import * from 'a';
f) import { d, e: e2 } from 'a'
g) module X { }
h) export { d, e: e2}
i) export { K: a.K } // I believe it is not possible to do this without also having the corresponding import line (a) above
j) export * from X.Y
Pros: Mostly shorter
Cons: Lots of new syntax (module, import, from, as, =, *), feeling of inconsistency
// #2 – Current (B)
a) module a = “a”;
b) module b = X.Y.Z
c) import { X: c} from 'ide/code'
d) import * from X.Y.Z
e) import * from "a";
f) import { d, e: e2 } from 'a'
g) module X { }
h) export { d, e: e2}
i) export { K: a.K } // I believe it is not possible to do this without also having the corresponding import line (a) above
j) export * from X.Y
Pros: Slightly more consistent than (A)
Cons: Still a fair bit of new syntax (module, import, from, =, *), still inconsistent, though less so
// #3 – An alternative proposal currently used by TypeScript
a) import a = module('a');
b) import b = X.Y.Z
c) import c = module('ide/code').X
d) import X.Y.Z
e) import module('a');
f) import { d, e: e2 } = module('a')
g) module X { }
h) export { d, e: e2}
i) export { K: module('a').K }
j) export * from X.Y
Pros: Consistent use of 'import' for all notions of import, limited new syntax (import, module, =)
Cons: Wordier with use of module('a')
@lukehoban
Copy link
Author

@dherman: Yes, your line of reasoning makes sense. Cases (d) and (e) are the two that I had thought least about so far, both because they are being postponed from ES6 and because TypeScript similarly has not incorporated any syntax for these cases. But I do agree that designing the syntax with these cases in mind is a good idea.

I've updated the gist with a version of your h,i,j. I believe the moral equivalents of (i) and (j) in particular weren't possible with a single export expression in the earlier syntax proposals on the wiki?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment