Slide 1

Slide 1 text

Understanding
 JavaScript Modules Ahmad Alfy

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

JavaScript modules ecosystem may be intimidating,
 but understanding it is vital for web developers. Bundlers Loaders Browserify Webpack CommonJS AMD Import Export

Slide 4

Slide 4 text

• Self contained piece of code. • Has a very specific functionality. • Can be added or removed from a system as needed. • Can be injected/required by other modules. What is a Module?

Slide 5

Slide 5 text

1. Maintainability: because a module is self-contained, functionalities can be added/removed from it as it grow. Splitting your code into modules allows you to maintain it without growing complexity. 2. Reusability: Splitting your code into modules allows you to reuse it in the same project or different projects. 3. Name spacing: Using modules reduce the pollution of global environment in JavaScript Why we need modules?

Slide 6

Slide 6 text

• Before ES6, JavaScript didn’t have native modules. We had to come up with patterns to mimic the modular behavior. • Introduction of modules in JavaScript doesn’t mean we have to deprecate the methods that was being used. Eventually you will have to support a project or deal with a third party code that might not be using classes. How can we use modules in JavaScript

Slide 7

Slide 7 text

Patterns Object literal

Slide 8

Slide 8 text

Patterns Revealing module

Slide 9

Slide 9 text

• The previous approaches used a single global variable to wrap its code and hence creating a private namespace for itself. • It doesn’t address dependency management, meaning which script/module depends upon. • It can lead to namespace collisions like the probability of having two modules can have the same name.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

• It’s a volunteering working group started by Mozilla Engineer Kevin Dangoor in 2009 and it is not affiliated with ECMA group and TC39. • Originally started to introduce APIs that will facilitate working with JavaScript outside the browser like servers, command line tools, desktop apps. • One of the features introduced in CommonJS was the Modules. It’s essentially a reusable piece of JavaScript which exports specific objects, making them available for other modules to require.

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

• No more pollution for the name space. • Dependencies are explicitly declared.

Slide 15

Slide 15 text

• CommonJS approach is awesome when you are reading dependencies from files in the same disk because it works synchronously. It wasn’t suitable for the web. • The reason is because loading too many modules will block the main thread and halt the browser until all the dependencies are fetched and parsed.

Slide 16

Slide 16 text

×

Slide 17

Slide 17 text

AMD API specifies a mechanism for defining modules such that the module and its dependencies can be asynchronously loaded. This is particularly well suited for the browser environment where synchronous loading of modules incurs performance, usability, debugging, and cross-domain access problems. AMD Asynchronous Module Definition https://github.com/amdjs/amdjs-api/blob/master/AMD.md

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

A common implementation of AMD is Require.js
 http://requirejs.org/docs/whyamd.html

Slide 21

Slide 21 text

UMD tries to let the authors use both CommonJS and AMD typed modules. As a result, UMD modules are capable of working on both client and server. UMD Universal Module Definition https://github.com/umdjs/umd/blob/master/README.md

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Multiple exports per module

Slide 26

Slide 26 text

Single export per module

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

https://paulirish.github.io/es-modules-todomvc/

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Bundling Bundling is the process of grouping modules (and their dependencies) into a single file in the correct order.

Slide 35

Slide 35 text

Why? • Reduce the number of requests. • Allows minifiers/uglifiers to work more efficiently. • Browsers support isn’t ideal *yet*.

Slide 36

Slide 36 text

How? • We define a single or multiple entry points to the bundler • The bundler scan the dependencies and draw a dependency graph. • The bundler start to group all these files together in a single file (or multiple).

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Some benefits we get from the bundlers Code splitting Hot Module Replacement Tree shaking Splitting code into various bundles which can then be loaded on demand or in parallel. • Multiple Entry Points: Manually split code using entry configuration. • Prevent Duplication. • Dynamic Imports with Loading on demand (lazy loading)

Slide 41

Slide 41 text

To Bundle • Ensure your application works across all the browsers • Serving a single file or multiple ones according to 
 your configuration and preferences. • Bundlers will minify and uglify your output. • Get the benefits of some of the bundlers like • Tree shaking • Code splitting • Hot module replacement

Slide 42

Slide 42 text

To Bundle Not • Eliminate middleware and reduce the dependencies you need to ship your application (debugging, configuring, DX). • Modern browsers run ES6 faster than the compiled code. • Harnessing the power of multiplexing in HTTP2, serving multiple small files will work faster than serving a single file or even multiple bundled files. • Better management for browser caching as only the updated files will be invalidated, not a whole bunch of bundled files.

Slide 43

Slide 43 text

Further resources • Discussion on GitHub: Can you help me understand the benefit of require.js
 https://gist.github.com/desandro/4686136 • JavaScript Modules: A Beginner’s Guide
 https://medium.freecodecamp.org/javascript-modules-a-beginner-s-guide-783f7d7a5fcc • JavaScript Modules Part 2: Module Bundling
 https://medium.freecodecamp.org/javascript-modules-part-2-module-bundling-5020383cf306 • The state of JavaScript modules
 https://medium.com/webpack/the-state-of-javascript-modules-4636d1774358 • TodoMVC with native ES6 modules
 https://paulirish.github.io/es-modules-todomvc/ • Webpack - The confusing parts
 https://medium.com/@rajaraodv/webpack-the-confusing-parts-58712f8fcad9

Slide 44

Slide 44 text

No content