SlideShare a Scribd company logo
1 of 63
Download to read offline
Modular

JavaScript
Andrew Eisenberg

Tasktop Technologies
1
Modular

JavaScript
Andrew Eisenberg

Tasktop Technologies
1
(Non-)
A story about JavaScript
and modularization in 4
parts
2
A story about JavaScript
and modularization in 4
parts
2
No Modules

(ancient history)
A story about JavaScript
and modularization in 4
parts
2
No Modules

(ancient history)
Module Pattern

(industrial revolution)
A story about JavaScript
and modularization in 4
parts
2
No Modules

(ancient history)
Module Pattern

(industrial revolution)
Module loaders

(today)
A story about JavaScript
and modularization in 4
parts
2
No Modules

(ancient history)
Module Pattern

(industrial revolution)
Module loaders

(today)
Harmony Modules

(tomorrow)
A story about JavaScript
and modularization in 4
parts
2
No Modules

(ancient history)
Module Pattern

(industrial revolution)
Module loaders

(today)
Harmony Modules

(tomorrow)
This is really the story of a
language reaching maturity
We are Java Devs.

Why should we care?
JavaScript is not just blinky text and popups

Massive applications being built with it on
client AND server

JavaScript is useful and ubiquitous

TIOBE index #9 (March 2014)

But JavaScript is (or was) only a toy
language
3
blinky
We are Java Devs.

Why should we care?
JavaScript is not just blinky text and popups

Massive applications being built with it on
client AND server

JavaScript is useful and ubiquitous

TIOBE index #9 (March 2014)

But JavaScript is (or was) only a toy
language
3
But there is a core
goodness
4
But there is a core
goodness
4
No modules
Part I --- Ancient History (c. 1995)
5
The original approach
Script tags

All code is global
6
The original approach
Script tags

All code is global
6
html	
  
	
  	
  script	
  “foo.js”/	
  
	
  	
  script	
  “bar.js”/	
  
/html
The original approach
Script tags

All code is global
6
html	
  
	
  	
  script	
  “foo.js”/	
  
	
  	
  script	
  “bar.js”/	
  
/html
//	
  foo.js	
  
var	
  x	
  =	
  9;
The original approach
Script tags

All code is global
6
html	
  
	
  	
  script	
  “foo.js”/	
  
	
  	
  script	
  “bar.js”/	
  
/html
//	
  foo.js	
  
var	
  x	
  =	
  9;
//	
  bar.js	
  
console.log(x);
Prognosis?
Name clashes

No explicit dependencies

Order is important

Just pray that all dependencies are available
7
Prognosis?
Name clashes

No explicit dependencies

Order is important

Just pray that all dependencies are available
7
Ugggh!
Module pattern
Part II --- Industrial Revolution (c. 2006)
8
Module pattern
All code is still global

Each script defines its
own namespace

Pattern has many
variations
9
Module pattern
All code is still global

Each script defines its
own namespace

Pattern has many
variations
9
html	
  
	
  	
  script	
  “foo.js”/	
  
	
  	
  script	
  “bar.js”/	
  
/html
Module pattern
All code is still global

Each script defines its
own namespace

Pattern has many
variations
9
html	
  
	
  	
  script	
  “foo.js”/	
  
	
  	
  script	
  “bar.js”/	
  
/html
//	
  foo.js	
  
foo	
  =	
  foo	
  ||	
  {};	
  
foo.x	
  =	
  9;
Module pattern
All code is still global

Each script defines its
own namespace

Pattern has many
variations
9
html	
  
	
  	
  script	
  “foo.js”/	
  
	
  	
  script	
  “bar.js”/	
  
/html
//	
  foo.js	
  
foo	
  =	
  foo	
  ||	
  {};	
  
foo.x	
  =	
  9;
//	
  bar.js	
  
