RequireJS

A JavaScript Module Loader

Greg Franko


1/30/2014

Use arrow keys to navigate

About The Speaker



Greg Franko

JavaScript Engineer at AddThis

Open Source Developer

Author

Speaker

Why Do People Use RequireJS?

Performance

Asynchronous and Dynamic Loading

Maintainability

Decoupled Code into Modules

Workflow

No Build Step Required

What Projects/Companies Use RequireJS?

jQuery

Used in Development, Removed In Production

SoundCloud

RequireJS in Development, AlmondJS In Production

PayPal

Used In Both Development and Production

BBC News

Used In Both Development and Production

AddThis

RequireJS in Development, AMDCleanJS In Production

Are You Using RequireJS?

It's Okay If You Aren't

RequireJS Basics

Let's Learn

Most Projects Start Like This:

JavaScript files are included as HTML script tags


<body>
<script src='jquery.js'></script>
<script src='jquery-ui.js'></script>
<script src='lodash.js'></script>
<script src='backbone.js'></script>
<script src='init.js'></script>
<script src='app.js'></script>
</body>

This Is Fine...

This Is Better:

Only Require.js is included as an HTML script tag


<body>
<script src='require.js' data-main='init'></script>
</body>

data-main - Tells Require.js to load init.js first

Huh?


init.js is getting loaded, but none of my other JavaScript files are

RequireJS is the worst

Everything Will Be Okay!


You haven't told RequireJS that init.js depends on jQuery, jQueryUI, Lodash, and Backbone

How Do I Tell RequireJS About My File Dependencies?


What Format?


init.js AMD Example

// init.js
// Depends on jQuery, jQueryUI, Lodash, and Backbone
define(['jquery', 'jqueryui', 'lodash', 'backbone'], function($, null, _, Backbone) {
  /* This function gets called after
    jquery.js, jqueryui.js, lodash.js,
    and backbone.js are loaded
  */
});

Awesome, It Works!


RequireJS Is The Best

Please Tell Me More About AMD

AMD


define(id?, dependencies?, factory)

If define Is Confusing You...

define(id?, dependencies?, factory)

If define Is Still Confusing...

Is That It?


require(Array, Function)

Should I Use define or require?


Now What?


Third Party Libraries


How Would A Third Party Library Provide AMD Support?


Third Party Examples

jQuery


Provides AMD Support via a Named AMD module

Lodash


Provides AMD Support via an Anonymous AMD module

Backbone


Does Not Provide AMD Support yet

How Do You Use Scripts That Are Not AMD Compatible?


Shim Configuration


Useful for libraries that export a global variable

requirejs.config({
  shim: {
    'backbone': {
        deps: ['underscore', 'jquery'],
        exports: 'Backbone'
    }
  }
});

Is This Black Magic?


Shim Configuration


Also Useful for Declaring Dependencies

require.config({
  shim: {
    'jquery.ui.widget': ['jquery'],
    
    'jquery.selectBoxIt': ['jquery.ui.widget']
  }
});

Hold Up


RequireJS Config


Popular Config Options


Let's Review

Building with The RequireJS Optimizer

The RequireJS Optimizer


Example Build Profile

{
  baseUrl: "public/js/",
  paths: {
    "desktop": "app/config/Init"
  },
  optimize: "uglify",
  mainConfigFile: "public/js/app/config/Init.js",
  include: ["desktop"],
  out: "public/js/app/config/Init.min.js"
}

Common Optimizer Questions

Almond.js

AMDClean.js

For A Complete Example, Check Out:


Backbone-Require-Boilerplate

Additional Topics That May Interest You


Browserify vs RequireJS

Using CommonJS syntax with RequireJS

ES6 Modules and The Future

Thanks!


Twitter: GregFranko

Github: gfranko