You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h2><aname="rename">How can I rename require?</a><spanclass="sectionMark">§ 1</span></h2>
11
+
<h2><aname="rename">How can I rename require/define/requirejs?</a><spanclass="sectionMark">§ 1</span></h2>
12
12
13
-
<p>RequireJS and its optimization tool need to be version 0.11 or higher for this to work.</p>
13
+
<p>RequireJS and its optimization tool need to be version 0.26.0 or higher for this to work.</p>
14
14
15
-
<p>Why would you want to do this? You may have very strict global namespace requirements or you may be using code that already defines a require and you want to avoid interference. There are two steps to use the require defined by RequireJS with a different name:</p>
15
+
<p>Why would you want to do this? You may have very strict global namespace requirements
16
+
or you may be using code that already defines require/define and you want to avoid interference.</p>
17
+
18
+
<p>Some notes on this capability:</p>
19
+
20
+
<ul>
21
+
<li>Make sure to use a source version of require.js, not a minified version.</li>
22
+
<li>Make sure that version of require.js is included in the optimization because the require.js
23
+
file contents are altered for the namespacing to work.</li>
24
+
<li>Code your modules using require/define as normal, then do a build to namespace the values.
25
+
Do not code your modules using the namespaced require/define. It will make your code less
26
+
portable and usable by others.</li>
27
+
<li>This transformation/optimization only works once. Do not use the output of this optimization as
28
+
input to another optimization/build stage.</li>
29
+
</ul>
30
+
31
+
<p>The following example optimization config is based on the directory structure
32
+
used in the example on the <ahref="optimization.html"> optimization page</a>. This config combines
33
+
require.js with main.js into a new <strong>foo.js</strong>. file. define() is renamed to <strong>foo.define()</strong>
34
+
and require() is renamed to <strong>foo.require()</strong>:</p>
35
+
36
+
<pre><code>
37
+
{
38
+
appDir: "../",
39
+
baseUrl: "scripts",
40
+
dir: "../../appdirectory-build",
41
+
42
+
//Put in a mapping so that 'requireLib' in the
43
+
//modules section below will refer to the require.js
44
+
//contents.
45
+
paths: {
46
+
requireLib: 'require'
47
+
},
48
+
49
+
//Indicates the namespace to use for require/requirejs/define.
50
+
namespace: "foo",
51
+
52
+
modules: [
53
+
{
54
+
name: "foo",
55
+
include: ["requireLib", "main"],
56
+
//True tells the optimizer it is OK to create
57
+
//a new file foo.js. Normally the optimizer
58
+
//wants foo.js to exist in the source directory.
59
+
create: true
60
+
}
61
+
]
62
+
}
63
+
</code></pre>
64
+
65
+
66
+
<p>Once this optimization is done, the HTML that used to refer to require.js would
67
+
need to be modified to refer to foo.js.</p>
68
+
69
+
<p>Thanks to <ahref="http://ryanflorence.com">Ryan Florence</a> for help on the namespace design.</p>
70
+
71
+
<hr>
72
+
73
+
<p>Another approach to renaming, if you prefer to have more direct control of the content, and
74
+
want to commit source code with the modifications. This approach <strong>should not be used</strong>
75
+
with the "namespace" optimization demonstrated above.</p>
0 commit comments