(function(foo)	
  {	
  
	
  var	
  x	
  =	
  “Number	
  ”;	
  
	
  console.log(x	
  +	
  foo.x);	
  
})(foo);
Still very popular
e.g., jQuery fn namespace*
10
* jQuery 1.7 introduced AMD modules
Still very popular
e.g., jQuery fn namespace*
10
(function(	
  $	
  )	
  {	
  
	
  $.fn.myPlugin=function(args){	
  
	
  	
  //	
  awesome	
  plugin	
  stuff	
  
	
  };	
  
})(	
  jQuery	
  );	
  
	
  $.fn.myPlugin(args);
* jQuery 1.7 introduced AMD modules
Prognosis?
Name clashes,

but far less common

Easier to mock dependencies

But…

Order matters

Just pray that all
dependencies are available
11
Prognosis?
Name clashes,

but far less common

Easier to mock dependencies

But…

Order matters

Just pray that all
dependencies are available
11
Better
Prognosis?
Name clashes,

but far less common

Easier to mock dependencies

But…

Order matters

Just pray that all
dependencies are available
11
Better
But still ugggh!
Module loaders
Part III --- Today
12
The module explosion
13
Idea!

Use the environment to
selectively load modules in
order
The module explosion
13
Idea!

Use the environment to
selectively load modules in
order
Relies on a module
loader outside the
language specification
The module explosion
Formats

CommonJS

AMD
13
Idea!

Use the environment to
selectively load modules in
order
Relies on a module
loader outside the
language specification
The module explosion
Formats

CommonJS

AMD
Loaders

node (CJS)

requireJs (AMD)

curl (AMD)

…
13
Idea!

Use the environment to
selectively load modules in
order
Relies on a module
loader outside the
language specification
CommonJS
Used by node

Bad news for the web
14
var	
  modA	
  =	
  require(‘moduleA’),	
  
	
  	
  modB	
  =	
  require(‘moduleB’);	
  
!
export.aVal	
  =	
  modA.myVal;	
  
export.bVal	
  =	
  modB.myVal;
CJS is not web-friendly
15
//	
  main.js	
  
if	
  (needSub)	
  {	
  
	
  	
  require('./sub');	
  
}
//	
  sub.js	
  
console.log(‘here’);
Conditional loading of modules. (Yay!)

But, that means synchronous loading

Synchronous loading makes for horrendous web
experience
Asynchronous module
definition (AMD)
Asynchronous
loading of
modules

Makes a
happy web

Syntax more
cumbersome
16
define(‘myModule’,	
  
	
  	
  [‘moduleA’,	
  ‘moduleB],	
  	
  
	
  	
  function(a,	
  b)	
  {	
  
	
  	
  return	
  {	
  	
  
	
  	
  	
  	
  aVal:	
  a.myVal,	
  
	
  	
  	
  	
  bVal	
  :	
  b.myVal	
  
	
  	
  }	
  
});
Require.js: a
sort of deep dive
(There’s magic,

but the good kind)
17
Universal Module
Declarations (UMD)
Attempt to unify client and server side modules

BUT not a standard

Semantics different browser vs. server
18
Universal Module
Declarations (UMD)
Attempt to unify client and server side modules

BUT not a standard

Semantics different browser vs. server
18
define(function(require,export,module){	
  
	
  	
  var	
  modA	
  =	
  require(‘moduleA’),	
  
	
  	
  	
  	
  modB	
  =	
  require(‘moduleB’);	
  
	
  	
  export.aVal	
  =	
  modA.myVal;	
  
	
  	
  export.bVal	
  =	
  modB.myVal;	
  
});
But UMD needs even

more boilerplate
19
All sorts of UMD proposals here:

https://github.com/umdjs/umd
But UMD needs even

more boilerplate
19
All sorts of UMD proposals here:

https://github.com/umdjs/umd
//	
  Boilerplate	
  
