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
304 lines (220 loc) · 14.3 KB
/
optimization.html
File metadata and controls
304 lines (220 loc) · 14.3 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
<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="#onecss">Optimizing one CSS file</a><span class="spacer boxFlex"></span><span class="sect">§ 6</span></li>
<li class="hbox"><a href="#wholeproject">Optimizing a whole project</a><span class="spacer boxFlex"></span><span class="sect">§ 7</span></li>
<li class="hbox"><a href="#hasjs">Integration with has.js</a><span class="spacer boxFlex"></span><span class="sect">§ 8</span></li>
<li class="hbox"><a href="#options">All configuration options</a><span class="spacer boxFlex"></span><span class="sect">§ 9</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. 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 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/blob/master/README.md">r.js README</a> for how to run it in Java.</p>
</div>
<div class="section">
<h2><a 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 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 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 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>
</div>
<div class="section">
<h2><a 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 name="onecss">Optimizing one CSS file</a><span class="sectionMark">§ 6</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 name="wholeproject">Optimizing a whole project</a><span class="sectionMark">§ 7</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 name="hasjs">Integration with has.js</a><span class="sectionMark">§ 8</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/blob/master/README.md">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 name="options">All configuration options</a><span class="sectionMark">§ 9</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>