forked from requirejs/requirejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimization.html
More file actions
420 lines (294 loc) · 19.7 KB
/
optimization.html
File metadata and controls
420 lines (294 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<div id="directory" class="section">
<h1>RequireJS Optimizer</h1>
<ul class="index mono">
<li class="hbox"><a href="#requirements">Requirements</a><span class="spacer boxFlex"></span><span class="sect">§ 1</span></li>
<li class="hbox"><a href="#download">Download</a><span class="spacer boxFlex"></span><span class="sect">§ 2</span></li>
<li class="hbox"><a href="#setup">Example setup</a><span class="spacer boxFlex"></span><span class="sect">§ 3</span></li>
<li class="hbox"><a href="#onejs">Optimizing one JavaScript file</a><span class="spacer boxFlex"></span><span class="sect">§ 4</span></li>
<li class="hbox"><a href="#shallow">Shallow exclusions for fast development</a><span class="spacer boxFlex"></span><span class="sect">§ 5</span></li>
<li class="hbox"><a href="#empty">empty: paths for network/CDN resources</a><span class="spacer boxFlex"></span><span class="sect">§ 6</span></li>
<li class="hbox"><a href="#onecss">Optimizing one CSS file</a><span class="spacer boxFlex"></span><span class="sect">§ 7</span></li>
<li class="hbox"><a href="#wholeproject">Optimizing a whole project</a><span class="spacer boxFlex"></span><span class="sect">§ 8</span></li>
<li class="hbox"><a href="#hasjs">Integration with has.js</a><span class="spacer boxFlex"></span><span class="sect">§ 9</span></li>
<li class="hbox"><a href="#options">All configuration options</a><span class="spacer boxFlex"></span><span class="sect">§ 10</span></li>
<li class="hbox"><a href="#pitfalls">Common pitfalls</a><span class="spacer boxFlex"></span><span class="sect">§ 11</span></li>
</ul>
<p>RequireJS has an optimization tool that does the following</p>
<ul class="serif">
<li>Combines related scripts together into build layers and minifies them via <a href="https://github.com/mishoo/UglifyJS">UglifyJS</a> (the default) or <a href="http://code.google.com/closure/compiler/">Closure Compiler</a> (an option when using Java).</li>
<li>Optimizes CSS by inlining CSS files referenced by @import and removing comments.</li>
</ul>
<p>The optimizer is part of the <a href="https://github.com/jrburke/r.js">r.js adapter for Node and Rhino</a>, 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.</p>
<p>The optimizer will only combine modules that are specified in arrays of string literals that are passed to top-level require and define calls, or the require('name') string literal calls in a <a href="whyamd.html#sugar">simplified CommonJS wrapping</a>. So, it will not find modules that are loaded via a variable name:</p>
<pre><code>var mods = someCondition ? ['a', 'b'] : ['c', 'd'];
require(mods);</code></pre>
<p>but 'a' and 'b' will be included if specified like so:</p>
<pre><code>require(['a', 'b']);</code></pre>
<p>or:</p>
<pre><code>define(['a', 'b'], function (a, b) {});</code></pre>
<p>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 <strong>include</strong> option.</p>
</div>
<div class="section">
<h2>
<a href="#requirements" name="requirements">Requirements</a>
<span class="sectionMark">§ 1</span>
</h2>
<p>The optimizer can be run using either Node or using Java with Rhino. The requirements for each option:<p>
<ul>
<li><strong>Node:</strong> (preferred) <a href="http://nodejs.org">Node</a> 0.4.0 or later.</li>
<li><strong>Java:</strong> <a href="http://java.com/">Java 1.6</a> or later.</li>
</ul>
<p>Node is the preferred execution environment. The optimizer runs <strong>much faster</strong> with Node.</p>
<p>All the example commands in this page assume Node usage, and running on a Linux/OS X command line. See the <a href="https://github.com/jrburke/r.js">r.js README</a> for how to run it in Java.</p>
</div>
<div class="section">
<h2><a href="#download" name="download">Download</a><span class="sectionMark">§ 2</span></h2>
<p>1) You can download the tool on <a href="download.html#rjs">the download page</a>.</p>
<p>2) If you are using Node with NPM, you can install r.js as part of the "requirejs" package in NPM:</p>
<pre><code>> npm install requirejs
> r.js -o app.build.js
</code></pre>
<p>That will work for a project based in the directory in which "npm install" is run. You can also install it globally via the -g option to npm:</p>
<pre><code>> npm install -g requirejs
</code></pre>
<p>If you install the requirejs npm package, you can also <a href="node.html#optimizer">use the optimizer via a function call</a> inside a node program.</p>
<p>The rest of this page assumes that r.js is just downloaded manually from the download page. It is normally the clearest, most portable way to use the optimizer.</p>
</div>
<div class="section">
<h2><a href="#setup" name="setup">Example setup</a><span class="sectionMark">§ 3</span></h2>
<p>The examples in this page will assume you downloaded and saved r.js in a directory that is a sibling to your project directory. The optimizer that is part of r.js can live anywhere you want, but you will likely need to adjust the paths accordingly in these examples.</p>
<p>Example setup:</p>
<ul>
<li>appdirectory
<ul>
<li>main.html</li>
<li>css
<ul>
<li>common.css</li>
<li>main.css</li>
</ul></li>
<li>scripts
<ul>
<li>require.js</li>
<li>main.js</li>
<li>one.js</li>
<li>two.js</li>
<li>three.js</li>
</ul></li>
</ul></li>
<li>r.js (The r.js optimizer from <a href="download.html#rjs">download page</a>)</li>
</ul>
<p>main.html has script tags for require.js and loads main.js via a require call, like so:</p>
<pre><code><!DOCTYPE html>
<html>
<head>
<title>My App</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script data-main="scripts/main" src="scripts/require.js"></script>
</head>
<body>
<h1>My App</h1>
</body>
</html>
</code></pre>
<p>main.js loads one.js, two.js and three.js via a require call:</p>
<pre><code>require(["one", "two", "three"], function (one, two, three) {
});
</code></pre>
<p>main.css has content like the following:</p>
<pre><code>@import url("common.css");
.app {
background: transparent url(../../img/app.png);
}
</code></pre>
</div>
<div class="section">
<h2>
<a href="#onejs" name="onejs">Optimizing one JavaScript file</a>
<span class="sectionMark">§ 4</span>
</h2>
<p>Use the above example setup, if you just wanted to optimize main.js, you could use this command, from inside the <strong>appdirectory/scripts</strong> directory:</p>
<pre><code>node ../../r.js -o name=main out=main-built.js baseUrl=.
</code></pre>
<p>This will create a file called <strong>appdirectory/scripts/main-built.js</strong> that will include the contents of main.js, one.js, two.js and three.js.</p>
<p>Normally you should <strong>not</strong> save optimized files with your pristine project source. Normally you would save them to a copy of your project, but to make this example easier it is saved with the source. Change the <strong>out=</strong> option to any directory you like, that has a copy of your source. Then, you can change the main-built.js file name to just main.js so the HTML page will load the optimized version of the file.</p>
<p>It is not recommended that you bundle require.js with your main.js file. Ideally require.js should be the same file contents wherever it is used. However, if you do need to include require.js with the main.js source, you can use this command:</p>
<pre><code>node ../../r.js -o baseUrl=. paths.requireLib=../../require name=main include=requireLib out=main-built.js
</code></pre>
<p>Since "require" is a reserved dependency name, you create a "requireLib" dependency and map it to the require.js file.</p>
<p>Once that optimization is done, you can then rename the main-built.js file to require.js and your optimized project will only need to make one script request.</p>
<p>If you want to wrap your built file so it can be used in pages that do not have an AMD loader like RequireJS, see the <a href="faq-optimization.html">Optimization FAQ</a>.</p>
</div>
<div class="section">
<h2><a href="#shallow" name="shallow">Shallow exclusions for fast development</a><span class="sectionMark">§ 5</span> </h2>
<p>You can use the <a href="#onejs">one JavaScript file optimization</a> approach to make your development experience faster. By optimizing all the modules in your project into one file, except the one you are currently developing, you can reload your project quickly in the browser, but still give you the option of fine grained debugging in a module.</p>
<p>You can do this by using the <strong>excludeShallow</strong> option. Using the <a href="#example">example setup</a> above, assume you are currently building out or debugging two.js. You could use this optimization command:</p>
<pre><code>node ../../r.js -o name=main excludeShallow=two out=main-built.js baseUrl=.
</code></pre>
<p>If you do not want the main-build.js file minified, pass <strong>optimize=none</strong> in the command above.</p>
<p>Then configure the HTML page to load the main-built.js file instead of main.js by configuring the path used for "main" to be "main-built":</p>
<pre><code><!DOCTYPE html>
<html>
<head>
<title>My App</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script src="scripts/require.js"></script>
<script>
require.config({
paths: {
//Comment out this line to go back to loading
//the non-optimized main.js source file.
"main": "main-built"
}
});
require(["main"]);
</script>
</head>
<body>
<h1>My App</h1>
</body>
</html>
</code></pre>
<p>Now, when this page is loaded, the require() for "main" will load the main-built.js file. Since excludeShallow told it just to exclude two.js, two.js will still be loaded as a separate file, allowing you to see it as a separate file in the browser's debugger, so you can set breakpoints and better track its individual changes.</p>
</div>
<div class="section">
<h2><a href="#empty" name="empty">empty: paths for network/CDN resources</a><span class="sectionMark">§ 6</span></h2>
<p>You may have a script you want to load from a <a href="http://en.wikipedia.org/wiki/Content_delivery_network">Content Delivery Network (CDN)</a> or any other server on a different domain.</p>
<p>The optimizer cannot load network resources, so if you want it included in the build, be sure to create a <a href="api.html#config-paths">paths config</a> to map the file to a module name. Then, for running the optimizer, download the CDN script and pass a paths config to the optimizer that maps the module name to the local file path.</p>
<p>However, it is more likely that you do not want to include that resource in the build. If the script does not have any dependencies, or you do not want to include its dependencies or will be including them in another way, then you can use the special <strong>'empty:' scheme</strong> in the paths config to just skip the file when doing an optimization.</p>
<p>In your main.js file, create a paths config that gives the script a module name. This can be done even if the script does not define a module via a call to define(). paths config are just used to map short module/script IDs to an URL. This allows you to use a a different paths config for the optimization. In main.js:</p>
<pre><code>requirejs.config({
paths: {
'jquery': 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min'
}
});
require(['jquery'], function ($) {
});
</code></pre>
<p>Then, when running the optimizer, use 'empty:' for the paths config:</p>
<pre><code>node ../../r.js -o name=main out=main-built.js baseUrl=. paths.jquery=empty:
</code></pre>
<p>Or, in a <a href="#wholeproject">build profile</a>:</p>
<pre><code>({
baseUrl: ".",
name: "main",
out: "main-built.js",
paths: {
jquery: "empty:"
}
})
</code></pre>
</div>
<div class="section">
<h2><a href="#onecss" name="onecss">Optimizing one CSS file</a><span class="sectionMark">§ 7</span></h2>
<p>Use the above example setup, if you just wanted to optimize main.css, you could use this command, from inside the <strong>appdirectory/css</strong> directory:</p>
<pre><code>node ../../r.js -o cssIn=main.css out=main-built.css
</code></pre>
<p>This will create a file called <strong>appdirectory/css/main-build.css</strong> that will include the contents of common.js, have the url() paths properly adjusted, and have comments removed.</p>
<p>See the notes for the <a href="#onejs">Optimizing one JavaScript file</a> about avoiding saving optimized files in your pristine source tree. It is only done here to make the example simpler.</p>
<span class="note">Note: The url() path fixing will always fix the paths relative to the <strong>cssIn</strong> build option path, not the <strong>out</strong> build option.</span>
</div>
<div class="section">
<h2><a href="#wholeproject" name="wholeproject">Optimizing a whole project</a><span class="sectionMark">§ 8</span></h2>
<p>The optimizer can take care of optimizing all the CSS and JS files in your project by using a build profile.</p>
<p>Create a build profile, call it app.build.js, and put it in the <strong>scripts</strong> directory. The app.build.js file can live anywhere, but just be sure to adjust the paths accordingly in the example below -- all paths will be relative to where the app.build.js is located. Example app.build.js:</p>
<pre><code>({
appDir: "../",
baseUrl: "scripts",
dir: "../../appdirectory-build",
modules: [
{
name: "main"
}
]
})
</code></pre>
<p>This build profile tells RequireJS to copy all of <strong>appdirectory</strong> to a sibling directory called <strong>appdirectory-build</strong> and apply all the optimizations in the <strong>appdirectory-build</strong> directory. It is strongly suggested you use a different output directory than the source directory -- otherwise bad things will likely happen as the optimizer overwrites your source.</p>
<p>RequireJS will use <strong>baseUrl</strong> to resolve the paths for any module names. The <strong>baseUrl</strong> should be relative to <strong>appDir</strong>.</p>
<p>In the <strong>modules</strong> array, specify the module names that you want to optimize, in the example, "main". "main" will be mapped to <strong>appdirectory/scripts/main.js</strong> in your project. The build system will then trace the dependencies for main.js and inject them into the <strong>appdirectory-build/scripts/main.js</strong> file.</p>
<p>It will also optimize any CSS files it finds inside <strong>appdirectory-build</strong>.</p>
<p>To run the build, run this command from inside the <strong>appdirectory/scripts</strong> directory:</p>
<pre><code>node ../../r.js -o app.build.js
</code></pre>
<p>Once the build is done, you can use <strong>appdirectory-build</strong> as your optimized project, ready for deployment.</p>
</div>
<div class="section">
<h2><a href="#hasjs" name="hasjs">Integration with has.js</a><span class="sectionMark">§ 9</span></h2>
<p><a href="https://github.com/phiggins42/has.js">has.js</a> is a great tool to that adds easy feature detection for your project. There is some optimizer support for optimizing code paths for has.js tests.</p>
<p>If your code uses tests like the following:</p>
<pre><code>
if (has("someThing")) {
//use native someThing
} else {
//do some workaround
}
</code></pre>
<p>You can define a <b>has</b> object in the build config with true or false values for some has() tests, and the optimizer will replace the has() test with the true or false value.</p>
<p>If your build profile looked like so:</p>
<pre><code>
({
baseUrl: ".",
name: "hasTestModule",
out: "builds/hasTestModule.js",
has: {
someThing: true
}
})
</code></pre>
<p>Then the optimizer will transform the above code sample to:</p>
<pre><code>
if (true) {
//use native someThing
} else {
//do some workaround
}
</code></pre>
<p>Then, if you use the default optimize setting of "uglify" in r.js 0.26.0 or later, or if the <b>optimize</b> setting is set to "closure" (when <a href="https://github.com/jrburke/r.js">run under Java</a>), the minifier will optimize out the dead code branch! So you can do custom builds of your code that are optimized for a set of has() tests.</p>
</div>
<div class="section">
<h2><a href="#options" name="options">All configuration options</a><span class="sectionMark">§ 10</span></h2>
<p>There is an <a href="https://github.com/jrburke/r.js/blob/master/build/example.build.js">example.build.js</a> file in the requirejs/build directory that details all of the allowed optimizer configuration options.</p>
</div>
<div class="section">
<h2><a href="#pitfalls" name="pitfalls">Common pitfalls</a><span class="sectionMark">§ 11</span></h2>
<p>If you are having trouble with the examples below, here are some common pitfalls that might be the source of the problem:</p>
<p><strong>Command line arguments are interchangeable with a build profile properties</strong></p>
<p>You can either specify options on the command line:</p>
<pre><code>node r.js -o baseUrl=. paths.jquery=some/other/jquery name=main out=main-built.js
</code></pre>
<p>or in a build profile. In a <strong>build.js</strong>, the same command line arguments can be specified like so:</p>
<pre><code>({
baseUrl: ".",
paths: {
jquery: "some/other/jquery"
}
name: "main",
out: "main-built.js"
})
</code></pre>
<p>then just pass the build profile's file name to the optimizer:</p>
<pre><code>node r.js -o build.js
</code></pre>
<p>Command line arguments take precedence over build profile settings, and you can mix them together:</p>
<pre><code>node r.js -o build.js optimize=none
</code></pre>
<p><strong name="mainConfigFile">Config settings in your main JS file are not read by default by the optimizer</strong></p>
<p>This is because the config settings for a build can be very different, with multiple optimization targets. So a separate set of config options need to be specified for the optimizer.</p>
<p>In version 1.0.5+ of the optimizer, there is now a <strong><a href="https://github.com/jrburke/r.js/blob/master/build/example.build.js#L27">mainConfigFile</a></strong> option. If specified with the path to your main JS file, the first <code>requirejs({}), requirejs.config({}), require({}), or require.config({})</code> found in that file will be parsed out and used as part of the configuration options passed to the optimizer:</p>
<pre><code>mainConfigFile: 'path/to/main.js'
</code></pre>
<p>The precedence for config: command line, build profile, mainConfigFile. In other words, the mainConfigFile configuration has the lowest priority.</p>
<p><strong>Do not specify the output directory to within the source area for your JavaScript</strong></p>
<p>For instance, if your baseUrl is 'js' and your build output goes into 'js/build', there will likely be problems with extra, nested files generated on each optimization run. This guidance is only for optimizations that are not single file optimizations.</p>
<p><strong>Avoid optimization names that are outside the baseUrl</strong></p>
<p>For instance, if your baseUrl is 'js', and your optimization targets:</p>
<pre><code>name: '../main'</code></pre>
<p>the optimization could overwrite or place files outside the output directory. For those cases, create a <strong>paths</strong> config to map that file to a local name, like:</p>
<pre><code>paths: {
main: '../main'
}
</code></pre>
<p>then use name:</p>
<pre><code>name: 'main'</code></pre>
<p>for the optimization target.</p>
</div>