if	
  (typeof	
  module	
  ===	
  'object'	
  	
  	
  
	
  	
  	
  	
  typeof	
  define	
  !==	
  'function')	
  {	
  
	
  	
  var	
  define	
  =	
  function	
  (factory)	
  {	
  
	
  	
  	
  	
  module.exports	
  =	
  factory(require,exports,module);	
  
	
   };	
  
}	
  
//	
  Here	
  is	
  the	
  actual	
  module	
  
define(function	
  (require,	
  exports,	
  module)	
  {	
  
	
  	
  var	
  b	
  =	
  require('b');	
  
	
   return	
  function	
  ()	
  {};	
  
});
Browserify: no more
boilerplate
Pre-processor

converts CJS - AMD

something browser compatible

Avoid AMD boilerplate

Can use some node.js libs in browser
20
*http://browserify.org/
Browserify example
21
// require the core node events module!
var EventEmitter = require(‘events')!
! .EventEmitter;!
!
//create a new event emitter!
var emitter = new EventEmitter;!
!
// set up a listener for the event!
emitter.on('pizza', function(message){!
console.log(message);!
});!
!
// emit an event!
emitter.emit('pizza', ‘yummy!');
Browserify example
21
// require the core node events module!
var EventEmitter = require(‘events')!
! .EventEmitter;!
!
//create a new event emitter!
var emitter = new EventEmitter;!
!
// set up a listener for the event!
emitter.on('pizza', function(message){!
console.log(message);!
});!
!
// emit an event!
emitter.emit('pizza', ‘yummy!');
$	
  browserify	
  index.js	
  	
  bundle.js
Prognosis?
22
Prognosis?
No more language level name clashes
Not order dependent
Dependencies explicit in module
Load and edit time checkers
22
Prognosis?
No more language level name clashes
Not order dependent
Dependencies explicit in module
Load and edit time checkers
22
Yay! We did it!
Prognosis?
No more language level name clashes
Not order dependent
Dependencies explicit in module
Load and edit time checkers
22
Yay! We did it!
...right?
Prognosis?
No more language level name clashes
Not order dependent
Dependencies explicit in module
Load and edit time checkers
BUT...

Too many competing module systems

modules for client and server 

Modules still not first class

Boilerplate
22
Yay! We did it!
...right?
Prognosis?
No more language level name clashes
Not order dependent
Dependencies explicit in module
Load and edit time checkers
BUT...

Too many competing module systems

modules for client and server 

Modules still not first class

Boilerplate
22
Yay! We did it!
...right?
Ugggh!
Harmony Modules
Part IV --- Tomorrow
23
Real Modules
ES6 Harmony

Official JavaScript module spec

Sync and async friendly

Browser and server friendly
24
ES6 Harmony
25
//	
  foo.js	
  
module	
  foo	
  {	
  
	
  	
  	
  	
  export	
  var	
  x	
  =	
  42;	
  
}
//	
  bar.js	
  
import	
  foo	
  as	
  foo;	
  
console.log(foo.x);
ES6 modules, today!
Using the ES6 Module Transpiler*
26
*https://github.com/square/es6-module-transpiler
Prognosis ES6?
Finally, modules are first class in the
language!

Loaders/runtimes will implement spec
differently

sync loading possible

async loading possible
27
Prognosis ES6?
Finally, modules are first class in the
language!

Loaders/runtimes will implement spec
differently

sync loading possible

async loading possible
27
Expected release date? 201X?
Prognosis JavaScript?
28
Prognosis JavaScript?
Plagued by poor
design choices early
on
28
Prognosis JavaScript?
Plagued by poor
design choices early
on
JS used to be a toy
language
28
Prognosis JavaScript?
Plagued by poor
design choices early
on
JS used to be a toy
language
Hard, but possible to
fix past mistakes.
28
Resources
ES6 module transpiler

https://github.com/square/es6-module-transpiler

AMD module specification (draft, never ratified):

http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition

CommonJS module specification:

http://wiki.commonjs.org/wiki/Modules

UMD modules (various proposed implementations):

https://github.com/umdjs/umd

ES6 Harmony draft specification

http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts

JavaScript Module pattern

http://www.adequatelygood.com/JavaScript-Module-Pattern-In-
Depth.html

Browserify

http://browserify.org

http://superbigtree.tumblr.com/post/54873453939/introduction-to-
browserify (borrowed browserify example)
29
Questions?
Andrew Eisenberg

twitter: @werdnagreb

email:
andrew.eisenberg@tasktop.com
30
Story parts

No modules

Module pattern

Module loaders

Language modules

More Related Content

What's hot

Moving to modules
Moving to modulesMoving to modules
Moving to modulesSean Mize
 
Testing nodejs apps
Testing nodejs appsTesting nodejs apps
Testing nodejs appsfelipefsilva
 
General Assembly Workshop: Advanced JavaScript
General Assembly Workshop: Advanced JavaScriptGeneral Assembly Workshop: Advanced JavaScript
General Assembly Workshop: Advanced JavaScriptSpike Brehm
 
Lightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with BrowserifyLightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with Browserifycrgwbr
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developergicappa
 
"今" 使えるJavaScriptのトレンド
"今" 使えるJavaScriptのトレンド"今" 使えるJavaScriptのトレンド
"今" 使えるJavaScriptのトレンドHayato Mizuno
 
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume LaforgeGroovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume LaforgeGuillaume Laforge
 
The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
The Power of the JVM: Applied Polyglot Projects with Java and JavaScriptThe Power of the JVM: Applied Polyglot Projects with Java and JavaScript
The Power of the JVM: Applied Polyglot Projects with Java and JavaScriptHazelcast
 
Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications  Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications Juliana Lucena
 
WPE WebKit for Android
WPE WebKit for AndroidWPE WebKit for Android
WPE WebKit for AndroidIgalia
 
System webpack-jspm
System webpack-jspmSystem webpack-jspm
System webpack-jspmJesse Warden
 
Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...
Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...
Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...Christian Janz
 
uRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.orguRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.orgAgelos Pikoulas
 
Workshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewWorkshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewVisual Engineering
 
Boxen: How to Manage an Army of Laptops and Live to Talk About It
Boxen: How to Manage an Army of Laptops and Live to Talk About ItBoxen: How to Manage an Army of Laptops and Live to Talk About It
Boxen: How to Manage an Army of Laptops and Live to Talk About ItPuppet
 
Once upon a time, there were css, js and server-side rendering
Once upon a time, there were css, js and server-side renderingOnce upon a time, there were css, js and server-side rendering
Once upon a time, there were css, js and server-side renderingAndrea Giannantonio
 

What's hot (20)

JS Class 2016
JS Class 2016JS Class 2016
JS Class 2016
 
JS class slides (2016)
JS class slides (2016)JS class slides (2016)
JS class slides (2016)
 
Moving to modules
Moving to modulesMoving to modules
Moving to modules
 
Testing nodejs apps
Testing nodejs appsTesting nodejs apps
Testing nodejs apps
 
General Assembly Workshop: Advanced JavaScript
General Assembly Workshop: Advanced JavaScriptGeneral Assembly Workshop: Advanced JavaScript
General Assembly Workshop: Advanced JavaScript
 
Lightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with BrowserifyLightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with Browserify
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
 
"今" 使えるJavaScriptのトレンド
"今" 使えるJavaScriptのトレンド"今" 使えるJavaScriptのトレンド
"今" 使えるJavaScriptのトレンド
 
lecture5
lecture5lecture5
lecture5
 
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume LaforgeGroovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
 
The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
The Power of the JVM: Applied Polyglot Projects with Java and JavaScriptThe Power of the JVM: Applied Polyglot Projects with Java and JavaScript
The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
 
Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications  Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications
 
WPE WebKit for Android
WPE WebKit for AndroidWPE WebKit for Android
WPE WebKit for Android
 
System webpack-jspm
System webpack-jspmSystem webpack-jspm
System webpack-jspm
 
Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...
Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...
Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...
 
uRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.orguRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.org
 
Workshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewWorkshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General Overview
 
Boxen: How to Manage an Army of Laptops and Live to Talk About It
Boxen: How to Manage an Army of Laptops and Live to Talk About ItBoxen: How to Manage an Army of Laptops and Live to Talk About It
Boxen: How to Manage an Army of Laptops and Live to Talk About It
 
Mantri Presentation One
Mantri Presentation OneMantri Presentation One
Mantri Presentation One
 
Once upon a time, there were css, js and server-side rendering
Once upon a time, there were css, js and server-side renderingOnce upon a time, there were css, js and server-side rendering
Once upon a time, there were css, js and server-side rendering
 

Similar to Modular JavaScript

HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyDavid Padbury
 
AFUP Lorraine - Symfony Webpack Encore
AFUP Lorraine - Symfony Webpack EncoreAFUP Lorraine - Symfony Webpack Encore
AFUP Lorraine - Symfony Webpack EncoreEngineor
 
JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureSimon Willison
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Javascript as a target language - GWT KickOff - Part 2/2
Javascript as a target language - GWT KickOff - Part 2/2Javascript as a target language - GWT KickOff - Part 2/2
Javascript as a target language - GWT KickOff - Part 2/2JooinK
 
Webpack: your final module bundler
Webpack: your final module bundlerWebpack: your final module bundler
Webpack: your final module bundlerAndrea Giannantonio
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineRicardo Silva
 
从小书签到浏览器扩展的应用
从小书签到浏览器扩展的应用从小书签到浏览器扩展的应用
从小书签到浏览器扩展的应用Alipay
 
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...Alain Hippolyte
 
The Many Ways to Build Modular JavaScript
The Many Ways to Build Modular JavaScriptThe Many Ways to Build Modular JavaScript
The Many Ways to Build Modular JavaScriptTim Perry
 
Modular JavaScript in an OSGi World - S Mak
Modular JavaScript in an OSGi World - S MakModular JavaScript in an OSGi World - S Mak
Modular JavaScript in an OSGi World - S Makmfrancis
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller ColumnsJonathan Fine
 
Mobile Java with GWT: Still "Write Once, Run Everywhere"
Mobile Java with GWT: Still "Write Once, Run Everywhere"Mobile Java with GWT: Still "Write Once, Run Everywhere"
Mobile Java with GWT: Still "Write Once, Run Everywhere"Alex Theedom
 
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Ryan Weaver
 
JavaScript Modules Done Right
JavaScript Modules Done RightJavaScript Modules Done Right
JavaScript Modules Done RightMariusz Nowak
 
Webpack Encore Symfony Live 2017 San Francisco
Webpack Encore Symfony Live 2017 San FranciscoWebpack Encore Symfony Live 2017 San Francisco
Webpack Encore Symfony Live 2017 San FranciscoRyan Weaver
 
Web versus Native: round 1!
Web versus Native: round 1!Web versus Native: round 1!
Web versus Native: round 1!Chris Mills
 
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Ran Mizrahi
 

Similar to Modular JavaScript (20)

HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight Guy
 
AFUP Lorraine - Symfony Webpack Encore
AFUP Lorraine - Symfony Webpack EncoreAFUP Lorraine - Symfony Webpack Encore
AFUP Lorraine - Symfony Webpack Encore
 
JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big Picture
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Javascript as a target language - GWT KickOff - Part 2/2
Javascript as a target language - GWT KickOff - Part 2/2Javascript as a target language - GWT KickOff - Part 2/2
Javascript as a target language - GWT KickOff - Part 2/2
 
Webpack: your final module bundler
Webpack: your final module bundlerWebpack: your final module bundler
Webpack: your final module bundler
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
从小书签到浏览器扩展的应用
从小书签到浏览器扩展的应用从小书签到浏览器扩展的应用
从小书签到浏览器扩展的应用
 
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...
 
The Many Ways to Build Modular JavaScript
The Many Ways to Build Modular JavaScriptThe Many Ways to Build Modular JavaScript
The Many Ways to Build Modular JavaScript
 
Modular JavaScript in an OSGi World - S Mak
Modular JavaScript in an OSGi World - S MakModular JavaScript in an OSGi World - S Mak
Modular JavaScript in an OSGi World - S Mak
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
5.node js
5.node js5.node js
5.node js
 
Mobile Java with GWT: Still "Write Once, Run Everywhere"
Mobile Java with GWT: Still "Write Once, Run Everywhere"Mobile Java with GWT: Still "Write Once, Run Everywhere"
Mobile Java with GWT: Still "Write Once, Run Everywhere"
 
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
 
JavaScript Modules Done Right
JavaScript Modules Done RightJavaScript Modules Done Right
JavaScript Modules Done Right
 
Webpack Encore Symfony Live 2017 San Francisco
Webpack Encore Symfony Live 2017 San FranciscoWebpack Encore Symfony Live 2017 San Francisco
Webpack Encore Symfony Live 2017 San Francisco
 
Web versus Native: round 1!
Web versus Native: round 1!Web versus Native: round 1!
Web versus Native: round 1!
 
Jet presentation
Jet presentationJet presentation
Jet presentation
 
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)
 

Recently uploaded

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Recently uploaded (20)

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 

Modular JavaScript

  • 3. A story about JavaScript and modularization in 4 parts 2
  • 4. A story about JavaScript and modularization in 4 parts 2 No Modules (ancient history)
  • 5. A story about JavaScript and modularization in 4 parts 2 No Modules (ancient history) Module Pattern (industrial revolution)
  • 6. A story about JavaScript and modularization in 4 parts 2 No Modules (ancient history) Module Pattern (industrial revolution) Module loaders (today)
  • 7. A story about JavaScript and modularization in 4 parts 2 No Modules (ancient history) Module Pattern (industrial revolution) Module loaders (today) Harmony Modules
 (tomorrow)
  • 8. A story about JavaScript and modularization in 4 parts 2 No Modules (ancient history) Module Pattern (industrial revolution) Module loaders (today) Harmony Modules
 (tomorrow) This is really the story of a language reaching maturity
  • 9. We are Java Devs. Why should we care? JavaScript is not just blinky text and popups Massive applications being built with it on client AND server JavaScript is useful and ubiquitous TIOBE index #9 (March 2014) But JavaScript is (or was) only a toy language 3 blinky
  • 10. We are Java Devs. Why should we care? JavaScript is not just blinky text and popups Massive applications being built with it on client AND server JavaScript is useful and ubiquitous TIOBE index #9 (March 2014) But JavaScript is (or was) only a toy language 3
  • 11. But there is a core goodness 4
  • 12. But there is a core goodness 4
  • 13. No modules Part I --- Ancient History (c. 1995) 5
  • 14. The original approach Script tags All code is global 6
  • 15. The original approach Script tags All code is global 6 html      script  “foo.js”/      script  “bar.js”/   /html
  • 16. The original approach Script tags All code is global 6 html      script  “foo.js”/      script  “bar.js”/   /html //  foo.js   var  x  =  9;
  • 17. The original approach Script tags All code is global 6 html      script  “foo.js”/      script  “bar.js”/   /html //  foo.js   var  x  =  9; //  bar.js   console.log(x);
  • 18. Prognosis? Name clashes No explicit dependencies Order is important Just pray that all dependencies are available 7
  • 19. Prognosis? Name clashes No explicit dependencies Order is important Just pray that all dependencies are available 7 Ugggh!
  • 20. Module pattern Part II --- Industrial Revolution (c. 2006) 8
  • 21. Module pattern All code is still global Each script defines its own namespace Pattern has many variations 9
  • 22. Module pattern All code is still global Each script defines its own namespace Pattern has many variations 9 html      script  “foo.js”/      script  “bar.js”/   /html
  • 23. Module pattern All code is still global Each script defines its own namespace Pattern has many variations 9 html      script  “foo.js”/      script  “bar.js”/   /html //  foo.js   foo  =  foo  ||  {};   foo.x  =  9;
  • 24. Module pattern All code is still global Each script defines its own namespace Pattern has many variations 9 html      script  “foo.js”/      script  “bar.js”/   /html //  foo.js   foo  =  foo  ||  {};   foo.x  =  9; //  bar.js   (function(foo)  {    var  x  =  “Number  ”;    console.log(x  +  foo.x);   })(foo);
  • 25. Still very popular e.g., jQuery fn namespace* 10 * jQuery 1.7 introduced AMD modules
  • 26. Still very popular e.g., jQuery fn namespace* 10 (function(  $  )  {    $.fn.myPlugin=function(args){      //  awesome  plugin  stuff    };   })(  jQuery  );    $.fn.myPlugin(args); * jQuery 1.7 introduced AMD modules
  • 27. Prognosis? Name clashes, but far less common Easier to mock dependencies But… Order matters Just pray that all dependencies are available 11
  • 28. Prognosis? Name clashes, but far less common Easier to mock dependencies But… Order matters Just pray that all dependencies are available 11 Better
  • 29. Prognosis? Name clashes, but far less common Easier to mock dependencies But… Order matters Just pray that all dependencies are available 11 Better But still ugggh!
  • 30. Module loaders Part III --- Today 12
  • 31. The module explosion 13 Idea! Use the environment to selectively load modules in order
  • 32. The module explosion 13 Idea! Use the environment to selectively load modules in order Relies on a module loader outside the language specification
  • 33. The module explosion Formats CommonJS AMD 13 Idea! Use the environment to selectively load modules in order Relies on a module loader outside the language specification
  • 34. The module explosion Formats CommonJS AMD Loaders node (CJS) requireJs (AMD) curl (AMD) … 13 Idea! Use the environment to selectively load modules in order Relies on a module loader outside the language specification
  • 35. CommonJS Used by node Bad news for the web 14 var  modA  =  require(‘moduleA’),      modB  =  require(‘moduleB’);   ! export.aVal  =  modA.myVal;   export.bVal  =  modB.myVal;
  • 36. CJS is not web-friendly 15 //  main.js   if  (needSub)  {      require('./sub');   } //  sub.js   console.log(‘here’); Conditional loading of modules. (Yay!) But, that means synchronous loading Synchronous loading makes for horrendous web experience
  • 37. Asynchronous module definition (AMD) Asynchronous loading of modules Makes a happy web Syntax more cumbersome 16 define(‘myModule’,      [‘moduleA’,  ‘moduleB],        function(a,  b)  {      return  {            aVal:  a.myVal,          bVal  :  b.myVal      }   });
  • 38. Require.js: a sort of deep dive (There’s magic,
 but the good kind) 17
  • 39. Universal Module Declarations (UMD) Attempt to unify client and server side modules BUT not a standard Semantics different browser vs. server 18
  • 40. Universal Module Declarations (UMD) Attempt to unify client and server side modules BUT not a standard Semantics different browser vs. server 18 define(function(require,export,module){      var  modA  =  require(‘moduleA’),          modB  =  require(‘moduleB’);      export.aVal  =  modA.myVal;      export.bVal  =  modB.myVal;   });
  • 41. But UMD needs even
 more boilerplate 19 All sorts of UMD proposals here: https://github.com/umdjs/umd
  • 42. But UMD needs even
 more boilerplate 19 All sorts of UMD proposals here: https://github.com/umdjs/umd //  Boilerplate   if  (typeof  module  ===  'object'              typeof  define  !==  'function')  {      var  define  =  function  (factory)  {          module.exports  =  factory(require,exports,module);     };   }   //  Here  is  the  actual  module   define(function  (require,  exports,  module)  {      var  b  =  require('b');     return  function  ()  {};   });
  • 43. Browserify: no more boilerplate Pre-processor converts CJS - AMD something browser compatible Avoid AMD boilerplate Can use some node.js libs in browser 20 *http://browserify.org/
  • 44. Browserify example 21 // require the core node events module! var EventEmitter = require(‘events')! ! .EventEmitter;! ! //create a new event emitter! var emitter = new EventEmitter;! ! // set up a listener for the event! emitter.on('pizza', function(message){! console.log(message);! });! ! // emit an event! emitter.emit('pizza', ‘yummy!');
  • 45. Browserify example 21 // require the core node events module! var EventEmitter = require(‘events')! ! .EventEmitter;! ! //create a new event emitter! var emitter = new EventEmitter;! ! // set up a listener for the event! emitter.on('pizza', function(message){! console.log(message);! });! ! // emit an event! emitter.emit('pizza', ‘yummy!'); $  browserify  index.js    bundle.js
  • 47. Prognosis? No more language level name clashes Not order dependent Dependencies explicit in module Load and edit time checkers 22
  • 48. Prognosis? No more language level name clashes Not order dependent Dependencies explicit in module Load and edit time checkers 22 Yay! We did it!
  • 49. Prognosis? No more language level name clashes Not order dependent Dependencies explicit in module Load and edit time checkers 22 Yay! We did it! ...right?
  • 50. Prognosis? No more language level name clashes Not order dependent Dependencies explicit in module Load and edit time checkers BUT... Too many competing module systems modules for client and server Modules still not first class Boilerplate 22 Yay! We did it! ...right?
  • 51. Prognosis? No more language level name clashes Not order dependent Dependencies explicit in module Load and edit time checkers BUT... Too many competing module systems modules for client and server Modules still not first class Boilerplate 22 Yay! We did it! ...right? Ugggh!
  • 52. Harmony Modules Part IV --- Tomorrow 23
  • 53. Real Modules ES6 Harmony Official JavaScript module spec Sync and async friendly Browser and server friendly 24
  • 54. ES6 Harmony 25 //  foo.js   module  foo  {          export  var  x  =  42;   } //  bar.js   import  foo  as  foo;   console.log(foo.x);
  • 55. ES6 modules, today! Using the ES6 Module Transpiler* 26 *https://github.com/square/es6-module-transpiler
  • 56. Prognosis ES6? Finally, modules are first class in the language! Loaders/runtimes will implement spec differently sync loading possible async loading possible 27
  • 57. Prognosis ES6? Finally, modules are first class in the language! Loaders/runtimes will implement spec differently sync loading possible async loading possible 27 Expected release date? 201X?
  • 59. Prognosis JavaScript? Plagued by poor design choices early on 28
  • 60. Prognosis JavaScript? Plagued by poor design choices early on JS used to be a toy language 28
  • 61. Prognosis JavaScript? Plagued by poor design choices early on JS used to be a toy language Hard, but possible to fix past mistakes. 28
  • 62. Resources ES6 module transpiler https://github.com/square/es6-module-transpiler AMD module specification (draft, never ratified): http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition CommonJS module specification: http://wiki.commonjs.org/wiki/Modules UMD modules (various proposed implementations): https://github.com/umdjs/umd ES6 Harmony draft specification http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts JavaScript Module pattern http://www.adequatelygood.com/JavaScript-Module-Pattern-In- Depth.html Browserify http://browserify.org http://superbigtree.tumblr.com/post/54873453939/introduction-to- browserify (borrowed browserify example) 29
  • 63. Questions? Andrew Eisenberg twitter: @werdnagreb email: andrew.eisenberg@tasktop.com 30 Story parts No modules Module pattern Module loaders Language modules