forked from seajs/seajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.html
156 lines (118 loc) · 3.18 KB
/
rules.html
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
<!doctype html>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache"/>
<meta charset="utf-8"/>
<title>Rules - Sea.js Manual & Documentation</title>
<link rel="stylesheet" href="assets/api.css"/>
</head>
<body>
<div id="container">
<header>
<h1>Sea.js Manual & Documentation</h1>
<div id="gtoc">
<p><a href="../">Home</a> | <a href="index.html">Index</a></p>
</div>
<hr/>
</header>
<div id="toc">
<h2 id="table_of_Contents">Table of Contents</h2>
<ul>
<li><a href="#rules">Rules</a>
<ol>
<li><a href="#spell-it-right">Spell It Right</a></li>
<li><a href="#leave-it-alone">Leave It Alone</a></li>
<li><a href="#use-string-literals">Use String Literals</a></li>
</ol>
</li>
<li><a href="#note-dynamic-deps">A Note About Dynamic Dependencies</a></li>
</ul>
<hr/>
</div>
<h2 id="rules">Rules</h2>
<p>
In order for static analysis to work properly, you'll need to follow a few
simple rules when using the require function:
</p>
<h3 id="spell-it-right">1. Spell It Right</h3>
<p>
In the wrapper, the first argument <em>MUST</em> be named
"<code>require</code>".
</p>
<pre>
// Wrong!
define(function(req) {
// ...
});
// Right!
define(function(require) {
// ...
});
</pre>
<h3 id="leave-it-alone">2. Leave It Alone</h3>
<p>
Don't rename the <code>require</code> function, or assign any other value
to the <code>require</code> variable <em>in any scope</em>.
</p>
<pre>
// Wrong - Renaming "require"!
var req = require, mod = req("./mod");
// Wrong - Redefining "require"!
require = function() {};
// Wrong - Redefining "require" as a function parameter!
function F(require) {}
// Wrong - Redefining "require" in a nested scope!
function F() {
var require = function() {};
}
</pre>
<h3 id="use-string-literals">3. Use String Literals</h3>
<p>
The argument to <code>require</code> <em>MUST</em> be a single string
literal.
</p>
<pre>
// Wrong!
require(myModule);
// Wrong!
require("my-" + "module");
// Wrong!
require("MY-MODULE".toLowerCase());
// Right!
require("my-module");
</pre>
<p>
I assure you, you can create large, complex applications while following
these rules. Think of <code>require</code> not as a function, but as a
language-level statement, and you'll be just fine.
</p>
<h2 id="note-dynamic-deps">A Note About Dynamic Dependencies</h2>
<p>
Occasionally developers have felt the need to use <code>require</code>
conditionally:
</p>
<pre>
if (todayIsWeekend)
require("play");
else
require("work");
</pre>
<p>
Keep in mind that from the point of view of static analysis, this module is
dependant on both the "play" and "work" modules, and the loader will
probably attempt to fetch both.
</p>
<p>
In this case, you can use <code>require.async</code> to load conditional
modules asynchronously.
</p>
<hr/>
<p>
This page is forked from
<a href="http://www.flyscript.org/docs-static-analysis">FlyScript's Documentation</a>.
Thanks very much to Kevin H. Smith.
</p>
</div>
<script src="../dist/sea.js" data-main="assets/init"></script>
</body>
</html>