└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # ES6 (ES2015) Modules Cheatsheet 2 | 3 | This document is adapted from an [excellent proposal by leebyron](https://github.com/leebyron/ecmascript-more-export-from). 4 | 5 | ### Imports and Exports: 6 | 7 | Import Statement Form | [[ModuleRequest]] | [[ImportName]] | [[LocalName]] 8 | --------------------- | ----------------- | -------------- | ------------- 9 | `import v from "mod";` | `"mod"` | `"default"` | `"v"` 10 | `import {default as v} from "mod";` | `"mod"` | `"default"` | `"v"` 11 | `import * as ns from "mod";` | `"mod"` | `"*"` | `"ns"` 12 | `import {x} from "mod";` | `"mod"` | `"x"` | `"x"` 13 | `import {x as v} from "mod";` | `"mod"` | `"x"` | `"v"` 14 | `import "mod";` | | | 15 | 16 | 17 | Export Statement Form | [[ModuleRequest]] | [[ImportName]] | [[LocalName]] | [[ExportName]] 18 | --------------------- | ----------------- | -------------- | ------------- | -------------- 19 | `export var v;` | **null** | **null** | `"v"` | `"v"` 20 | `export default function f(){};` | **null** | **null** | `"f"` | `"default"` 21 | `export default function(){};` | **null** | **null** | `"*default*"` | `"default"` 22 | `export default 42;` | **null** | **null** | `"*default*"` | `"default"` 23 | `export {x}`; | **null** | **null** | `"x"` | `"x"` 24 | `export {x as v}`; | **null** | **null** | `"x"` | `"v"` 25 | `export {x as default}`; | **null** | **null** | `"x"` | `"default"` 26 | `export {x} from "mod"`; | `"mod"` | `"x"` | **null** | `"x"` 27 | `export {x as v} from "mod"`; | `"mod"` | `"x"` | **null** | `"v"` 28 | `export {default as v} from "mod";` | `"mod"` | `"default"` | **null** | `"v"` 29 | `export * from "mod"`; | `"mod"` | `"*"` | **null** | **null** 30 | 31 | 32 | ### Symmetry between import and export 33 | 34 | There's a syntactic symmetry between the export-from statements and the import 35 | statements they resemble. There is also a semantic symmetry; where import 36 | creates a locally named binding, export-from creates an export entry. 37 | 38 | As an example: 39 | 40 | ```js 41 | export {v} from "mod"; 42 | ``` 43 | 44 | Is symmetric to: 45 | 46 | ```js 47 | import {v} from "mod"; 48 | ``` 49 | 50 | #### Table showing symmetry 51 | 52 | Statement Form | [[ModuleRequest]] | [[ImportName]] | [[LocalName]] | [[ExportName]] 53 | -------------- | ----------------- | -------------- | -------------- | -------------- 54 | `import v from "mod";` | `"mod"` | `"default"` | `"v"` | 55 | `import * as ns from "mod";` | `"mod"` | `"*"` | `"ns"` | 56 | `import {x} from "mod";` | `"mod"` | `"x"` | `"x"` | 57 | `export {x} from "mod";` | `"mod"` | `"x"` | **null** | `"x"` 58 | `import {x as v} from "mod";` | `"mod"` | `"x"` | `"v"` | 59 | `export {x as v} from "mod";` | `"mod"` | `"x"` | **null** | `"v"` 60 | `export * from "mod";` | `"mod"` | `"*"` | **null** | **null** (many) 61 | 62 | ### Compound statements 63 | 64 | You can also import the default value and named values in a compound statement: 65 | 66 | ```js 67 | import v, {x, y as w} from "mod"; 68 | // which is the same as: 69 | import {default as v, x, y as w} from "mod"; 70 | ``` 71 | 72 | As well as 73 | 74 | ```js 75 | import v, * as ns from "mod"; 76 | ``` 77 | --------------------------------------------------------------------------------