Skip to content

Commit 34ea126

Browse files
committed
Doc updates, and add a test that was part of last commit.
1 parent be45948 commit 34ea126

5 files changed

Lines changed: 79 additions & 18 deletions

File tree

docs/api.html

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ <h1>RequireJS API</h1>
1111
<li class="hbox"><a href="#deffunc">Definition Functions</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.2</span></li>
1212
<li class="hbox"><a href="#defdep">Definition Functions with Dependencies</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.3</span></li>
1313
<li class="hbox"><a href="#funcmodule">Define a Module as a Function</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.4</span></li>
14-
<li class="hbox"><a href="#modulename">Define a Module with a name</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.5</span></li>
15-
<li class="hbox"><a href="#modulenotes">Other Module Notes</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.6</span></li>
16-
<li class="hbox"><a href="#circular">Circular Dependencies</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.7</span></li>
14+
<li class="hbox"><a href="#cjsmodule">Define a Module with Simplified CommonJS Wrapper</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.5</span></li>
15+
<li class="hbox"><a href="#modulename">Define a Module with a name</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.6</span></li>
16+
<li class="hbox"><a href="#modulenotes">Other Module Notes</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.7</span></li>
17+
<li class="hbox"><a href="#circular">Circular Dependencies</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.2.8</span></li>
1718
</ul>
1819
<li class="hbox"><a href="#i18n">Define an I18N Bundle</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.3</span></li>
1920
<li class="hbox"><a href="#text">Specify a Text File Dependency</a><span class="spacer boxFlex"></span><span class="sect">&sect; 1.4</span></li>
@@ -76,7 +77,15 @@ <h3>
7677
<li>b.js (in the same directory as the HTML page that has the above HTML snippet)</li>
7778
</ul>
7879

79-
<p>Files that end in ".js" are assumed to just be plain JS files that do not use the RequireJS module syntax, and therefore do not use the module-to-path algorithm used for looking up dot-notation modules, like "some/module" above.</p>
80+
<p>RequireJS normally uses the <a href="#config-baseUrl">baseUrl</a> and <a href="#config-paths">paths</a> configuration to convert names like "some/module" into a file path.</p>
81+
82+
<p>However, if the dependency name has one of the following properties, it is treated as a regular file path, like something that was passed to a &lt;script src=""&gt; tag:</p>
83+
84+
<ul>
85+
<li>Ends in ".js".</li>
86+
<li>Starts with a "/".</li>
87+
<li>Contains an URL protocol, like "http:" or "https:".</li>
88+
</ul>
8089

8190
<p>See the <a href="#config">Configuration Options</a> section for information on changing the lookup paths used for dependencies.</p>
8291

@@ -218,8 +227,20 @@ <h4><a name="funcmodule">Define a Module as a Function</a><span class="sectionMa
218227
</code></pre>
219228
</div>
220229

230+
231+
<div class="subSection">
232+
<h4><a name="cjsmodule">Define a Module with Simplified CommonJS Wrapper</a><span class="sectionMark">&sect; 1.2.5</span></h4>
233+
234+
<p>If you wish to reuse some code that was written in the traditional <a href="http://wiki.commonjs.org/wiki/Modules/1.1.1">CommonJS module format</a> it may be difficult to re-work to the array of dependencies used above. Also, Node 0.5 will likely support a limited kind of define() function that provides a simple wrapper over CommonJS files. So if you are building a small utility and you wish it to be used in both Node (without using the <a href="node.html">RequireJS Node adapter</a>), and in the browser, you can use the <a href="commonjs.html">simplified CommonJS wrapper</a>.</p>
235+
236+
<p>It can also be useful if you have many dependencies, and keeping dependency array positioning in sync with the function argument positioning is undesirable.</p>
237+
238+
<p>This wrapper relies on Function.prototype.toString() to give a useful string value of the function contents. This does not work on some devices like the PS3 and some older Opera mobile browsers. Use the <a href="optimization.html">optimizer</a> to pull out the dependencies in the array format for use on those devices.</p>
239+
</div>
240+
241+
221242
<div class="subSection">
222-
<h4><a name="modulename">Define a Module with a Name</a><span class="sectionMark">&sect; 1.2.5</span></h4>
243+
<h4><a name="modulename">Define a Module with a Name</a><span class="sectionMark">&sect; 1.2.6</span></h4>
223244

224245
<p>You may encounter some define() calls that include a name for the module as the first argument to define():</p>
225246

@@ -237,7 +258,7 @@ <h4><a name="modulename">Define a Module with a Name</a><span class="sectionMark
237258

238259
<div class="subSection">
239260

240-
<h4><a name="modulenotes">Other Module Notes</a><span class="sectionMark">&sect; 1.2.6</span></h4>
261+
<h4><a name="modulenotes">Other Module Notes</a><span class="sectionMark">&sect; 1.2.7</span></h4>
241262

