Slide 1

Slide 1 text

JavaScript Modules

Slide 2

Slide 2 text

@Jack_Franklin javascriptplayground.com

Slide 3

Slide 3 text

gocardless.com

Slide 4

Slide 4 text

The Problem

Slide 5

Slide 5 text

Dependencies

Slide 6

Slide 6 text

Solutions

Slide 7

Slide 7 text

AMD (Require.js)

Slide 8

Slide 8 text

define(['jquery'], function($) { return { red: function() { $('p').css('color', 'red');
 } }
 }); require(['./file'], function(f) { f.red(); });

Slide 9

Slide 9 text

define(['jquery'], function($) { return { red: function() { $('p').css('color', 'red');
 } }
 }); require(['./file'], function(f) { f.red(); });

Slide 10

Slide 10 text

define(['jquery'], function($) { return { red: function() { $('p').css('color', 'red');
 } }
 }); require(['./file'], function(f) { f.red(); });

Slide 11

Slide 11 text

define(['jquery'], function($) { return { red: function() { $('p').css('color', 'red');
 } }
 }); require(['./file'], function(f) { f.red(); });

Slide 12

Slide 12 text

define(['jquery'], function($) { return { red: function() { $('p').css('color', 'red');
 } }
 }); require(['./file'], function(f) { f.red(); });

Slide 13

Slide 13 text

no dev build step build tool for prod specific syntax to learn asynchronous

Slide 14

Slide 14 text

Browserify

Slide 15

Slide 15 text

// module.js var $ = require('jquery'); module.exports = { red: function() { $('p').css('color', 'red');
 }
 }; // app.js var app = require('./module'); app.red();

Slide 16

Slide 16 text

// module.js var $ = require('jquery'); module.exports = { red: function() { $('p').css('color', 'red');
 }
 }; // app.js var app = require('./module'); app.red();

Slide 17

Slide 17 text

// module.js var $ = require('jquery'); module.exports = { red: function() { $('p').css('color', 'red');
 }
 }; // app.js var app = require('./module'); app.red();

Slide 18

Slide 18 text

// module.js var $ = require('jquery'); module.exports = { red: function() { $('p').css('color', 'red');
 }
 }; // app.js var app = require('./module'); app.red();

Slide 19

Slide 19 text

dev build step build tool for prod CommonJS (Node) synchronous

Slide 20

Slide 20 text

tags

Slide 21

Slide 21 text

// module.js var red = function() { $('p').css('color', 'red');
 } // app.js red(); // index.html

Slide 22

Slide 22 text

// module.js var red = function() { $('p').css('color', 'red');
 } // app.js red(); // index.html

Slide 23

Slide 23 text

// module.js var red = function() { $('p').css('color', 'red');
 } // app.js red(); // index.html

Slide 24

Slide 24 text

// module.js var red = function() { $('p').css('color', 'red');
 } // app.js red(); // index.html

Slide 25

Slide 25 text

no dev build step (ish) build tool for prod (ish) no dependency management async or sync

Slide 26

Slide 26 text

no dependency management

Slide 27

Slide 27 text

We can do better

Slide 28

Slide 28 text

We have done better

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

ES6 Modules

Slide 31

Slide 31 text

// module.js import $ from './jquery'; export var red = function() { $('p').css('color', red');
 } // app.js import * as app from './module'; app.red();

Slide 32

Slide 32 text

// module.js import $ from './jquery'; export var red = function() { $('p').css('color', red');
 } // app.js import * as app from './module'; app.red();

Slide 33

Slide 33 text

// module.js import $ from './jquery'; export var red = function() { $('p').css('color', red');
 } // app.js import * as app from './module'; app.red();

Slide 34

Slide 34 text

// module.js import $ from './jquery'; export var red = function() { $('p').css('color', red');
 } // app.js import * as app from './module'; app.red();

Slide 35

Slide 35 text

// module.js import $ from './jquery'; export var red = function() { $('p').css('color', red');
 } // app.js import {red} from './module'; red();

Slide 36

Slide 36 text

// module.js import $ from './jquery'; export default function() { $('p').css('color', red');
 } // app.js import red from './module'; red();

Slide 37

Slide 37 text

// module.js import $ from './jquery'; export default function() { $('p').css('color', red');
 } // app.js import red from './module'; red();

Slide 38

Slide 38 text

Static

Slide 39

Slide 39 text

don't have to run code to know exports static lookups visibility of variables circular dependencies ready for types (!)

Slide 40

Slide 40 text

async or sync

Slide 41

Slide 41 text

static imports = can resolve before evaluation of module

Slide 42

Slide 42 text

it just…works?

Slide 43

Slide 43 text

Module API

Slide 44

Slide 44 text

// module.js import $ from './jquery'; export var red = function() { $('p').css('color', red');
 } // app.js System.import('./module') .then(function(mod) { mod.red();
 });

Slide 45

Slide 45 text

Module Config (hooks)

Slide 46

Slide 46 text

http://www.2ality.com/ 2014/09/es6-modules- final.html

Slide 47

Slide 47 text

But what about now?

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

system.js traceur.js es6-module-loader.js

Slide 50

Slide 50 text

// index.html </ script> <script> System.import('./app'); // app.js alert('hello world');

Slide 51

Slide 51 text

// index.html </ script> <script> System.import('./app'); // app.js alert('hello world');

Slide 52

Slide 52 text

// index.html </ script> <script> System.import('./app'); // app.js alert('hello world');

Slide 53

Slide 53 text

// index.html System.import(‘./app’) .catch( console.error.bind(console) ); …

Hello World

Slide 54

Slide 54 text

// red.js import $ from 'jquery' export default function() { $('p').css('color', 'red'); };

Slide 55

Slide 55 text

// red.js import $ from 'jquery' export default function() { $('p').css('color', 'red'); };

Slide 56

Slide 56 text

// red.js import $ from 'jquery' export default function() { $('p').css('color', 'red'); };

Slide 57

Slide 57 text

// app.js import $ from 'jquery' import red from './red' $(function() { red(); });

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

jspm.io

Slide 61

Slide 61 text

Demo!

Slide 62

Slide 62 text

// index.html System.import('~/app');

Slide 63

Slide 63 text

// index.html System.import('~/app');

Slide 64

Slide 64 text

npm install --global jspm

Slide 65

Slide 65 text

jspm install npm:lodash jspm install github:components/ jquery

Slide 66

Slide 66 text

// app.js import $ from 'npm:jquery'; console.log($.fn.jquery) // 2.1.1

Slide 67

Slide 67 text

this is the best solution right now

Slide 68

Slide 68 text

jspm bundle app

Slide 69

Slide 69 text

// built.html System.import('app');

Slide 70

Slide 70 text

it just…works?

Slide 71

Slide 71 text

jspm.io @guybedford

Slide 72

Slide 72 text

ES6 is coming The tooling is there And will only get better

Slide 73

Slide 73 text

http://javascriptplayground.com/ https://github.com/assetgraph/ assetgraph https://github.com/ModuleLoader/es6- module-loader https://github.com/systemjs/systemjs http://jspm.io/

Slide 74

Slide 74 text

Thanks! @jack_franklin