Slide 1

Slide 1 text

JavaScript Modules John Novatnack / @jnovatnack @SquareNY New York Front End Coders Meetup 3/26/2014

Slide 2

Slide 2 text

JavaScript Without Modules ● Code in the global namespace ● Poor code organization ● Difficult to share code between client and server ● Painful to use third-party libraries

Slide 3

Slide 3 text

Existing Module Pattern var calendar = (function() { return { daysOfWeekUS : function() { return [ "Sunday" // ... "Saturday" ]}; }; })();

Slide 4

Slide 4 text

Existing Module Pattern var i18n = (function(calendar) { return { Calendar : function() { this.daysOfWeek = function(countryCode) { if (countryCode === 'US') { return calendar.daysOfWeekUS(); } }; }}; })(calendar);

Slide 5

Slide 5 text

Three JavaScript API Modules ● CommonJS ● Asynchronous Module Definition (AMD) ● ECMAScript 6 Harmony

Slide 6

Slide 6 text

CommonJS ● CommonJS - Goal is to standardize JavaScript APIs to improve web, desktop, command line interoperability ● Node.JS is an implementation of CommonJS modules for server side

Slide 7

Slide 7 text

Declaring CommonJS Modules function daysOfWeekUS() { // ... }; exports.daysOfWeekUS = daysOfWeekUS;

Slide 8

Slide 8 text

Importing CommonJS Modules var calendar = require("./calendar"); function Calendar() { this.daysOfWeek = function(countryCode) { if (countryCode === 'US') { return calendar.daysOfWeekUS(); } }}; exports.Calendar = Calendar;

Slide 9

Slide 9 text

Browserify ● Browserify bundles CommonJS modules for shipping to the browser $ browserify calendar.js i18n.js -o bundle.js

Slide 10

Slide 10 text

Node Libraries and Browserify ● In addition to your own modules, browserify will bundle browser compatible node modules

Slide 11

Slide 11 text

Importing Node Library $ npm install console-browserify var calendar = require("./calendar"); var log = require("console-browserify"); function Calendar() { this.daysOfWeek = function(countryCode) { log.debug('daysOfWeek for countryCode: ', countryCode); // ... }}; exports.Calendar = Calendar;

Slide 12

Slide 12 text

Asynchronous Module Definition (AMD) ● CommonJS modules have been criticized for not being ideal for browsers ● AMD modules focus on browser modules ○ Especially asynchronous loading

Slide 13

Slide 13 text

Defining AMD Modules define([], function() { return { daysOfWeekUS : function() { return [ "Sunday", // ... "Saturday" ]; }}; });

Slide 14

Slide 14 text

Importing AMD Modules define(["calendar"], function(calendar) { return { Calendar : function() { this.daysOfWeek = function(countryCode) { if (countryCode === 'US') { return calendar.daysOfWeekUS(); }}; }}; });

Slide 15

Slide 15 text

RequireJS and AMD Modules ● RequireJS is a popular AMD module loader ○ Modules are loaded by the browser asynchronously as needed

Slide 16

Slide 16 text

RequireJS Loading Example window.displayAvailabilityCalendar = function() { require(["i18n"], function(i18n) { alert("Displaying calendar with day ordering " + new i18n.Calendar().daysOfWeek("US")); }); } Dependencies loaded lazily Module executed after dependencies are loaded

Slide 17

Slide 17 text

ECMAScript 6 (ES 6) Modules ● ECMAScript 6 Harmony next version of JavaScript specification ● Large number of language changes including classes, new iterators, collections library and modules

Slide 18

Slide 18 text

Declaring ES6 Modules function daysOfWeekUS() { return [ "Sunday", // ... "Saturday", ]; } export { daysOfWeekUS };

Slide 19

Slide 19 text

Importing ES6 Modules import { daysOfWeekUS } from "./calendar"; function Calendar() { this.daysOfWeek = function(countryCode) { if (countryCode === 'US') { return daysOfWeekUS(); } }}; export { Calendar };

Slide 20

Slide 20 text

Additional ES6 Export Syntax // Export functions, vars and ES6 classes export function foo() { }; export var x = 10; export class Foo { }; // Export a default for the module export default daysOfWeekUS;

Slide 21

Slide 21 text

Additional ES6 Import Syntax // Import the default export import dow from "./calendar"; // Rename an import import { daysOfWeekUS as dow } from "./calendar"; // Set module to an object module calendar from "./calendar";

Slide 22

Slide 22 text

Use ES6 Modules Today ● ES6 module transpiler is a project started by Square that maps ES6 modules to AMD, CommonJS, or global modules ● Google Traceur is a general ES6 compiler that also supports modules

Slide 23

Slide 23 text

ES6 Module Transpiler Example $ npm install -g es6-module-transpiler $ compile-modules i18n.js calendar.js \ --to=build/ \ --type='cjs'

Slide 24

Slide 24 text

Thanks! Source, Node & Grunt configs @ https://github.com/jnovatnack/js-modules-talk

Slide 25

Slide 25 text

Learning More ● Writing Modular JavaScript With AMD, CommonJS & ES Harmony by Addy Osmani