RequireJS Optimizer

RequireJS has an optimization tool that does the following

The optimizer is part of the r.js adapter for Node and Rhino, and it is designed to be run as part of a build or packaging step after you are done with development and are ready to deploy the code for your users.

The optimizer will only combine modules that are specified in arrays of string literals that are passed to top-level require and define calls. So, it will not find modules that are loaded via a variable name:

var mods = someCondition ? ['a', 'b'], ['c', 'd'];
require(mods);

but 'a' and 'b' will be included if specified like so:

require(['a', 'b']);

or:

define(['a', 'b'], function (a, b) {});

This behavior allows dynamic loading of modules even after optimization. You can always explicitly add modules that are not found via the optimizer's static analysis by using the include option.

Requirements § 1

The optimizer can be run using either Node or using Java with Rhino. The requirements for each option:

Node is the preferred execution environment. The optimizer runs much faster with Node.

All the example commands in this page assume Node usage, and running on a Linux/OS X command line. See the r.js README for how to run it in Java.

Download§ 2

1) You can download the tool on the download page.

2) If you are using Node with NPM, you can install r.js as part of the "requirejs" package in NPM:

> npm install requirejs
> r.js -o app.build.js

That will work for a project based in in the directory in which "npm install" is run. You can also install it globally via the -g option to npm:

> npm install -g requirejs

If you install the requirejs npm package, you can also use the optimizer via a function call inside a node program.

The rest of this page assumes that r.js is just downloaded manually from the download page. It is