Modules

  • ES6 modules do not need ‘use strict’
  • Similar to CommonJS

Default export

The default means will import the default export.

  1. // MyClass.js
  2. class MyClass{
  3. constructor() {}
  4. }
  5. export default MyClass;
  6. // Main.js
  7. import MyClass from 'MyClass';

Multiple exports.

You can call just the exports you need from a specific module.

  1. // lib.js
  2. export const sqrt = Math.sqrt;
  3. export function square(x) {
  4. return x * x;
  5. }
  6. export function diag(x, y) {
  7. return sqrt(square(x) + square(y));
  8. }
  9. // main.js
  10. import { square, diag } from 'lib';
  11. console.log(square(11)); // 121
  12. console.log(diag(4, 3)); // 5
  13. // second.js
  14. // or you can call them with '*'
  15. // but then you have to prefix the exports with
  16. // the module name
  17. import * as lib from 'lib';
  18. console.log(lib.square(11)); // 121
  19. console.log(lib.diag(4, 3)); // 5

Export as

  1. // lib.js
  2. class MyClass {
  3. //...
  4. }
  5. // main.js
  6. import { Dude as Bro } from 'lib';
  7. var bro = new Bro(); // instanceof MyClass

Cyclical Dependencies

The following would be allowed

  1. // lib.js
  2. import Main from 'main';
  3. var lib = {message: "This Is A Lib"};
  4. export { lib as Lib };
  5. // main.js
  6. import { Lib } from 'lib';
  7. export default class Main {
  8. // ....
  9. }

More importing

  1. // lib.js
  2. // Default exports and named exports
  3. import theDefault, { named1, named2 } from 'src/mylib';
  4. import theDefault from 'src/mylib';
  5. import { named1, named2 } from 'src/mylib';
  6. // Renaming: import named1 as myNamed1
  7. import { named1 as myNamed1, named2 } from 'src/mylib';
  8. // Importing the module as an object
  9. // (with one property per named export)
  10. import * as mylib from 'src/mylib';
  11. // Only load the module, don't import anything
  12. import 'src/mylib';

More Exporting

  1. export var myVar = ...;
  2. export let myVar = ...;
  3. export const MY_CONST = ...;
  4. export function myFunc() {
  5. ...
  6. }
  7. export function* myGeneratorFunc() {
  8. ...
  9. }
  10. export class MyClass {
  11. ...
  12. }

Re-exporting

This is for exporting something you are importing.

  1. export * from 'src/other_module';
  2. export { foo, bar } from 'src/other_module';
  3. // Export other_module's foo as myFoo
  4. export { foo as myFoo, bar } from 'src/other_module';

Modules - Programatic Loading API

System.import API

This method will return a promise

  1. System.import('some_module')
  2. .then(some_module => {
  3. ...
  4. })
  5. .catch(error => {
  6. ...
  7. });

Load All

  1. Promise.all(
  2. ['module1', 'module2', 'module3']
  3. .map(x => System.import(x)))
  4. .then(function ([module1, module2, module3]) {
  5. // my code...
  6. });

System “Module” functions

  1. System.import(source);
  2. // Returns module via Promise
  3. System.module(source, options);
  4. // Returns module via Promise
  5. System.set(name, module);
  6. // Inline register a new module
  7. System.define(name, source, options?);
  8. // Eval code and register module

Module HTML Tag

To load module in the html

  1. <head>
  2. <module import="my-module.js"></module>
  3. </head>
  1. <head>
  2. <module>
  3. import $ from 'lib/jquery';
  4. console.log('$' in this); // false becaue it won't attach the import to the window global
  5. // globals trapped in module
  6. // Other JS here
  7. console.log(window); // Still can call window
  8. // let x = 1;
  9. Module Tag is force strict mode
  10. </module>
  11. </head>