Skip to content

Commit 917d3f0

Browse files
committed
Adding faq, cleaning up some links, and starting new parse task in build to find require.plugin definition for proper injection of plugins in a layer.
1 parent eb5e92e commit 917d3f0

11 files changed

Lines changed: 202 additions & 27 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ The unit tests work in IE 6+, Firefox 3.0+, Safari 4+, Chrome 3+, and Opera 10+.
1616

1717
Latest Release: [0.8.0](http://requirejs.org/docs/download.html)
1818

19-
* [Start](docs/start.md)
20-
* [Using it with jQuery](docs/jquery.md)
21-
* [Download](docs/download.md)
22-
* [API](docs/api.md)
23-
* [Optimization](docs/optimization.md)
24-
* [Why](docs/why.md)
25-
* [Requirements](docs/requirements.md)
26-
* [History](docs/history.md)
19+
* [Start](tree/master/docs/docs/start.md)
20+
* [Using it with jQuery](tree/master/docs/docs/jquery.md)
21+
* [Download](tree/master/docs/docs/download.md)
22+
* [API](tree/master/docs/docs/api.md)
23+
* [Optimization](tree/master/docs/docs/optimization.md)
24+
* [Why](tree/master/docs/docs/why.md)
25+
* [Requirements](tree/master/docs/docs/requirements.md)
26+
* [History](tree/master/docs/docs/history.md)
2727
* [Get involved or ask questions](http://groups.google.com/group/requirejs)
2828
* [Blog](http://tagneto.blogspot.com)

build/jslib/parse.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var parse;
2929
STRING = 40,
3030
ARRAYLIT = 63,
3131
OBJECTLIT = 64,
32+
ASSIGN = 86,
3233
EXPR_RESULT = 130,
3334

3435
//Oh Java, you rascal.
@@ -102,6 +103,67 @@ var parse;
102103
return result;
103104
};
104105

106+
107+
/**
108+
* Determines if the file defines require.plugin().
109+
* @param {String} fileName
110+
* @param {String} fileContents
111+
* @returns {Boolean}
112+
*/
113+
parse.definesRequirePlugin = function (fileName, fileContents) {
114+
var i, node, result = null, parsed,
115+
jsSourceFile = closurefromCode(String(fileName), String(fileContents)),
116+
astRoot = compiler.parse(jsSourceFile);
117+
118+
return parse.nodeHasRequirePlugin(astRoot);
119+
};
120+
121+
/**
122+
* Determines if a given node contains a require.plugin() definition.
123+
* @param {Packages.com.google.javascript.rhino.Node} node
124+
* @returns {Boolean}
125+
*/
126+
parse.nodeHasRequirePlugin = function (node) {
127+
if (parse.isRequirePluginNode(node)) {
128+
return true;
129+
}
130+
131+
for (var i = 0, n; (n = node.getChildAtIndex(i)); i++) {
132+
if (parse.nodeHasRequirePlugin(n)) {
133+
return true;
134+
}
135+
}
136+
137+
return false;
138+
};
139+
140+
/**
141+
* Is the given node the actual definition of require()
142+
* @param {Packages.com.google.javascript.rhino.Node} node
143+
* @returns {Boolean}
144+
*/
145+
parse.isRequirePluginNode = function (node) {
146+
if (node.getType() === EXPR_RESULT) {
147+
assign = node.getFirstChild();
148+
if (assign.getType() === ASSIGN) {
149+
prop = assign.getFirstChild();
150+
if (prop.getType() === GETPROP) {
151+
name = prop.getFirstChild();
152+
if (name.getType() == NAME) {
153+
if (nodeString(name) === "require") {
154+
plugin = prop.getChildAtIndex(1);
155+
if (plugin && plugin.getType() === STRING &&
156+
nodeString(plugin) === "plugin") {
157+
return true;
158+
}
159+
}
160+
}
161+
}
162+
}
163+
}
164+
return false;
165+
};
166+
105167
/**
106168
* Determines if a specific node is a valid require or require.def call.
107169
* @param {Packages.com.google.javascript.rhino.Node} node

build/jslib/tests/all-rhino.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ load("parse.js");
2121
doh.run();
2222
doh.run();
2323
doh.run();
24+
doh.run();

build/jslib/tests/parse.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ doh.register(
3131

3232
t.is('require.modify("one","one-mod",["two","three"],function(){});', parse("good1", good1));
3333
t.is(null, parse("bad1", bad1));
34+
},
35+
36+
function hasRequirePlugin(t) {
37+
var good1 = "var require; function(){ require = function(){}; require.plugin = function(){};}",
38+
bad1 = "var require; function(){ require.plugin(); }";
39+
40+
t.is(true, parse.definesRequirePlugin("good1", good1));
41+
t.is(false, parse.definesRequirePlugin("bad1", bad1));
42+
3443
}
3544
]
3645
);

dist/dist-site.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ load("../build/jslib/fileUtil.js");
2626

2727
var files, i, mdFile, htmlFile, fileContents,
2828
runtime = Packages.java.lang.Runtime.getRuntime(),
29-
process, preContents, postContents, h1, path, length, j, isTopPage = false;
29+
process, preContents, postContents, h1, homePath, cssPath, length, j, isTopPage = false;
3030

3131
//Copy all the text files to a dist directory
3232
fileUtil.deleteFile("./dist-site/");
@@ -63,24 +63,27 @@ for (i = 0; (mdFile = files[i]); i++) {
6363

6464
fileContents = fileContents.replace(/\$\{title\}/, h1);
6565

66-
//Change any .md references to .html references
67-
fileContents = fileContents.replace(/href="([^"]+)\.md/g, 'href="$1.html');
66+
//Change any .md references to .html references, and remove tree/master
67+
//links
68+
fileContents = fileContents.replace(/href="\/tree\/master\/docs\//g, 'href="docs/').replace(/href="([^"]+)\.md/g, 'href="$1.html');
6869

69-
//Adjust the path the main.css
70-
path = htmlFile.replace(/\/[^\/]+$/, "").replace(/^\.\/dist-site\//, "");
71-
if (!path || path === "./dist-site") {
70+
//Adjust the path the home and main.css
71+
homePath = htmlFile.replace(/\/[^\/]+$/, "").replace(/^\.\/dist-site\//, "");
72+
if (!homePath || homePath === "./dist-site") {
7273
isTopPage = true;
73-
path = "main.css";
74+
homePath = "./";
75+
cssPath = "main.css";
7476
} else {
7577
isTopPage = false;
76-
length = path.split("/").length;
77-
path = "";
78+
length = homePath.split("/").length;
79+
homePath = "";
7880
for (j = 0; j < length; j++) {
79-
path += "../";
81+
homePath += "../";
8082
}
81-
path += "main.css";
83+
cssPath = homePath + "main.css";
8284
}
83-
fileContents = fileContents.replace(/\main\.css/, path);
85+
fileContents = fileContents.replace(/HOMEPATH/, homePath);
86+
fileContents = fileContents.replace(/\main\.css/, cssPath);
8487

8588

8689
//If it is the top page, adjust the header links

dist/pre.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
</head>
2525
<body>
2626
<ul id="nav">
27-
<li class="first"><a href="../">Home</a></li>
27+
<li class="first"><a href="HOMEPATH">Home</a></li>
2828

2929
<li class="section"><a class="local" href="start.html">Start</a></li>
3030
<li><a class="local" href="download.html">Download</a></li>
3131
<li><a class="local" href="api.html">API</a></li>
3232
<li><a class="local" href="optimization.html">Optimization</a></li>
3333

34+
<li class="section"><a class="local" href="faq.html">FAQs</a></li>
35+
3436
<li class="section"><a class="local" href="why.html">Why</a></li>
3537
<li><a class="local" href="requirements.html">Requirements</a></li>
3638
<li><a class="local" href="history.html">History</a></li>

docs/download.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
* Renamed from RunJS to RequireJS
88
* Adds better support for existing JS files
99

10-
#### <a name="requirejs">require.js</a> [Minified](release/0.8.0/minified/require.js) | [With Comments](release/0.8.0/comments/require.js)
10+
#### <a name="requirejs">require.js</a> [Minified](http://requirejs.org/docs/release/0.8.0/minified/require.js) | [With Comments](http://requirejs.org/docs/release/0.8.0/comments/require.js)
1111

1212
All you need to start using require.js. Does not include i18n, text plugins or rhino support.
1313

14-
#### <a name="requirejsplugins">require.js with plugins</a> [Minified](release/0.8.0/minified/allplugins-require.js) | [With Comments](release/0.8.0/comments/allplugins-require.js)
14+
#### <a name="requirejsplugins">require.js with plugins</a> [Minified](http://requirejs.org/docs/release/0.8.0/minified/allplugins-require.js) | [With Comments](http://requirejs.org/docs/release/0.8.0/comments/allplugins-require.js)
1515

1616
require.js with the i18n and text plugins included.
1717

18-
#### <a name="jqueryrequirejs">jQuery 1.4.2 with require()</a> [Minified](release/0.8.0/minified/require-jquery-1.4.2.js) | [With Comments](release/0.8.0/comments/require-jquery-1.4.2.js)
18+
#### <a name="jqueryrequirejs">jQuery 1.4.2 with require()</a> [Minified](http://requirejs.org/docs/release/0.8.0/minified/require-jquery-1.4.2.js) | [With Comments](http://requirejs.org/docs/release/0.8.0/comments/require-jquery-1.4.2.js)
1919

2020
A build of jQuery with integrated require() support. Just includes the basic RequireJS, does not have the following features:
2121

@@ -24,7 +24,7 @@ A build of jQuery with integrated require() support. Just includes the basic Req
2424
* page load support (it is assumed you will use jQuery's methods)
2525
* require.modify() support
2626

27-
#### <a name="jqueryrequirejsplugins">jQuery 1.4.2 with require() and plugins</a> [Minified](release/0.8.0/minified/requireplugins-jquery-1.4.2.js) | [With Comments](release/0.8.0/comments/requireplugins-jquery-1.4.2.js)
27+
#### <a name="jqueryrequirejsplugins">jQuery 1.4.2 with require() and plugins</a> [Minified](http://requirejs.org/docs/release/0.8.0/minified/requireplugins-jquery-1.4.2.js) | [With Comments](http://requirejs.org/docs/release/0.8.0/comments/requireplugins-jquery-1.4.2.js)
2828

2929
A build of jQuery with integrated require() support and the i18n and text plugins. Does not include these other RequireJS features:
3030

@@ -36,7 +36,7 @@ The integrated builds with jQuery contain the changes in [this jQuery fork](http
3636

3737
<hr>
3838

39-
#### <a name="samplejquery">Sample jQuery 1.4.2 project with require()</a> [Download](release/0.8.0/jquery-require-sample.zip)
39+
#### <a name="samplejquery">Sample jQuery 1.4.2 project with require()</a> [Download](http://requirejs.org/docs/release/0.8.0/jquery-require-sample.zip)
4040

4141
A zip file containing a build of jQuery with integrated require() support, with an sample project included to show how it can be used when using jQuery. Does not include these features in RequireJS:
4242

@@ -47,7 +47,7 @@ A zip file containing a build of jQuery with integrated require() support, with
4747

4848
<hr>
4949

50-
#### <a name="optimizationtool">Optimization Tool / Full Source</a> [Download](release/0.8.0/requirejs-0.8.0.zip)
50+
#### <a name="optimizationtool">Optimization Tool / Full Source</a> [Download](http://requirejs.org/docs/release/0.8.0/requirejs-0.8.0.zip)
5151

5252
A zip file that is the optimization tool for RequireJS. It also includes the full source for require.js and its plugins.
5353

docs/faq-optimization.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# FAQ: RequireJS Optimization Tool
2+
3+
* [How do I use the Optimization Tool?](#usage)
4+
* [What if I just have some JavaScript files to optimize, not a whole application directory?](#scriptsonly)
5+
* [Expanding on the previous question, what if I want optimize to just one JS request?](#onescript)
6+
7+
8+
### <a name="usage">How do I use the Optimization Tool?</a>
9+
10+
See the [general optimization page](optimization.md) for basic set-up. Also see the [jQuery doc page](jquery.md) for a good way to set up your project, even if you are not using jQuery.
11+
12+
### <a name="scriptsonly">What if I just have some JavaScript files to optimize, not a whole application directory?</a>
13+
14+
In this case, you have just some .js files you want to optimize and do not want to copy a whole application directory. Here is how you might have your code set up:
15+
16+
* appdirectory
17+
* app.html
18+
* scripts
19+
* require.js
20+
* app.js
21+
* one.js
22+
* two.js
23+
* three.js
24+
* requirejs (the unzipped optimization tool from the [download page](download.md))
25+
26+
app.html has script tags for require.js and loads app.js via a require call, like so:
27+
28+
<!DOCTYPE html>
29+
<html>
30+
<head>
31+
<title>My App</title>
32+
<script src="scripts/require.js"></script>
33+
<script>require(["app"]);</script>
34+
</head>
35+
<body>
36+
<h1>My App</h1>
37+
</body>
38+
</html>
39+
40+
app.js loads one.js, two.js and three.js via a require call:
41+
42+
require(["one", "two", "three"], function (one, two, three) {
43+
});
44+
45+
You just want combine app.js, one.js, two.js and three.js into one script file, and you just want to process the .js files, you do not want the optimization tool to copy over your HTML and CSS, and therefore skip the CSS optimizations.
46+
47+
In that case, create a build profile, call it app.build.js, and put it in the **scripts** 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.
48+
49+
require({
50+
baseUrl: "./"
51+
dir: "../scripts-build",
52+
requireUrl: "../../requirejs/require.js"
53+
},
54+
"app");
55+
56+
This build profile tells RequireJS to treat the baseUrl to find scripts as the current directory (the **scripts** directory since this example assumes app.build.js is in the **scripts** directory), and to put the built output in the **scripts-build** directory, which will be a sibling to the **scripts** directory. It is strongly suggested you use a different output directory than the source directory -- otherwise bad things will likely happen as the optimization tool overwrites your source.
57+
58+
The **requireUrl** tells the optimization tool where to find the require.js that accompanies the optimization tool. The tool uses require.js in the optimization process.
59+
60+
The final argument to the require() call, **"app"**, tells the build system to use app.js as the basis for the build layer. The build system will then trace the dependencies for app.js and inject them into the **scripts-build/app.js** file.
61+
62+
To run the build on Unix/Linux systems, run this command from inside the **scripts** directory:
63+
64+
../../requirejs/build/build.sh app.build.js
65+
66+
For windows operating systems (a .bat file is in the works to make this easier):
67+
68+
java -classpath ..\..\requirejs\build\lib\rhino\js.jar;..\..\requirejs\build\lib\closure\compiler.jar org.mozilla.javascript.tools.shell.Main ..\..\requirejs\build\build.js ..\..\requirejs\build\ app.build.js
69+
70+
Once the build is done, you can use **scripts-build/app.js** in your deployed application. It will contain app.js, one.js, two.js and three.js.
71+
72+
### Expanding on the previous question, what if I want optimize to just one JS request?
73+
74+
In that case, assuming the same setup as above, add the **includeRequire: true** to the build profile:
75+
76+
require({
77+
baseUrl: "./"
78+
dir: "../scripts-build",
79+
requireUrl: "../../requirejs/require.js",
80+
includeRequire: true
81+
},
82+
"app");
83+
84+
After the optimization tool runs, the **scripts-build/app.js** will include the contents of require.js, app.js, one.js, two.js and three.js. Copy **scripts-build/app.js** to your deployment area and rename it to **require.js** to load everything in one script call.
85+

docs/faq.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# RequireJS FAQs (Frequently Asked Questions)
2+
3+
* [Optimization Tool](faq-optimization.md)
4+
5+

docs/optimization.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ Each call to require() defines a build layer. The require() call looks similar t
3737
## Build layer configuration options
3838

3939
There is an [example.build.js](http://github.com/jrburke/requirejs/blob/master/build/example.build.js) file in the requirejs/build directory that details all the options that are allowed in the configuration options. It also gives some examples of require() calls for build layers.
40+
41+
## More Questions?
42+
43+
See the [Optimization FAQ](faq-optimization.md) for a discussion of different optimization approaches.

0 commit comments

Comments
 (0)