r.js is a command-line tool to run AMD-based JavaScript with RequireJS and to optimize and bundle modules for browsers. It works with the RequireJS AMD implementation.
Overview
r.js is a command-line utility for JavaScript projects that use the Asynchronous Module Definition API to declare and load modules. It belongs to the RequireJS project and runs AMD scripts together with the RequireJS loader implementation.
One role is executing AMD application entry points, for example running main.js under Node with npm install -g requirejs, or under Nashorn, Rhino, or xpcshell with the documented java or shell invocations. It can load npm-installed Node modules when run in Node.
The other role is the optimizer invoked with -o and a build config file. During optimization it runs require.js so it traces module dependencies and inlines them, which matters for anonymous modules that break under naive concatenation. Rhino setups can pair with Closure Compiler jars for minification.
Additional commands include printing combined r.js and require.js versions and converting directories of CommonJS modules to define()-wrapped AMD modules, though some dynamic require patterns and circular dependencies may fail. The optimizer outperforms plain concatenation because it understands the AMD dependency graph.
Key features
- Run AMD entry points from CLI
- Module bundling via optimizer
- Dependency tracing for anonymous modules
- CommonJS to define() conversion
- Node, Rhino, Nashorn, xpcshell hosts
- Closure Compiler integration option
- Version and convert subcommands
Use cases
- Building RequireJS apps for production
- Running AMD main.js under Node
- Migrating CommonJS modules to AMD
- Java-based JS build pipelines with Rhino