Eloquent Javascript Chapter 10: Modules
when writing a module's interface...
- prefer functions to stateful objects - follow existing conventions if possible - consider how your output will be composed with other functions
scripts vs. ES modules
Scripts: bindings in global scope, cannot directly reference other scripts Modules: get their own scope, support import & export keywords to declare their dependencies and interface Module support started in 2015
Why might you compile your code into a different JS version?
So you can write in Typescript, but the code can then be converted to older JS versions so more browsers can use it
NPM
a repository of javascript packages (node package manager)
What can you use 'export' on in a module?
functions, class, bindings
How to import everything from a module?
import * as local_scope_name from "path/to/module";
How to import from a module?
import {export_name as local_scope_name} from "path/to/module";
How to import the default from a module?
omit the braces import local_scope_name from "path/to/module";
CommonJS Modules
require function & exports object the pre-ESmodule solution. A regular script, but it has access to two bindings that it uses for module interaction: `require` and `exports`
bundlers
tools that combine modularized programs into a single file
minifiers
tools that make a javascript file smaller for lower latency