- I don't have exercises for this part
- No time to set up the sandbox logic
- You can use babel and webpack/browserify locally
- You can think of ES6 modules as a different syntax for CommonJS modules
- the thing with require/exports
- except it is more well-integrated into the language
- My initial reaction was -- why?
- Who wants static
- And why invent syntax if we can cleverly reuse existing concepts
- But they are actually quite nice
- I've ported on library (Acorn) and written a new project (ProseMirror) using such modules
- And I'm sold
- The syntax is much more pleasant
- Importing groups of bindings from a module is much cleaner
- exporting also definitely looks better
- no more need to both define a local variable and assign to exports
- The staticness is usually what you want
- If my code won't browserify/webpack that's a bug anyway
- So there's very little clever run-time tricks I can use
- Lint errors for import/export mistakes are a plus
- So here's what it looks like
- you can prefix the various declarations--var, let, const, function, class--with export
- these add an export with the same name to the module
- modules are collections of exports
Imports and exports appear the top level
- No more 'i'm going to export a function'
- except that the export named 'default' has a special role
- You can export an expression as default like this
- But it is not necessarily the only thing exported
- To import, you normally specify a list of imported stuff in braces
- But if you do this, you get the default export
- You can also list the word default in a list of imports
- Imported modules are referenced as string literals
- Static string literals, not any string expression
- The spec does not specify a resolution mechanism
- But people are mostly using Node's mechanism
- conveniently import from node_modules
- You can rename imports
- To, for example, avoid name clases
- Or import all exports under an object
- You can not import * into the local scope directly
- I think that's a good thing
- It means every identifier you pull in is explicitly declared
- No magic or unexpected conflicts
Finally, there's syntax for re-exporting
And similar syntax for exporting after the fact from the local module
- Dynamic loading
- System.import takes any string, dynamically resolves/loads
- Returns a promise -- async loading
- I am not aware of any implementations or projects using this
- but it's in the spec, will probably be adopted