242263
<p><strong>One module per file.</strong>: Only one module should be defined per JavaScript file, given the nature of the module name-to-file-path lookup algorithm. Multiple modules will be grouped into optimized files by the <a href="optimization.html">optimization tool</a>, but you should only use the optimization tool to place more than one module in a file.</p>
243264

@@ -266,7 +287,7 @@ <h4><a name="modulenotes">Other Module Notes</a><span class="sectionMark">&sect;
266287
</div>
267288

268289
<div class="subSection">
269-
<h4><a name="circular">Circular Dependencies</a><span class="sectionMark">&sect; 1.2.7</span></h4>
290+
<h4><a name="circular">Circular Dependencies</a><span class="sectionMark">&sect; 1.2.8</span></h4>
270291

271292
<p>If you define a circular dependency (A needs B and B needs A), then in this case when B's module function is called, it will get an undefined value for A. B can fetch A later after modules have been defined by using the require() method (be sure to specify require as a dependency so the right context is used to look up A):</p>
272293

docs/errors.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h2><a name="mismatch">Mismatched anonymous define() modules ...</a><span class=
2020

2121
<p>Also seen if you manually code a script tag in HTML to load a script that has a few named modules, but then try to load an anonymous module that ends up having the same name as one of the named modules in the script loaded by the manually coded script tag.</p>
2222

23-
<p>Finally, if you use the loader plugins or anonymous modules but do not use the RequireJS optimizer to combine files together, this error can occur. The optimizer knows how to name anonymous modules correctly so that can be combined with other modules in an optimized file.</p>
23+
<p>Finally, if you use the loader plugins or anonymous modules (modules that call define() with no string ID) but do not use the RequireJS optimizer to combine files together, this error can occur. The optimizer knows how to name anonymous modules correctly so that they can be combined with other modules in an optimized file.</p>
2424

2525
<p>To avoid the error:</p>
2626

docs/node.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ <h2>
3535

3636
<p>RequireJS will use its <a href="api.html#config">Configuration Options</a> first to find modules. If RequireJS cannot find the module with its configuration, it is assumed to be a module that uses Node's type of modules and configuration. So, only configure module locations with RequireJS if they use the RequireJS API. For modules that expect Node's APIs and configuration/paths, just install them with a Node package manager, like <a href="http://npmjs.org/">npm</a>, and do not configure their locations with RequireJS.</p>
3737

38+
<p><strong>Best practice</strong>: Use npm to install Node-only packages/modules into the projects <strong>node_modules</strong> directory, but do not configure RequireJS to look inside the node_modules directory. Also avoid using relative module IDs to reference modules that are Node-only modules. So, <strong>do not</strong> do something like <strong>require("./node_modules/foo/foo")</strong>.</p>
39+
3840
<p>Even though RequireJS is an asynchronous loader in the browser, the RequireJS Node adapter loads modules synchronously in the Node environment to match the default loading behavior in Node.</p>
3941

4042
<p>Finally, RequireJS in Node can only load modules that are on the local disk -- fetching modules across http, for instance, is not supported at this time.</p>

tasks.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,7 @@ Next release
1515

1616
- Confirm text plugin API change is worth it.
1717

18-
- anonymous define call 4th case:
19-
We found a fourth path, two calls to define() in one file.
20-
require-debug.js reports this error as:
21-
Second anonymous define(): <the text of the second function found>
22-
so then you can search for the text of the second function to perhaps understand the problem.
23-
2418
Docs:
25-
- http://requirejs.org/docs/api.html#jsfiles make the "plain script tag rules"
26-
more of a bulleted list to call them out.
27-
- Update api.html#define to indicate commonjs wrapper.
28-
- Interaction of node_modules and paths in r.js
2919
- Pointer to motivations behind current thing, reason for dependencies.
3020
- Put page title in head's title element.
3121

tests/configRequirejs.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>require.js: Config requirejs Test</title>
5+
<script type="text/javascript" src="doh/runner.js"></script>
6+
<script type="text/javascript" src="doh/_browserRunner.js"></script>
7+
<script>
8+
function require() {
9+
return "pristine";
10+
}
11+
12+
function done() {
13+
doh.register(
14+
"config",
15+
[
16+
function config(t){
17+
t.is("pristine", require());
18+
t.is("blue", s.color);
19+
t.is("dimple-blue", d.color);
20+
t.is("You called a function", f());
21+
}
22+
]
23+
);
24+
doh.run();
25+
}
26+
27+
var s, d, f,
28+
requirejs = {
29+
baseUrl: "./",
30+
deps: ["require", "simple", "dimple", "func"],
31+
callback: function (require, simple, dimple, func) {
32+
s = simple;
33+
d = dimple;
34+
f = func;
35+
},
36+
ready: function() {
37+
done();
38+
}
39+
};
40+
</script>
41+
<script type="text/javascript" src="../require.js"></script>
42+
</head>
43+
<body>
44+
<h1>require.js: Config requirejs Test</h1>
45+
<p>Tests requirejs being defined as an object with init and ready configuration.</p>
46+
<p>Check console for messages.</p>
47+
</body>
48+
</html>

0 commit comments

Comments
 (0)