Skip to content

Commit 6f50598

Browse files
committed
Support for replacing has() tests with true or false in optimizer.
1 parent 3c4e457 commit 6f50598

5 files changed

Lines changed: 99 additions & 2 deletions

File tree

build/jslib/pragma.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var pragma = {
1313
conditionalRegExp: /(exclude|include)Start\s*\(\s*["'](\w+)["']\s*,(.*)\)/,
1414
useStrictRegExp: /['"]use strict['"];/g,
15+
hasRegExp: /has\s*\(\s*['"]([^'"]+)['"]\)/g,
1516

1617
removeStrict: function (contents, config) {
1718
return config.useStrict ? contents : contents.replace(pragma.useStrictRegExp, '');
@@ -29,6 +30,16 @@ var pragma = {
2930
//when dojo no longer needs conversion:
3031
kwArgs = pragmas;
3132

33+
//Replace has references if desired
34+
if (config.has) {
35+
fileContents = fileContents.replace(pragma.hasRegExp, function (match, test) {
36+
if (test in config.has) {
37+
return !!config.has[test];
38+
}
39+
return match;
40+
});
41+
}
42+
3243
//If pragma work is not desired, skip it.
3344
if (config.skipPragmas) {
3445
return pragma.removeStrict(fileContents, config);

build/tests/hasTestModule.build.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
({
2+
baseUrl: "./",
3+
optimize: "none",
4+
name: "hasTestModule",
5+
out: "builds/hasTestModule.js",
6+
has: {
7+
aTrueValue: true,
8+
aFalseValue: false
9+
}
10+
})

build/tests/hasTestModule.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Just a test module that tests has replacements. Not usable on its own.
3+
*/
4+
define(function (require) {
5+
var foo, bar, baz, blurp, bat, blip;
6+
7+
if (has("aTrueValue")) {
8+
foo = "is true";
9+
} else {
10+
foo = "is false";
11+
}
12+
13+
if (has("aFalseValue")) {
14+
bar = "is true";
15+
} else {
16+
bar = "is false";
17+
}
18+
19+
if (has("some skipped value")) {
20+
baz = "what";
21+
} else {
22+
baz = "ever";
23+
}
24+
25+
blurp = has("aTrueValue") ? "OK" : "FAIL";
26+
bat = has ('aFalseValue') ? "FAIL" : "OK";
27+
blip = has("some skipped value") ? "what" : "ever";
28+
29+
return foo + bar + baz + blurp + bat + blip;
30+
});

docs/optimization.html

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ <h1>RequireJS Optimization Tool</h1>
99
<li class="hbox"><a href="#shallow">Shallow exclusions for fast development</a><span class="spacer boxFlex"></span><span class="sect">&sect; 5</span></li>
1010
<li class="hbox"><a href="#onecss">Optimizing one CSS file</a><span class="spacer boxFlex"></span><span class="sect">&sect; 6</span></li>
1111
<li class="hbox"><a href="#wholeproject">Optimizing a whole project</a><span class="spacer boxFlex"></span><span class="sect">&sect; 7</span></li>
12-
<li class="hbox"><a href="#options">All configuration options</a><span class="spacer boxFlex"></span><span class="sect">&sect; 8</span></li>
12+
<li class="hbox"><a href="#hasjs">Integration with has.js</a><span class="spacer boxFlex"></span><span class="sect">&sect; 8</span></li>
13+
<li class="hbox"><a href="#options">All configuration options</a><span class="spacer boxFlex"></span><span class="sect">&sect; 9</span></li>
1314
</ul>
1415

1516
<span class="note">Note: RequireJS comes with an optimization tool that does the following</span>
@@ -223,7 +224,51 @@ <h2><a name="wholeproject">Optimizing a whole project</a><span class="sectionMar
223224
</div>
224225

225226
<div class="section">
226-
<h2><a name="options">All configuration options</a><span class="sectionMark">&sect; 8</span></h2>
227+
<h2><a name="hasjs">Integration with has.js</a><span class="sectionMark">&sect; 8</span></h2>
228+
229+
<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>
230+
231+
<p>If your code uses tests like the following:</p>
232+
233+
<pre><code>
234+
if (has("someThing")) {
235+
//use native someThing
236+
} else {
237+
//do some workaround
238+
}
239+
</code></pre>
240+
241+
<p>You can define a <b>has</b> object in the build config with true or false values for some has() tests, and the optmizer will replace the has() test with the true or false value.</p>
242+
243+
<p>If your build profile looked like so:</p>
244+
245+
<pre><code>
246+
({
247+
baseUrl: "./",
248+
name: "hasTestModule",
249+
out: "builds/hasTestModule.js",
250+
has: {
251+
someThing: true
252+
}
253+
})
254+
</code></pre>
255+
256+
<p>Then the optimizer will transform the above code sample to:</p>
257+
258+
<pre><code>
259+
if (true) {
260+
//use native someThing
261+
} else {
262+
//do some workaround
263+
}
264+
</code></pre>
265+
266+
<p>Then, if you use the default <b>optimize</b> setting that uses Closure Compiler, Closure Compiler 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>
267+
268+
</div>
269+
270+
<div class="section">
271+
<h2><a name="options">All configuration options</a><span class="sectionMark">&sect; 9</span></h2>
227272

228273
<p>There is an <a href="http://github.com/jrburke/requirejs/blob/master/build/example.build.js">example.build.js</a> file in the requirejs/build directory that details all of the allowed optimization tool configuration options.</p>
229274
</div>

tests/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Try manual testing:
3535
* ../build.sh indexBuilder.build.js - confirm plugin write calls are done.
3636
* ../build.sh text.build.js - confirm plugin write calls are done.
3737
* ../build.sh i18n.build.js - confirm plugin required the nls/en-us-surfer/colors module.
38+
* ../build.sh hasTestModule.build.js - confirm plugin required the nls/en-us-surfer/colors module.
3839

3940
# Sample jQuery project
4041

0 commit comments

Comments
 (0)