Skip to content

Instantly share code, notes, and snippets.

@samoshkin
Created January 16, 2018 20:09
Show Gist options
  • Save samoshkin/d68adc198c2f745dec8d969a31a3196d to your computer and use it in GitHub Desktop.
Save samoshkin/d68adc198c2f745dec8d969a31a3196d to your computer and use it in GitHub Desktop.
ES module syntax examples
// import a module with side-effect without any import bindings
import 'jquery';
// import the default export of a module
import $ from 'jquery';
// import a named export of a module
import { reduce } from 'underscore';
// import a named export to a different name
import { reduce as fold } from 'underscore';
// import an entire module as an object
import * as crypto from 'crypto';
// import both default and named exports
import _, * as lib from 'underscore';
import _, { reduce } from 'underscore';
// named export
export let x = 42;
export function foo() {};
// default export
export default 42;
export default function foo() {};
// more verbose/pedantic way for default export
let x = 42;
export { x as default };
// export an existing variable as named export
let encrypt = function() {}
export { encrypt };
// export a variable as named export with another name
export { encrypt as enc };
// re-export all exports from another module
export * from 'crypto';
// re-export selectively with renaming
export { foo as myFoo, bar } from 'module';
// reexport the default as is
export { default } from 'foo';
export { default as default } from 'foo'; // more verbose form of the above
// reexport named as default
export { myFunc as default } from 'foo';
// re-export default as named
export { default as bar } form 'module'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment