0
# Command Line Interface
1
2
RequireJS r.js provides a comprehensive command line interface for building, optimizing, and running AMD-based JavaScript applications. The CLI supports multiple JavaScript environments including Node.js, Rhino, Nashorn, and xpcshell.
3
4
## Basic Usage
5
6
### Installation and Execution
7
8
```bash
9
# Install globally via npm
10
npm install -g requirejs
11
12
# Or use locally
13
npm install requirejs
14
15
# Run r.js directly (Node.js)
16
node r.js [options] [script.js]
17
18
# Use global installation
19
r.js [options] [script.js]
20
```
21
22
### Command Structure
23
24
```bash { .api }
25
r.js [command-option] [arguments]
26
```
27
28
**Command Options:**
29
- `-o` - Run optimizer/build
30
- `-v` - Display version information
31
- `-convert` - Convert CommonJS modules to AMD
32
- `-lib` - Load bundled libraries
33
34
## Core Commands
35
36
### Running Scripts
37
38
Execute AMD-based JavaScript applications:
39
40
```bash { .api }
41
# Run main application script
42
r.js main.js
43
44
# Run with specific module as entry point
45
r.js app/startup.js
46
47
# Run script in current directory (defaults to main.js if no file specified)
48
r.js
49
```
50
51
### Version Information
52
53
Display RequireJS version and environment details:
54
55
```bash { .api }
56
# Show version information
57
r.js -v
58
59
# Sample output:
60
# r.js: 2.3.7, RequireJS: 2.3.7, Env: node, Command line args: -v
61
```
62
63
## Build and Optimization
64
65
### Basic Build Command
66
67
```bash { .api }
68
# Build using configuration file
69
r.js -o build.js
70
71
# Build using configuration object
72
r.js -o baseUrl=src name=main out=dist/main-built.js
73
```
74
75
### Build Configuration Options
76
77
Pass build options directly via command line:
78
79
```bash { .api }
80
# Single module optimization
81
r.js -o name=main baseUrl=js out=built/main.js
82
83
# Multi-option build
84
r.js -o appDir=src dir=dist baseUrl=. optimize=uglify
85
86
# Specify configuration file
87
r.js -o build/config.build.js
88
89
# Multiple configuration sources
90
r.js -o build.js baseUrl=override optimize=none
91
```
92
93
### Advanced Build Options
94
95
```bash { .api }
96
# CSS optimization
97
r.js -o cssIn=css/main.css out=css/main-built.css optimizeCss=standard
98
99
# Source map generation
100
r.js -o build.js generateSourceMaps=true optimize=uglify2
101
102
# Skip directory optimization
103
r.js -o build.js skipDirOptimize=true
104
105
# Preserve license comments
106
r.js -o build.js preserveLicenseComments=true
107
```
108
109
## Module Conversion
110
111
### CommonJS to AMD Conversion
112
113
Convert CommonJS modules to AMD format:
114
115
```bash { .api }
116
# Convert single directory
117
r.js -convert path/to/commonjs/modules path/to/amd/modules
118
119
# Convert with specific options
120
r.js -convert src/commonjs dist/amd
121
```
122
123
**Conversion Process:**
124
- Wraps CommonJS modules in AMD `define()` calls
125
- Converts `require()` calls to dependency arrays
126
- Handles `module.exports` and `exports` assignments
127
- Maintains file structure and names
128
129
### Conversion Examples
130
131
```bash
132
# Input CommonJS module (math.js):
133
var helper = require('./helper');
134
module.exports = {
135
add: function(a, b) { return a + b; }
136
};
137
138
# After conversion:
139
define(['./helper'], function(helper) {
140
return {
141
add: function(a, b) { return a + b; }
142
};
143
});
144
```
145
146
## Library Loading
147
148
### Bundled Library Usage
149
150
Load and use RequireJS with bundled libraries:
151
152
```bash { .api }
153
# Load with library support
154
r.js -lib script.js
155
156
# Available in script.js:
157
# - requirejs/require/define functions
158
# - Bundled utility libraries
159
# - Environment abstraction
160
```
161
162
## Environment-Specific Usage
163
164
### Node.js Environment
165
166
```bash { .api }
167
# Standard Node.js execution
168
node r.js -o build.js
169
170
# Use specific Node.js options
171
node --max-old-space-size=4096 r.js -o large-build.js
172
173
# Environment variable configuration
174
NODE_ENV=production node r.js -o build.js
175
```
176
177
### Java/Rhino Environment
178
179
```bash { .api }
180
# Rhino execution with classpath
181
java -classpath rhino.jar:. org.mozilla.javascript.tools.shell.Main r.js -o build.js
182
183
# Nashorn execution (Java 8+)
184
jjs r.js -- -o build.js
185
186
# With Closure Compiler
187
java -Xmx1g -classpath rhino.jar:closure-compiler.jar org.mozilla.javascript.tools.shell.Main r.js -o build.js optimize=closure
188
```
189
190
### xpcshell Environment (Mozilla)
191
192
```bash { .api }
193
# Firefox extension development
194
xpcshell r.js -o addon-build.js
195
196
# With Mozilla-specific options
197
xpcshell -f r.js -e "load('build-script.js')"
198
```
199
200
## Configuration File Formats
201
202
### JavaScript Configuration
203
204
```javascript { .api }
205
// build.js
206
({
207
appDir: "src/",
208
baseUrl: "./",
209
dir: "dist/",
210
modules: [
211
{ name: "main" }
212
],
213
optimize: "uglify"
214
})
215
```
216
217
```bash
218
r.js -o build.js
219
```
220
221
### JSON Configuration
222
223
```json
224
{
225
"appDir": "src/",
226
"baseUrl": "./",
227
"dir": "dist/",
228
"modules": [
229
{ "name": "main" }
230
],
231
"optimize": "uglify"
232
}
233
```
234
235
```bash { .api }
236
r.js -o build.json
237
```
238
239
### Command Line Configuration
240
241
```bash { .api }
242
# Inline configuration (single line)
243
r.js -o "({appDir:'src',dir:'dist',modules:[{name:'main'}]})"
244
245
# Mixed file and inline options
246
r.js -o build.js optimize=none generateSourceMaps=true
247
```
248
249
## Output and Logging
250
251
### Standard Output Control
252
253
```bash { .api }
254
# Write optimized output to stdout
255
r.js -o build.js out=stdout
256
257
# Combine with other tools
258
r.js -o name=main baseUrl=src out=stdout | gzip > main.js.gz
259
260
# Silent operation
261
r.js -o build.js logLevel=4
262
```
263
264
### Log Levels
265
266
```bash { .api }
267
# Trace level (most verbose)
268
r.js -o build.js logLevel=0
269
270
# Info level (default)
271
r.js -o build.js logLevel=1
272
273
# Warning level
274
r.js -o build.js logLevel=2
275
276
# Error level
277
r.js -o build.js logLevel=3
278
279
# Silent (no output)
280
r.js -o build.js logLevel=4
281
```
282
283
## Error Handling and Debugging
284
285
### Common Error Scenarios
286
287
```bash
288
# Missing build file
289
r.js -o nonexistent.js
290
# Error: ENOENT: no such file or directory
291
292
# Invalid configuration
293
r.js -o "({invalid:syntax})"
294
# Error: SyntaxError: Unexpected token
295
296
# Module not found
297
r.js -o name=missing baseUrl=src out=dist/missing.js
298
# Error: Module name "missing" has not been loaded yet
299
```
300
301
### Debugging Options
302
303
```bash { .api }
304
# Verbose output with trace information
305
r.js -o build.js logLevel=0
306
307
# Preserve intermediate files for debugging
308
r.js -o build.js keepBuildDir=true
309
310
# Generate source maps for debugging minified code
311
r.js -o build.js generateSourceMaps=true
312
313
# Skip minification for debugging
314
r.js -o build.js optimize=none
315
```
316
317
## Build Performance Options
318
319
### Optimization Control
320
321
```bash { .api }
322
# Fast development builds
323
r.js -o build.js optimize=none skipDirOptimize=true
324
325
# Maximum optimization
326
r.js -o build.js optimize=uglify generateSourceMaps=false removeCombined=true
327
328
# Memory usage control (Node.js)
329
node --max-old-space-size=8192 r.js -o large-build.js
330
```
331
332
### Incremental Builds
333
334
```bash { .api }
335
# Keep previous build directory for faster rebuilds
336
r.js -o build.js keepBuildDir=true
337
338
# Only rebuild changed modules
339
r.js -o build.js skipDirOptimize=true
340
```
341
342
## Integration Examples
343
344
### Build Scripts
345
346
```bash
347
#!/bin/bash
348
# build.sh
349
350
# Development build
351
echo "Building development version..."
352
r.js -o build-dev.js
353
354
# Production build
355
echo "Building production version..."
356
r.js -o build-prod.js
357
358
# Create source maps
359
echo "Generating source maps..."
360
r.js -o build-prod.js generateSourceMaps=true
361
```
362
363
### Package.json Scripts
364
365
```json
366
{
367
"scripts": {
368
"build": "r.js -o build.js",
369
"build:dev": "r.js -o build-dev.js",
370
"build:prod": "r.js -o build-prod.js",
371
"watch": "nodemon --exec 'npm run build:dev' --watch src/",
372
"convert": "r.js -convert src/commonjs dist/amd"
373
}
374
}
375
```
376
377
### Continuous Integration
378
379
```bash
380
# CI build script
381
set -e # Exit on any error
382
383
echo "Converting CommonJS modules..."
384
r.js -convert lib/commonjs lib/amd
385
386
echo "Running build optimization..."
387
r.js -o build-ci.js
388
389
echo "Build completed successfully"
390
```
391
392
## Help and Documentation
393
394
### Getting Help
395
396
```bash { .api }
397
# Show help information
398
r.js -help
399
r.js --help
400
r.js -h
401
402
# Online documentation reference
403
# See https://github.com/requirejs/r.js for usage.
404
```
405
406
### Command Line Reference
407
408
The r.js command line tool provides a flexible interface for all RequireJS optimization and execution needs, supporting multiple JavaScript environments and extensive configuration options for modern web application builds.