requirejs-rails 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,6 +40,7 @@ module Requirejs::Rails
40
40
  paths
41
41
  priority
42
42
  scriptType
43
+ shim
43
44
  urlArgs
44
45
  waitSeconds
45
46
  xhtml
@@ -75,6 +76,7 @@ module Requirejs::Rails
75
76
  pragmas
76
77
  pragmasOnSave
77
78
  preserveLicenseComments
79
+ shim
78
80
  skipModuleInsertion
79
81
  skipPragmas
80
82
  uglify
@@ -108,7 +110,6 @@ module Requirejs::Rails
108
110
  mod['name'] = 'almond'
109
111
  mod['include'] = name
110
112
  end
111
- self.rewrite_urls_in_paths self[:build_config]
112
113
  end
113
114
  self[:build_config]
114
115
  end
@@ -150,13 +151,5 @@ module Requirejs::Rails
150
151
  accum || (matcher =~ asset)
151
152
  end ? true : false
152
153
  end
153
-
154
- def rewrite_urls_in_paths(cfg)
155
- if cfg.has_key? 'paths'
156
- cfg['paths'] = cfg['paths'].each_with_object({}) do |(k, v), h|
157
- h[k] = (v =~ /^https?:/) ? 'empty:' : v
158
- end
159
- end
160
- end
161
154
  end
162
155
  end
@@ -1,6 +1,6 @@
1
1
  module Requirejs
2
2
  module Rails
3
- Version = "0.8.2"
4
- LibVersion = "1.0.8"
3
+ Version = "0.9.0"
4
+ LibVersion = "2.0.2"
5
5
  end
6
6
  end
@@ -85,27 +85,7 @@ class RequirejsRailsConfigTest < ActiveSupport::TestCase
85
85
  assert_nil @cfg.build_config['priority']
86
86
  end
87
87
 
88
- test "build config should replace urls in paths with 'empty:'" do
89
- @cfg.user_config = { 'paths' =>
90
- {
91
- 'jquery' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js',
92
- 'jqueryssl' => 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'
93
- }
94
- }
95
- assert_equal 'empty:', @cfg.build_config['paths']['jquery']
96
- assert_equal 'empty:', @cfg.build_config['paths']['jqueryssl']
97
- end
98
-
99
- test "build_config should not modify non-urls in paths" do
100
- @cfg.user_config = { 'paths' =>
101
- {
102
- 'foo' => 'components/foo'
103
- }
104
- }
105
- assert_equal 'components/foo', @cfg.build_config['paths']['foo']
106
- end
107
-
108
- ## Almond-specific tests
88
+ ## Almond tests
109
89
  test "build_config with almond should accept one module" do
110
90
  @cfg.loader = :almond
111
91
  @cfg.user_config = { 'modules' => [ { 'name' => 'foo' } ] }
@@ -149,7 +129,7 @@ class RequirejsHelperTest < ActionView::TestCase
149
129
 
150
130
  test "requirejs_include_tag_with_param" do
151
131
  render :text => wrap(requirejs_include_tag("application"))
152
- assert_select "script:last-of-type[src^=/javascripts/require.js][data-main^=/javascripts/application.js]", :count => 1
132
+ assert_select "script:last-of-type[src^=/javascripts/require.js][data-main^=/javascripts/application]", :count => 1
153
133
  end
154
134
 
155
135
  test "requirejs_include_tag_with_block" do
@@ -158,7 +138,7 @@ class RequirejsHelperTest < ActionView::TestCase
158
138
  end
159
139
 
160
140
  render :text => wrap(requirejs_include_tag("application", &test_block))
161
- assert_select "script:last-of-type[src^=/javascripts/require.js][data-main^=/javascripts/application.js]", :count => 1
141
+ assert_select "script:last-of-type[src^=/javascripts/require.js][data-main^=/javascripts/application]", :count => 1
162
142
  assert_select "script:last-of-type[src^=/javascripts/require.js][data-class^=TestController]", :count => 1
163
143
  end
164
144
 
@@ -1,25 +1,22 @@
1
1
  /**
2
- * almond 0.0.3 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
2
+ * almond 0.1.1 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
3
3
  * Available via the MIT or new BSD license.
4
4
  * see: http://github.com/jrburke/almond for details
5
5
  */
6
- /*jslint strict: false, plusplus: false */
6
+ //Going sloppy to avoid 'use strict' string cost, but strict practices should
7
+ //be followed.
8
+ /*jslint sloppy: true */
7
9
  /*global setTimeout: false */
8
10
 
9
11
  var requirejs, require, define;
10
12
  (function (undef) {
11
-
12
13
  var defined = {},
13
14
  waiting = {},
15
+ config = {},
16
+ defining = {},
14
17
  aps = [].slice,
15
18
  main, req;
16
19
 
17
- if (typeof define === "function") {
18
- //If a define is already in play via another AMD loader,
19
- //do not overwrite.
20
- return;
21
- }
22
-
23
20
  /**
24
21
  * Given a relative module name, like ./something, normalize it to
25
22
  * a real name that can be mapped to a path.
@@ -29,6 +26,11 @@ var requirejs, require, define;
29
26
  * @returns {String} normalized name
30
27
  */
31
28
  function normalize(name, baseName) {
29
+ var baseParts = baseName && baseName.split("/"),
30
+ map = config.map,
31
+ starMap = (map && map['*']) || {},
32
+ nameParts, nameSegment, mapValue, foundMap, i, j, part;
33
+
32
34
  //Adjust any relative paths.
33
35
  if (name && name.charAt(0) === ".") {
34
36
  //If have a base name, try to normalize against it,
@@ -40,13 +42,11 @@ var requirejs, require, define;
40
42
  //module. For instance, baseName of "one/two/three", maps to
41
43
  //"one/two/three.js", but we want the directory, "one/two" for
42
44
  //this normalization.
43
- baseName = baseName.split("/");
44
- baseName = baseName.slice(0, baseName.length - 1);
45
+ baseParts = baseParts.slice(0, baseParts.length - 1);
45
46
 
46
- name = baseName.concat(name.split("/"));
47
+ name = baseParts.concat(name.split("/"));
47
48
 
48
49
  //start trimDots
49
- var i, part;
50
50
  for (i = 0; (part = name[i]); i++) {
51
51
  if (part === ".") {
52
52
  name.splice(i, 1);
@@ -59,7 +59,7 @@ var requirejs, require, define;
59
59
  //no path mapping for a path starting with '..'.
60
60
  //This can still fail, but catches the most reasonable
61
61
  //uses of ..
62
- break;
62
+ return true;
63
63
  } else if (i > 0) {
64
64
  name.splice(i - 1, 2);
65
65
  i -= 2;
@@ -71,6 +71,43 @@ var requirejs, require, define;
71
71
  name = name.join("/");
72
72
  }
73
73
  }
74
+
75
+ //Apply map config if available.
76
+ if ((baseParts || starMap) && map) {
77
+ nameParts = name.split('/');
78
+
79
+ for (i = nameParts.length; i > 0; i -= 1) {
80
+ nameSegment = nameParts.slice(0, i).join("/");
81
+
82
+ if (baseParts) {
83
+ //Find the longest baseName segment match in the config.
84
+ //So, do joins on the biggest to smallest lengths of baseParts.
85
+ for (j = baseParts.length; j > 0; j -= 1) {
86
+ mapValue = map[baseParts.slice(0, j).join('/')];
87
+
88
+ //baseName segment has config, find if it has one for
89
+ //this name.
90
+ if (mapValue) {
91
+ mapValue = mapValue[nameSegment];
92
+ if (mapValue) {
93
+ //Match, update name to the new value.
94
+ foundMap = mapValue;
95
+ break;
96
+ }
97
+ }
98
+ }
99
+ }
100
+
101
+ foundMap = foundMap || starMap[nameSegment];
102
+
103
+ if (foundMap) {
104
+ nameParts.splice(0, i, foundMap);
105
+ name = nameParts.join('/');
106
+ break;
107
+ }
108
+ }
109
+ }
110
+
74
111
  return name;
75
112
  }
76
113
 
@@ -99,8 +136,13 @@ var requirejs, require, define;
99
136
  if (waiting.hasOwnProperty(name)) {
100
137
  var args = waiting[name];
101
138
  delete waiting[name];
139
+ defining[name] = true;
102
140
  main.apply(undef, args);
103
141
  }
142
+
143
+ if (!defined.hasOwnProperty(name)) {
144
+ throw new Error('No ' + name);
145
+ }
104
146
  return defined[name];
105
147
  }
106
148
 
@@ -136,27 +178,27 @@ var requirejs, require, define;
136
178
  };
137
179
  }
138
180
 
181
+ function makeConfig(name) {
182
+ return function () {
183
+ return (config && config.config && config.config[name]) || {};
184
+ };
185
+ }
186
+
139
187
  main = function (name, deps, callback, relName) {
140
188
  var args = [],
141
189
  usingExports,
142
- cjsModule, depName, i, ret, map;
190
+ cjsModule, depName, ret, map, i;
143
191
 
144
192
  //Use name if no relName
145
- if (!relName) {
146
- relName = name;
147
- }
193
+ relName = relName || name;
148
194
 
149
195
  //Call the callback to define the module, if necessary.
150
196
  if (typeof callback === 'function') {
151
197
 
152
- //Default to require, exports, module if no deps if
153
- //the factory arg has any arguments specified.
154
- if (!deps.length && callback.length) {
155
- deps = ['require', 'exports', 'module'];
156
- }
157
-
158
198
  //Pull out the defined dependencies and pass the ordered
159
199
  //values to the callback.
200
+ //Default to [require, exports, module] if no deps
201
+ deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
160
202
  for (i = 0; i < deps.length; i++) {
161
203
  map = makeMap(deps[i], relName);
162
204
  depName = map.f;
@@ -173,15 +215,16 @@ var requirejs, require, define;
173
215
  cjsModule = args[i] = {
174
216
  id: name,
175
217
  uri: '',
176
- exports: defined[name]
218
+ exports: defined[name],
219
+ config: makeConfig(name)
177
220
  };
178
221
  } else if (defined.hasOwnProperty(depName) || waiting.hasOwnProperty(depName)) {
179
222
  args[i] = callDep(depName);
180
223
  } else if (map.p) {
181
224
  map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
182
225
  args[i] = defined[depName];
183
- } else {
184
- throw name + ' missing ' + depName;
226
+ } else if (!defining[depName]) {
227
+ throw new Error(name + ' missing ' + depName);
185
228
  }
186
229
  }
187
230
 
@@ -191,9 +234,10 @@ var requirejs, require, define;
191
234
  //If setting exports via "module" is in play,
192
235
  //favor that over return value and exports. After that,
193
236
  //favor a non-undefined return value over exports use.
194
- if (cjsModule && cjsModule.exports !== undef) {
237
+ if (cjsModule && cjsModule.exports !== undef &&
238
+ cjsModule.exports !== defined[name]) {
195
239
  defined[name] = cjsModule.exports;
196
- } else if (!usingExports) {
240
+ } else if (ret !== undef || !usingExports) {
197
241
  //Use the return value from the function.
198
242
  defined[name] = ret;
199
243
  }
@@ -205,9 +249,8 @@ var requirejs, require, define;
205
249
  }
206
250
  };
207
251
 
208
- requirejs = req = function (deps, callback, relName, forceSync) {
252
+ requirejs = require = req = function (deps, callback, relName, forceSync) {
209
253
  if (typeof deps === "string") {
210
-
211
254
  //Just return the module wanted. In this scenario, the
212
255
  //deps arg is the module name, and second arg (if passed)
213
256
  //is just the relName.
@@ -215,17 +258,21 @@ var requirejs, require, define;
215
258
  return callDep(makeMap(deps, callback).f);
216
259
  } else if (!deps.splice) {
217
260
  //deps is a config object, not an array.
218
- //Drop the config stuff on the ground.
261
+ config = deps;
219
262
  if (callback.splice) {
220
263
  //callback is an array, which means it is a dependency list.
221
264
  //Adjust args if there are dependencies
222
265
  deps = callback;
223
- callback = arguments[2];
266
+ callback = relName;
267
+ relName = null;
224
268
  } else {
225
- deps = [];
269
+ deps = undef;
226
270
  }
227
271
  }
228
272
 
273
+ //Support require(['a'])
274
+ callback = callback || function () {};
275
+
229
276
  //Simulate async callback;
230
277
  if (forceSync) {
231
278
  main(undef, deps, callback, relName);
@@ -242,17 +289,11 @@ var requirejs, require, define;
242
289
  * Just drops the config on the floor, but returns req in case
243
290
  * the config return value is used.
244
291
  */
245
- req.config = function () {
292
+ req.config = function (cfg) {
293
+ config = cfg;
246
294
  return req;
247
295
  };
248
296
 
249
- /**
250
- * Export require as a global, but only if it does not already exist.
251
- */
252
- if (!require) {
253
- require = req;
254
- }
255
-
256
297
  define = function (name, deps, callback) {
257
298
 
258
299
  //This module may not have dependencies
@@ -264,11 +305,7 @@ var requirejs, require, define;
264
305
  deps = [];
265
306
  }
266
307
 
267
- if (define.unordered) {
268
- waiting[name] = [name, deps, callback];
269
- } else {
270
- main(name, deps, callback);
271
- }
308
+ waiting[name] = [name, deps, callback];
272
309
  };
273
310
 
274
311
  define.amd = {
@@ -1,54 +1,98 @@
1
1
  /** vim: et:ts=4:sw=4:sts=4
2
- * @license RequireJS 1.0.8 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
2
+ * @license RequireJS 2.0.2 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
3
3
  * Available via the MIT or new BSD license.
4
4
  * see: http://github.com/jrburke/requirejs for details
5
5
  */
6
- /*jslint strict: false, plusplus: false, sub: true */
6
+ /*jslint regexp: true, nomen: true */
7
7
  /*global window, navigator, document, importScripts, jQuery, setTimeout, opera */
8
8
 
9
9
  var requirejs, require, define;
10
- (function (undefined) {
11
- //Change this version number for each release.
12
- var version = "1.0.8",
10
+ (function (global) {
11
+ 'use strict';
12
+
13
+ var version = '2.0.2',
13
14
  commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
14
- cjsRequireRegExp = /require\(\s*["']([^'"\s]+)["']\s*\)/g,
15
- currDirRegExp = /^\.\//,
15
+ cjsRequireRegExp = /require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,
16
16
  jsSuffixRegExp = /\.js$/,
17
+ currDirRegExp = /^\.\//,
17
18
  ostring = Object.prototype.toString,
18
19
  ap = Array.prototype,
19
20
  aps = ap.slice,
20
21
  apsp = ap.splice,
21
- isBrowser = !!(typeof window !== "undefined" && navigator && document),
22
- isWebWorker = !isBrowser && typeof importScripts !== "undefined",
22
+ isBrowser = !!(typeof window !== 'undefined' && navigator && document),
23
+ isWebWorker = !isBrowser && typeof importScripts !== 'undefined',
23
24
  //PS3 indicates loaded and complete, but need to wait for complete
24
- //specifically. Sequence is "loading", "loaded", execution,
25
- // then "complete". The UA check is unfortunate, but not sure how
25
+ //specifically. Sequence is 'loading', 'loaded', execution,
26
+ // then 'complete'. The UA check is unfortunate, but not sure how
26
27
  //to feature test w/o causing perf issues.
27
28
  readyRegExp = isBrowser && navigator.platform === 'PLAYSTATION 3' ?
28
29
  /^complete$/ : /^(complete|loaded)$/,
29
- defContextName = "_",
30
+ defContextName = '_',
30
31
  //Oh the tragedy, detecting opera. See the usage of isOpera for reason.
31
- isOpera = typeof opera !== "undefined" && opera.toString() === "[object Opera]",
32
- empty = {},
32
+ isOpera = typeof opera !== 'undefined' && opera.toString() === '[object Opera]',
33
33
  contexts = {},
34
+ cfg = {},
34
35
  globalDefQueue = [],
35
- interactiveScript = null,
36
- checkLoadedDepth = 0,
37
36
  useInteractive = false,
38
- reservedDependencies = {
39
- require: true,
40
- module: true,
41
- exports: true
42
- },
43
- req, cfg = {}, currentlyAddingScript, s, head, baseElement, scripts, script,
44
- src, subPath, mainScript, dataMain, globalI, ctx, jQueryCheck, checkLoadedTimeoutId;
37
+ req, s, head, baseElement, dataMain, src,
38
+ interactiveScript, currentlyAddingScript, mainScript, subPath;
45
39
 
46
40
  function isFunction(it) {
47
- return ostring.call(it) === "[object Function]";
41
+ return ostring.call(it) === '[object Function]';
48
42
  }
49
43
 
50
44
  function isArray(it) {
51
- return ostring.call(it) === "[object Array]";
45
+ return ostring.call(it) === '[object Array]';
46
+ }
47
+
48
+ /**
49
+ * Helper function for iterating over an array. If the func returns
50
+ * a true value, it will break out of the loop.
51
+ */
52
+ function each(ary, func) {
53
+ if (ary) {
54
+ var i;
55
+ for (i = 0; i < ary.length; i += 1) {
56
+ if (ary[i] && func(ary[i], i, ary)) {
57
+ break;
58
+ }
59
+ }
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Helper function for iterating over an array backwards. If the func
65
+ * returns a true value, it will break out of the loop.
66
+ */
67
+ function eachReverse(ary, func) {
68
+ if (ary) {
69
+ var i;
70
+ for (i = ary.length - 1; i > -1; i -= 1) {
71
+ if (ary[i] && func(ary[i], i, ary)) {
72
+ break;
73
+ }
74
+ }
75
+ }
76
+ }
77
+
78
+ function hasProp(obj, prop) {
79
+ return obj.hasOwnProperty(prop);
80
+ }
81
+
82
+ /**
83
+ * Cycles over properties in an object and calls a function for each
84
+ * property value. If the function returns a truthy value, then the
85
+ * iteration is stopped.
86
+ */
87
+ function eachProp(obj, func) {
88
+ var prop;
89
+ for (prop in obj) {
90
+ if (obj.hasOwnProperty(prop)) {
91
+ if (func(obj[prop], prop)) {
92
+ break;
93
+ }
94
+ }
95
+ }
52
96
  }
53
97
 
54
98
  /**
@@ -58,13 +102,82 @@ var requirejs, require, define;
58
102
  * Object.prototype names, but the uses of mixin here seem unlikely to
59
103
  * trigger a problem related to that.
60
104
  */
61
- function mixin(target, source, force) {
62
- for (var prop in source) {
63
- if (!(prop in empty) && (!(prop in target) || force)) {
64
- target[prop] = source[prop];
65
- }
105
+ function mixin(target, source, force, deepStringMixin) {
106
+ if (source) {
107
+ eachProp(source, function (value, prop) {
108
+ if (force || !hasProp(target, prop)) {
109
+ if (deepStringMixin && typeof value !== 'string') {
110
+ if (!target[prop]) {
111
+ target[prop] = {};
112
+ }
113
+ mixin(target[prop], value, force, deepStringMixin);
114
+ } else {
115
+ target[prop] = value;
116
+ }
117
+ }
118
+ });
119
+ }
120
+ return target;
121
+ }
122
+
123
+ //Similar to Function.prototype.bind, but the 'this' object is specified
124
+ //first, since it is easier to read/figure out what 'this' will be.
125
+ function bind(obj, fn) {
126
+ return function () {
127
+ return fn.apply(obj, arguments);
128
+ };
129
+ }
130
+
131
+ function scripts() {
132
+ return document.getElementsByTagName('script');
133
+ }
134
+
135
+ //Allow getting a global that expressed in
136
+ //dot notation, like 'a.b.c'.
137
+ function getGlobal(value) {
138
+ if (!value) {
139
+ return value;
66
140
  }
67
- return req;
141
+ var g = global;
142
+ each(value.split('.'), function (part) {
143
+ g = g[part];
144
+ });
145
+ return g;
146
+ }
147
+
148
+ function makeContextModuleFunc(func, relMap, enableBuildCallback) {
149
+ return function () {
150
+ //A version of a require function that passes a moduleName
151
+ //value for items that may need to
152
+ //look up paths relative to the moduleName
153
+ var args = aps.call(arguments, 0), lastArg;
154
+ if (enableBuildCallback &&
155
+ isFunction((lastArg = args[args.length - 1]))) {
156
+ lastArg.__requireJsBuild = true;
157
+ }
158
+ args.push(relMap);
159
+ return func.apply(null, args);
160
+ };
161
+ }
162
+
163
+ function addRequireMethods(req, context, relMap) {
164
+ each([
165
+ ['toUrl'],
166
+ ['undef'],
167
+ ['defined', 'requireDefined'],
168
+ ['specified', 'requireSpecified']
169
+ ], function (item) {
170
+ var prop = item[1] || item[0];
171
+ req[item[0]] = context ? makeContextModuleFunc(context[prop], relMap) :
172
+ //If no context, then use default context. Reference from
173
+ //contexts instead of early binding to default context, so
174
+ //that during builds, the latest instance of the default
175
+ //context with its config gets used.
176
+ function () {
177
+ var ctx = contexts[defContextName];
178
+ return ctx[prop].apply(ctx, arguments);
179
+ };
180
+ });
68
181
  }
69
182
 
70
183
  /**
@@ -75,128 +188,60 @@ var requirejs, require, define;
75
188
  *
76
189
  * @returns {Error}
77
190
  */
78
- function makeError(id, msg, err) {
191
+ function makeError(id, msg, err, requireModules) {
79
192
  var e = new Error(msg + '\nhttp://requirejs.org/docs/errors.html#' + id);
193
+ e.requireType = id;
194
+ e.requireModules = requireModules;
80
195
  if (err) {
81
196
  e.originalError = err;
82
197
  }
83
198
  return e;
84
199
  }
85
200
 
86
- /**
87
- * Used to set up package paths from a packagePaths or packages config object.
88
- * @param {Object} pkgs the object to store the new package config
89
- * @param {Array} currentPackages an array of packages to configure
90
- * @param {String} [dir] a prefix dir to use.
91
- */
92
- function configurePackageDir(pkgs, currentPackages, dir) {
93
- var i, location, pkgObj;
94
-
95
- for (i = 0; (pkgObj = currentPackages[i]); i++) {
96
- pkgObj = typeof pkgObj === "string" ? { name: pkgObj } : pkgObj;
97
- location = pkgObj.location;
98
-
99
- //Add dir to the path, but avoid paths that start with a slash
100
- //or have a colon (indicates a protocol)
101
- if (dir && (!location || (location.indexOf("/") !== 0 && location.indexOf(":") === -1))) {
102
- location = dir + "/" + (location || pkgObj.name);
103
- }
104
-
105
- //Create a brand new object on pkgs, since currentPackages can
106
- //be passed in again, and config.pkgs is the internal transformed
107
- //state for all package configs.
108
- pkgs[pkgObj.name] = {
109
- name: pkgObj.name,
110
- location: location || pkgObj.name,
111
- //Remove leading dot in main, so main paths are normalized,
112
- //and remove any trailing .js, since different package
113
- //envs have different conventions: some use a module name,
114
- //some use a file name.
115
- main: (pkgObj.main || "main")
116
- .replace(currDirRegExp, '')
117
- .replace(jsSuffixRegExp, '')
118
- };
119
- }
120
- }
121
-
122
- /**
123
- * jQuery 1.4.3-1.5.x use a readyWait/ready() pairing to hold DOM
124
- * ready callbacks, but jQuery 1.6 supports a holdReady() API instead.
125
- * At some point remove the readyWait/ready() support and just stick
126
- * with using holdReady.
127
- */
128
- function jQueryHoldReady($, shouldHold) {
129
- if ($.holdReady) {
130
- $.holdReady(shouldHold);
131
- } else if (shouldHold) {
132
- $.readyWait += 1;
133
- } else {
134
- $.ready(true);
135
- }
136
- }
137
-
138
- if (typeof define !== "undefined") {
201
+ if (typeof define !== 'undefined') {
139
202
  //If a define is already in play via another AMD loader,
140
203
  //do not overwrite.
141
204
  return;
142
205
  }
143
206
 
144
- if (typeof requirejs !== "undefined") {
207
+ if (typeof requirejs !== 'undefined') {
145
208
  if (isFunction(requirejs)) {
146
209
  //Do not overwrite and existing requirejs instance.
147
210
  return;
148
- } else {
149
- cfg = requirejs;
150
- requirejs = undefined;
151
211
  }
212
+ cfg = requirejs;
213
+ requirejs = undefined;
152
214
  }
153
215
 
154
216
  //Allow for a require config object
155
- if (typeof require !== "undefined" && !isFunction(require)) {
217
+ if (typeof require !== 'undefined' && !isFunction(require)) {
156
218
  //assume it is a config object.
157
219
  cfg = require;
158
220
  require = undefined;
159
221
  }
160
222
 
161
- /**
162
- * Creates a new context for use in require and define calls.
163
- * Handle most of the heavy lifting. Do not want to use an object
164
- * with prototype here to avoid using "this" in require, in case it
165
- * needs to be used in more super secure envs that do not want this.
166
- * Also there should not be that many contexts in the page. Usually just
167
- * one for the default context, but could be extra for multiversion cases
168
- * or if a package needs a special context for a dependency that conflicts
169
- * with the standard context.
170
- */
171
223
  function newContext(contextName) {
172
- var context, resume,
173
- config = {
224
+ var config = {
174
225
  waitSeconds: 7,
175
- baseUrl: "./",
226
+ baseUrl: './',
176
227
  paths: {},
177
228
  pkgs: {},
178
- catchError: {}
229
+ shim: {}
179
230
  },
231
+ registry = {},
232
+ undefEvents = {},
180
233
  defQueue = [],
181
- specified = {
182
- "require": true,
183
- "exports": true,
184
- "module": true
185
- },
186
- urlMap = {},
187
234
  defined = {},
188
- loaded = {},
189
- waiting = {},
190
- waitAry = [],
191
235
  urlFetched = {},
192
- managerCounter = 0,
193
- managerCallbacks = {},
194
- plugins = {},
195
- //Used to indicate which modules in a build scenario
196
- //need to be full executed.
197
- needFullExec = {},
198
- fullExec = {},
199
- resumeDepth = 0;
236
+ requireCounter = 1,
237
+ unnormalizedCounter = 1,
238
+ //Used to track the order in which modules
239
+ //should be executed, by the order they
240
+ //load. Important for consistent cycle resolution
241
+ //behavior.
242
+ waitAry = [],
243
+ inCheckLoaded, Module, context, handlers,
244
+ checkLoadedTimeoutId;
200
245
 
201
246
  /**
202
247
  * Trims the . and .. from an array of path segments.
@@ -209,11 +254,12 @@ var requirejs, require, define;
209
254
  */
210
255
  function trimDots(ary) {
211
256
  var i, part;
212
- for (i = 0; (part = ary[i]); i++) {
213
- if (part === ".") {
257
+ for (i = 0; ary[i]; i+= 1) {
258
+ part = ary[i];
259
+ if (part === '.') {
214
260
  ary.splice(i, 1);
215
261
  i -= 1;
216
- } else if (part === "..") {
262
+ } else if (part === '..') {
217
263
  if (i === 1 && (ary[2] === '..' || ary[0] === '..')) {
218
264
  //End of the line. Keep at least one non-dot
219
265
  //path segment at the front so it can be mapped
@@ -236,13 +282,19 @@ var requirejs, require, define;
236
282
  * @param {String} name the relative name
237
283
  * @param {String} baseName a real name that the name arg is relative
238
284
  * to.
285
+ * @param {Boolean} applyMap apply the map config to the value. Should
286
+ * only be done if this normalization is for a dependency ID.
239
287
  * @returns {String} normalized name
240
288
  */
241
- function normalize(name, baseName) {
242
- var pkgName, pkgConfig;
289
+ function normalize(name, baseName, applyMap) {
290
+ var baseParts = baseName && baseName.split('/'),
291
+ map = config.map,
292
+ starMap = map && map['*'],
293
+ pkgName, pkgConfig, mapValue, nameParts, i, j, nameSegment,
294
+ foundMap;
243
295
 
244
296
  //Adjust any relative paths.
245
- if (name && name.charAt(0) === ".") {
297
+ if (name && name.charAt(0) === '.') {
246
298
  //If have a base name, try to normalize against it,
247
299
  //otherwise, assume it is a top-level require that will
248
300
  //be relative to baseUrl in the end.
@@ -250,36 +302,99 @@ var requirejs, require, define;
250
302
  if (config.pkgs[baseName]) {
251
303
  //If the baseName is a package name, then just treat it as one
252
304
  //name to concat the name with.
253
- baseName = [baseName];
305
+ baseParts = [baseName];
254
306
  } else {
255
307
  //Convert baseName to array, and lop off the last part,
256
- //so that . matches that "directory" and not name of the baseName's
257
- //module. For instance, baseName of "one/two/three", maps to
258
- //"one/two/three.js", but we want the directory, "one/two" for
308
+ //so that . matches that 'directory' and not name of the baseName's
309
+ //module. For instance, baseName of 'one/two/three', maps to
310
+ //'one/two/three.js', but we want the directory, 'one/two' for
259
311
  //this normalization.
260
- baseName = baseName.split("/");
261
- baseName = baseName.slice(0, baseName.length - 1);
312
+ baseParts = baseParts.slice(0, baseParts.length - 1);
262
313
  }
263
314
 
264
- name = baseName.concat(name.split("/"));
315
+ name = baseParts.concat(name.split('/'));
265
316
  trimDots(name);
266
317
 
267
318
  //Some use of packages may use a . path to reference the
268
- //"main" module name, so normalize for that.
319
+ //'main' module name, so normalize for that.
269
320
  pkgConfig = config.pkgs[(pkgName = name[0])];
270
- name = name.join("/");
321
+ name = name.join('/');
271
322
  if (pkgConfig && name === pkgName + '/' + pkgConfig.main) {
272
323
  name = pkgName;
273
324
  }
274
- } else if (name.indexOf("./") === 0) {
325
+ } else if (name.indexOf('./') === 0) {
275
326
  // No baseName, so this is ID is resolved relative
276
327
  // to baseUrl, pull off the leading dot.
277
328
  name = name.substring(2);
278
329
  }
279
330
  }
331
+
332
+ //Apply map config if available.
333
+ if (applyMap && (baseParts || starMap) && map) {
334
+ nameParts = name.split('/');
335
+
336
+ for (i = nameParts.length; i > 0; i -= 1) {
337
+ nameSegment = nameParts.slice(0, i).join('/');
338
+
339
+ if (baseParts) {
340
+ //Find the longest baseName segment match in the config.
341
+ //So, do joins on the biggest to smallest lengths of baseParts.
342
+ for (j = baseParts.length; j > 0; j -= 1) {
343
+ mapValue = map[baseParts.slice(0, j).join('/')];
344
+
345
+ //baseName segment has config, find if it has one for
346
+ //this name.
347
+ if (mapValue) {
348
+ mapValue = mapValue[nameSegment];
349
+ if (mapValue) {
350
+ //Match, update name to the new value.
351
+ foundMap = mapValue;
352
+ break;
353
+ }
354
+ }
355
+ }
356
+ }
357
+
358
+ if (!foundMap && starMap && starMap[nameSegment]) {
359
+ foundMap = starMap[nameSegment];
360
+ }
361
+
362
+ if (foundMap) {
363
+ nameParts.splice(0, i, foundMap);
364
+ name = nameParts.join('/');
365
+ break;
366
+ }
367
+ }
368
+ }
369
+
280
370
  return name;
281
371
  }
282
372
 
373
+ function removeScript(name) {
374
+ if (isBrowser) {
375
+ each(scripts(), function (scriptNode) {
376
+ if (scriptNode.getAttribute('data-requiremodule') === name &&
377
+ scriptNode.getAttribute('data-requirecontext') === context.contextName) {
378
+ scriptNode.parentNode.removeChild(scriptNode);
379
+ return true;
380
+ }
381
+ });
382
+ }
383
+ }
384
+
385
+ function hasPathFallback(id) {
386
+ var pathConfig = config.paths[id];
387
+ if (pathConfig && isArray(pathConfig) && pathConfig.length > 1) {
388
+ removeScript(id);
389
+ //Pop off the first array value, since it failed, and
390
+ //retry
391
+ pathConfig.shift();
392
+ context.undef(id);
393
+ context.require([id]);
394
+ return true;
395
+ }
396
+ }
397
+
283
398
  /**
284
399
  * Creates a module mapping that includes plugin prefix, module
285
400
  * name, and path. If parentModuleMap is provided it will
@@ -288,15 +403,28 @@ var requirejs, require, define;
288
403
  * @param {String} name the module name
289
404
  * @param {String} [parentModuleMap] parent module map
290
405
  * for the module name, used to resolve relative names.
406
+ * @param {Boolean} isNormalized: is the ID already normalized.
407
+ * This is true if this call is done for a define() module ID.
408
+ * @param {Boolean} applyMap: apply the map config to the ID.
409
+ * Should only be true if this map is for a dependency.
291
410
  *
292
411
  * @returns {Object}
293
412
  */
294
- function makeModuleMap(name, parentModuleMap) {
295
- var index = name ? name.indexOf("!") : -1,
413
+ function makeModuleMap(name, parentModuleMap, isNormalized, applyMap) {
414
+ var index = name ? name.indexOf('!') : -1,
296
415
  prefix = null,
297
416
  parentName = parentModuleMap ? parentModuleMap.name : null,
298
417
  originalName = name,
299
- normalizedName, url, pluginModule;
418
+ isDefine = true,
419
+ normalizedName = '',
420
+ url, pluginModule, suffix;
421
+
422
+ //If no name, then it means it is a require call, generate an
423
+ //internal name.
424
+ if (!name) {
425
+ isDefine = false;
426
+ name = '_@r' + (requireCounter += 1);
427
+ }
300
428
 
301
429
  if (index !== -1) {
302
430
  prefix = name.substring(0, index);
@@ -304,1015 +432,947 @@ var requirejs, require, define;
304
432
  }
305
433
 
306
434
  if (prefix) {
307
- prefix = normalize(prefix, parentName);
435
+ prefix = normalize(prefix, parentName, applyMap);
436
+ pluginModule = defined[prefix];
308
437
  }
309
438
 
310
439
  //Account for relative paths if there is a base name.
311
440
  if (name) {
312
441
  if (prefix) {
313
- pluginModule = defined[prefix];
314
442
  if (pluginModule && pluginModule.normalize) {
315
443
  //Plugin is loaded, use its normalize method.
316
444
  normalizedName = pluginModule.normalize(name, function (name) {
317
- return normalize(name, parentName);
445
+ return normalize(name, parentName, applyMap);
318
446
  });
319
447
  } else {
320
- normalizedName = normalize(name, parentName);
448
+ normalizedName = normalize(name, parentName, applyMap);
321
449
  }
322
450
  } else {
323
451
  //A regular module.
324
- normalizedName = normalize(name, parentName);
325
-
326
- url = urlMap[normalizedName];
327
- if (!url) {
328
- //Calculate url for the module, if it has a name.
329
- //Use name here since nameToUrl also calls normalize,
330
- //and for relative names that are outside the baseUrl
331
- //this causes havoc. Was thinking of just removing
332
- //parentModuleMap to avoid extra normalization, but
333
- //normalize() still does a dot removal because of
334
- //issue #142, so just pass in name here and redo
335
- //the normalization. Paths outside baseUrl are just
336
- //messy to support.
337
- url = context.nameToUrl(name, null, parentModuleMap);
338
-
339
- //Store the URL mapping for later.
340
- urlMap[normalizedName] = url;
341
- }
452
+ normalizedName = normalize(name, parentName, applyMap);
453
+
454
+ //Calculate url for the module, if it has a name.
455
+ //Use name here since nameToUrl also calls normalize,
456
+ //and for relative names that are outside the baseUrl
457
+ //this causes havoc. Was thinking of just removing
458
+ //parentModuleMap to avoid extra normalization, but
459
+ //normalize() still does a dot removal because of
460
+ //issue #142, so just pass in name here and redo
461
+ //the normalization. Paths outside baseUrl are just
462
+ //messy to support.
463
+ url = context.nameToUrl(name, null, parentModuleMap);
342
464
  }
343
465
  }
344
466
 
467
+ //If the id is a plugin id that cannot be determined if it needs
468
+ //normalization, stamp it with a unique ID so two matching relative
469
+ //ids that may conflict can be separate.
470
+ suffix = prefix && !pluginModule && !isNormalized ?
471
+ '_unnormalized' + (unnormalizedCounter += 1) :
472
+ '';
473
+
345
474
  return {
346
475
  prefix: prefix,
347
476
  name: normalizedName,
348
477
  parentMap: parentModuleMap,
478
+ unnormalized: !!suffix,
349
479
  url: url,
350
480
  originalName: originalName,
351
- fullName: prefix ? prefix + "!" + (normalizedName || '') : normalizedName
481
+ isDefine: isDefine,
482
+ id: (prefix ?
483
+ prefix + '!' + normalizedName :
484
+ normalizedName) + suffix
352
485
  };
353
486
  }
354
487
 
355
- /**
356
- * Determine if priority loading is done. If so clear the priorityWait
357
- */
358
- function isPriorityDone() {
359
- var priorityDone = true,
360
- priorityWait = config.priorityWait,
361
- priorityName, i;
362
- if (priorityWait) {
363
- for (i = 0; (priorityName = priorityWait[i]); i++) {
364
- if (!loaded[priorityName]) {
365
- priorityDone = false;
366
- break;
367
- }
368
- }
369
- if (priorityDone) {
370
- delete config.priorityWait;
371
- }
488
+ function getModule(depMap) {
489
+ var id = depMap.id,
490
+ mod = registry[id];
491
+
492
+ if (!mod) {
493
+ mod = registry[id] = new context.Module(depMap);
372
494
  }
373
- return priorityDone;
374
- }
375
495
 
376
- function makeContextModuleFunc(func, relModuleMap, enableBuildCallback) {
377
- return function () {
378
- //A version of a require function that passes a moduleName
379
- //value for items that may need to
380
- //look up paths relative to the moduleName
381
- var args = aps.call(arguments, 0), lastArg;
382
- if (enableBuildCallback &&
383
- isFunction((lastArg = args[args.length - 1]))) {
384
- lastArg.__requireJsBuild = true;
385
- }
386
- args.push(relModuleMap);
387
- return func.apply(null, args);
388
- };
496
+ return mod;
389
497
  }
390
498
 
391
- /**
392
- * Helper function that creates a require function object to give to
393
- * modules that ask for it as a dependency. It needs to be specific
394
- * per module because of the implication of path mappings that may
395
- * need to be relative to the module name.
396
- */
397
- function makeRequire(relModuleMap, enableBuildCallback, altRequire) {
398
- var modRequire = makeContextModuleFunc(altRequire || context.require, relModuleMap, enableBuildCallback);
399
-
400
- mixin(modRequire, {
401
- nameToUrl: makeContextModuleFunc(context.nameToUrl, relModuleMap),
402
- toUrl: makeContextModuleFunc(context.toUrl, relModuleMap),
403
- defined: makeContextModuleFunc(context.requireDefined, relModuleMap),
404
- specified: makeContextModuleFunc(context.requireSpecified, relModuleMap),
405
- isBrowser: req.isBrowser
406
- });
407
- return modRequire;
408
- }
499
+ function on(depMap, name, fn) {
500
+ var id = depMap.id,
501
+ mod = registry[id];
409
502
 
410
- /*
411
- * Queues a dependency for checking after the loader is out of a
412
- * "paused" state, for example while a script file is being loaded
413
- * in the browser, where it may have many modules defined in it.
414
- */
415
- function queueDependency(manager) {
416
- context.paused.push(manager);
503
+ if (hasProp(defined, id) &&
504
+ (!mod || mod.defineEmitComplete)) {
505
+ if (name === 'defined') {
506
+ fn(defined[id]);
507
+ }
508
+ } else {
509
+ getModule(depMap).on(name, fn);
510
+ }
417
511
  }
418
512
 
419
- function execManager(manager) {
420
- var i, ret, err, errFile, errModuleTree,
421
- cb = manager.callback,
422
- map = manager.map,
423
- fullName = map.fullName,
424
- args = manager.deps,
425
- listeners = manager.listeners,
426
- execCb = config.requireExecCb || req.execCb,
427
- cjsModule;
428
-
429
- //Call the callback to define the module, if necessary.
430
- if (cb && isFunction(cb)) {
431
- if (config.catchError.define) {
432
- try {
433
- ret = execCb(fullName, manager.callback, args, defined[fullName]);
434
- } catch (e) {
435
- err = e;
436
- }
437
- } else {
438
- ret = execCb(fullName, manager.callback, args, defined[fullName]);
439
- }
513
+ function onError(err, errback) {
514
+ var ids = err.requireModules,
515
+ notified = false;
440
516
 
441
- if (fullName) {
442
- //If setting exports via "module" is in play,
443
- //favor that over return value and exports. After that,
444
- //favor a non-undefined return value over exports use.
445
- cjsModule = manager.cjsModule;
446
- if (cjsModule &&
447
- cjsModule.exports !== undefined &&
448
- //Make sure it is not already the exports value
449
- cjsModule.exports !== defined[fullName]) {
450
- ret = defined[fullName] = manager.cjsModule.exports;
451
- } else if (ret === undefined && manager.usingExports) {
452
- //exports already set the defined value.
453
- ret = defined[fullName];
454
- } else {
455
- //Use the return value from the function.
456
- defined[fullName] = ret;
457
- //If this module needed full execution in a build
458
- //environment, mark that now.
459
- if (needFullExec[fullName]) {
460
- fullExec[fullName] = true;
517
+ if (errback) {
518
+ errback(err);
519
+ } else {
520
+ each(ids, function (id) {
521
+ var mod = registry[id];
522
+ if (mod) {
523
+ //Set error on module, so it skips timeout checks.
524
+ mod.error = err;
525
+ if (mod.events.error) {
526
+ notified = true;
527
+ mod.emit('error', err);
461
528
  }
462
529
  }
463
- }
464
- } else if (fullName) {
465
- //May just be an object definition for the module. Only
466
- //worry about defining if have a module name.
467
- ret = defined[fullName] = cb;
468
-
469
- //If this module needed full execution in a build
470
- //environment, mark that now.
471
- if (needFullExec[fullName]) {
472
- fullExec[fullName] = true;
530
+ });
531
+
532
+ if (!notified) {
533
+ req.onError(err);
473
534
  }
474
535
  }
536
+ }
475
537
 
476
- //Clean up waiting. Do this before error calls, and before
477
- //calling back listeners, so that bookkeeping is correct
478
- //in the event of an error and error is reported in correct order,
479
- //since the listeners will likely have errors if the
480
- //onError function does not throw.
481
- if (waiting[manager.id]) {
482
- delete waiting[manager.id];
483
- manager.isDone = true;
484
- context.waitCount -= 1;
485
- if (context.waitCount === 0) {
486
- //Clear the wait array used for cycles.
487
- waitAry = [];
488
- }
538
+ /**
539
+ * Internal method to transfer globalQueue items to this context's
540
+ * defQueue.
541
+ */
542
+ function takeGlobalQueue() {
543
+ //Push all the globalDefQueue items into the context's defQueue
544
+ if (globalDefQueue.length) {
545
+ //Array splice in the values since the context code has a
546
+ //local var ref to defQueue, so cannot just reassign the one
547
+ //on context.
548
+ apsp.apply(defQueue,
549
+ [defQueue.length - 1, 0].concat(globalDefQueue));
550
+ globalDefQueue = [];
489
551
  }
552
+ }
490
553
 
491
- //Do not need to track manager callback now that it is defined.
492
- delete managerCallbacks[fullName];
554
+ /**
555
+ * Helper function that creates a require function object to give to
556
+ * modules that ask for it as a dependency. It needs to be specific
557
+ * per module because of the implication of path mappings that may
558
+ * need to be relative to the module name.
559
+ */
560
+ function makeRequire(mod, enableBuildCallback, altRequire) {
561
+ var relMap = mod && mod.map,
562
+ modRequire = makeContextModuleFunc(altRequire || context.require,
563
+ relMap,
564
+ enableBuildCallback);
493
565
 
494
- //Allow instrumentation like the optimizer to know the order
495
- //of modules executed and their dependencies.
496
- if (req.onResourceLoad && !manager.placeholder) {
497
- req.onResourceLoad(context, map, manager.depArray);
498
- }
566
+ addRequireMethods(modRequire, context, relMap);
567
+ modRequire.isBrowser = isBrowser;
499
568
 
500
- if (err) {
501
- errFile = (fullName ? makeModuleMap(fullName).url : '') ||
502
- err.fileName || err.sourceURL;
503
- errModuleTree = err.moduleTree;
504
- err = makeError('defineerror', 'Error evaluating ' +
505
- 'module "' + fullName + '" at location "' +
506
- errFile + '":\n' +
507
- err + '\nfileName:' + errFile +
508
- '\nlineNumber: ' + (err.lineNumber || err.line), err);
509
- err.moduleName = fullName;
510
- err.moduleTree = errModuleTree;
511
- return req.onError(err);
512
- }
569
+ return modRequire;
570
+ }
513
571
 
514
- //Let listeners know of this manager's value.
515
- for (i = 0; (cb = listeners[i]); i++) {
516
- cb(ret);
572
+ handlers = {
573
+ 'require': function (mod) {
574
+ return makeRequire(mod);
575
+ },
576
+ 'exports': function (mod) {
577
+ mod.usingExports = true;
578
+ if (mod.map.isDefine) {
579
+ return (mod.exports = defined[mod.map.id] = {});
580
+ }
581
+ },
582
+ 'module': function (mod) {
583
+ return (mod.module = {
584
+ id: mod.map.id,
585
+ uri: mod.map.url,
586
+ config: function () {
587
+ return (config.config && config.config[mod.map.id]) || {};
588
+ },
589
+ exports: defined[mod.map.id]
590
+ });
517
591
  }
592
+ };
518
593
 
519
- return undefined;
520
- }
594
+ function removeWaiting(id) {
595
+ //Clean up machinery used for waiting modules.
596
+ delete registry[id];
521
597
 
522
- /**
523
- * Helper that creates a callack function that is called when a dependency
524
- * is ready, and sets the i-th dependency for the manager as the
525
- * value passed to the callback generated by this function.
526
- */
527
- function makeArgCallback(manager, i) {
528
- return function (value) {
529
- //Only do the work if it has not been done
530
- //already for a dependency. Cycle breaking
531
- //logic in forceExec could mean this function
532
- //is called more than once for a given dependency.
533
- if (!manager.depDone[i]) {
534
- manager.depDone[i] = true;
535
- manager.deps[i] = value;
536
- manager.depCount -= 1;
537
- if (!manager.depCount) {
538
- //All done, execute!
539
- execManager(manager);
598
+ each(waitAry, function (mod, i) {
599
+ if (mod.map.id === id) {
600
+ waitAry.splice(i, 1);
601
+ if (!mod.defined) {
602
+ context.waitCount -= 1;
540
603
  }
604
+ return true;
541
605
  }
542
- };
606
+ });
543
607
  }
544
608
 
545
- function callPlugin(pluginName, depManager) {
546
- var map = depManager.map,
547
- fullName = map.fullName,
548
- name = map.name,
549
- plugin = plugins[pluginName] ||
550
- (plugins[pluginName] = defined[pluginName]),
551
- load;
552
-
553
- //No need to continue if the manager is already
554
- //in the process of loading.
555
- if (depManager.loading) {
609
+ function findCycle(mod, traced) {
610
+ var id = mod.map.id,
611
+ depArray = mod.depMaps,
612
+ foundModule;
613
+
614
+ //Do not bother with unitialized modules or not yet enabled
615
+ //modules.
616
+ if (!mod.inited) {
556
617
  return;
557
618
  }
558
- depManager.loading = true;
559
619
 
560
- load = function (ret) {
561
- depManager.callback = function () {
562
- return ret;
563
- };
564
- execManager(depManager);
565
-
566
- loaded[depManager.id] = true;
567
-
568
- //The loading of this plugin
569
- //might have placed other things
570
- //in the paused queue. In particular,
571
- //a loader plugin that depends on
572
- //a different plugin loaded resource.
573
- resume();
574
- };
620
+ //Found the cycle.
621
+ if (traced[id]) {
622
+ return mod;
623
+ }
575
624
 
576
- //Allow plugins to load other code without having to know the
577
- //context or how to "complete" the load.
578
- load.fromText = function (moduleName, text) {
579
- /*jslint evil: true */
580
- var hasInteractive = useInteractive;
625
+ traced[id] = true;
581
626
 
582
- //Indicate a the module is in process of loading.
583
- loaded[moduleName] = false;
584
- context.scriptCount += 1;
627
+ //Trace through the dependencies.
628
+ each(depArray, function (depMap) {
629
+ var depId = depMap.id,
630
+ depMod = registry[depId];
585
631
 
586
- //Indicate this is not a "real" module, so do not track it
587
- //for builds, it does not map to a real file.
588
- context.fake[moduleName] = true;
632
+ if (!depMod) {
633
+ return;
634
+ }
589
635
 
590
- //Turn off interactive script matching for IE for any define
591
- //calls in the text, then turn it back on at the end.
592
- if (hasInteractive) {
593
- useInteractive = false;
636
+ if (!depMod.inited || !depMod.enabled) {
637
+ //Dependency is not inited, so this cannot
638
+ //be used to determine a cycle.
639
+ foundModule = null;
640
+ delete traced[id];
641
+ return true;
594
642
  }
595
643
 
596
- req.exec(text);
644
+ //mixin traced to a new object for each dependency, so that
645
+ //sibling dependencies in this object to not generate a
646
+ //false positive match on a cycle. Ideally an Object.create
647
+ //type of prototype delegation would be used here, but
648
+ //optimizing for file size vs. execution speed since hopefully
649
+ //the trees are small for circular dependency scans relative
650
+ //to the full app perf.
651
+ return (foundModule = findCycle(depMod, mixin({}, traced)));
652
+ });
597
653
 
598
- if (hasInteractive) {
599
- useInteractive = true;
600
- }
654
+ return foundModule;
655
+ }
601
656
 
602
- //Support anonymous modules.
603
- context.completeLoad(moduleName);
604
- };
657
+ function forceExec(mod, traced, uninited) {
658
+ var id = mod.map.id,
659
+ depArray = mod.depMaps;
605
660
 
606
- //No need to continue if the plugin value has already been
607
- //defined by a build.
608
- if (fullName in defined) {
609
- load(defined[fullName]);
610
- } else {
611
- //Use parentName here since the plugin's name is not reliable,
612
- //could be some weird string with no path that actually wants to
613
- //reference the parentName's path.
614
- plugin.load(name, makeRequire(map.parentMap, true, function (deps, cb) {
615
- var moduleDeps = [],
616
- i, dep, depMap;
617
- //Convert deps to full names and hold on to them
618
- //for reference later, when figuring out if they
619
- //are blocked by a circular dependency.
620
- for (i = 0; (dep = deps[i]); i++) {
621
- depMap = makeModuleMap(dep, map.parentMap);
622
- deps[i] = depMap.fullName;
623
- if (!depMap.prefix) {
624
- moduleDeps.push(deps[i]);
625
- }
626
- }
627
- depManager.moduleDeps = (depManager.moduleDeps || []).concat(moduleDeps);
628
- return context.require(deps, cb);
629
- }), load, config);
661
+ if (!mod.inited || !mod.map.isDefine) {
662
+ return;
630
663
  }
631
- }
632
664
 
633
- /**
634
- * Adds the manager to the waiting queue. Only fully
635
- * resolved items should be in the waiting queue.
636
- */
637
- function addWait(manager) {
638
- if (!waiting[manager.id]) {
639
- waiting[manager.id] = manager;
640
- waitAry.push(manager);
641
- context.waitCount += 1;
665
+ if (traced[id]) {
666
+ return defined[id];
642
667
  }
643
- }
644
668
 
645
- /**
646
- * Function added to every manager object. Created out here
647
- * to avoid new function creation for each manager instance.
648
- */
649
- function managerAdd(cb) {
650
- this.listeners.push(cb);
651
- }
669
+ traced[id] = mod;
652
670
 
653
- function getManager(map, shouldQueue) {
654
- var fullName = map.fullName,
655
- prefix = map.prefix,
656
- plugin = prefix ? plugins[prefix] ||
657
- (plugins[prefix] = defined[prefix]) : null,
658
- manager, created, pluginManager, prefixMap;
671
+ each(depArray, function(depMap) {
672
+ var depId = depMap.id,
673
+ depMod = registry[depId],
674
+ value;
659
675
 
660
- if (fullName) {
661
- manager = managerCallbacks[fullName];
662
- }
676
+ if (handlers[depId]) {
677
+ return;
678
+ }
663
679
 
664
- if (!manager) {
665
- created = true;
666
- manager = {
667
- //ID is just the full name, but if it is a plugin resource
668
- //for a plugin that has not been loaded,
669
- //then add an ID counter to it.
670
- id: (prefix && !plugin ?
671
- (managerCounter++) + '__p@:' : '') +
672
- (fullName || '__r@' + (managerCounter++)),
673
- map: map,
674
- depCount: 0,
675
- depDone: [],
676
- depCallbacks: [],
677
- deps: [],
678
- listeners: [],
679
- add: managerAdd
680
- };
680
+ if (depMod) {
681
+ if (!depMod.inited || !depMod.enabled) {
682
+ //Dependency is not inited,
683
+ //so this module cannot be
684
+ //given a forced value yet.
685
+ uninited[id] = true;
686
+ return;
687
+ }
681
688
 
682
- specified[manager.id] = true;
689
+ //Get the value for the current dependency
690
+ value = forceExec(depMod, traced, uninited);
683
691
 
684
- //Only track the manager/reuse it if this is a non-plugin
685
- //resource. Also only track plugin resources once
686
- //the plugin has been loaded, and so the fullName is the
687
- //true normalized value.
688
- if (fullName && (!prefix || plugins[prefix])) {
689
- managerCallbacks[fullName] = manager;
692
+ //Even with forcing it may not be done,
693
+ //in particular if the module is waiting
694
+ //on a plugin resource.
695
+ if (!uninited[depId]) {
696
+ mod.defineDepById(depId, value);
697
+ }
690
698
  }
691
- }
699
+ });
692
700
 
693
- //If there is a plugin needed, but it is not loaded,
694
- //first load the plugin, then continue on.
695
- if (prefix && !plugin) {
696
- prefixMap = makeModuleMap(prefix);
697
-
698
- //Clear out defined and urlFetched if the plugin was previously
699
- //loaded/defined, but not as full module (as in a build
700
- //situation). However, only do this work if the plugin is in
701
- //defined but does not have a module export value.
702
- if (prefix in defined && !defined[prefix]) {
703
- delete defined[prefix];
704
- delete urlFetched[prefixMap.url];
705
- }
701
+ mod.check(true);
706
702
 
707
- pluginManager = getManager(prefixMap, true);
708
- pluginManager.add(function (plugin) {
709
- //Create a new manager for the normalized
710
- //resource ID and have it call this manager when
711
- //done.
712
- var newMap = makeModuleMap(map.originalName, map.parentMap),
713
- normalizedManager = getManager(newMap, true);
714
-
715
- //Indicate this manager is a placeholder for the real,
716
- //normalized thing. Important for when trying to map
717
- //modules and dependencies, for instance, in a build.
718
- manager.placeholder = true;
719
-
720
- normalizedManager.add(function (resource) {
721
- manager.callback = function () {
722
- return resource;
723
- };
724
- execManager(manager);
725
- });
726
- });
727
- } else if (created && shouldQueue) {
728
- //Indicate the resource is not loaded yet if it is to be
729
- //queued.
730
- loaded[manager.id] = false;
731
- queueDependency(manager);
732
- addWait(manager);
733
- }
703
+ return defined[id];
704
+ }
734
705
 
735
- return manager;
706
+ function modCheck(mod) {
707
+ mod.check();
736
708
  }
737
709
 
738
- function main(inName, depArray, callback, relModuleMap) {
739
- var moduleMap = makeModuleMap(inName, relModuleMap),
740
- name = moduleMap.name,
741
- fullName = moduleMap.fullName,
742
- manager = getManager(moduleMap),
743
- id = manager.id,
744
- deps = manager.deps,
745
- i, depArg, depName, depPrefix, cjsMod;
746
-
747
- if (fullName) {
748
- //If module already defined for context, or already loaded,
749
- //then leave. Also leave if jQuery is registering but it does
750
- //not match the desired version number in the config.
751
- if (fullName in defined || loaded[id] === true ||
752
- (fullName === "jquery" && config.jQuery &&
753
- config.jQuery !== callback().fn.jquery)) {
754
- return;
755
- }
710
+ function checkLoaded() {
711
+ var waitInterval = config.waitSeconds * 1000,
712
+ //It is possible to disable the wait interval by using waitSeconds of 0.
713
+ expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(),
714
+ noLoads = [],
715
+ stillLoading = false,
716
+ needCycleCheck = true,
717
+ map, modId, err, usingPathFallback;
756
718
 
757
- //Set specified/loaded here for modules that are also loaded
758
- //as part of a layer, where onScriptLoad is not fired
759
- //for those cases. Do this after the inline define and
760
- //dependency tracing is done.
761
- specified[id] = true;
762
- loaded[id] = true;
719
+ //Do not bother if this call was a result of a cycle break.
720
+ if (inCheckLoaded) {
721
+ return;
722
+ }
723
+
724
+ inCheckLoaded = true;
763
725
 
764
- //If module is jQuery set up delaying its dom ready listeners.
765
- if (fullName === "jquery" && callback) {
766
- jQueryCheck(callback());
726
+ //Figure out the state of all the modules.
727
+ eachProp(registry, function (mod) {
728
+ map = mod.map;
729
+ modId = map.id;
730
+
731
+ //Skip things that are not enabled or in error state.
732
+ if (!mod.enabled) {
733
+ return;
767
734
  }
768
- }
769
735
 
770
- //Attach real depArray and callback to the manager. Do this
771
- //only if the module has not been defined already, so do this after
772
- //the fullName checks above. IE can call main() more than once
773
- //for a module.
774
- manager.depArray = depArray;
775
- manager.callback = callback;
776
-
777
- //Add the dependencies to the deps field, and register for callbacks
778
- //on the dependencies.
779
- for (i = 0; i < depArray.length; i++) {
780
- depArg = depArray[i];
781
- //There could be cases like in IE, where a trailing comma will
782
- //introduce a null dependency, so only treat a real dependency
783
- //value as a dependency.
784
- if (depArg) {
785
- //Split the dependency name into plugin and name parts
786
- depArg = makeModuleMap(depArg, (name ? moduleMap : relModuleMap));
787
- depName = depArg.fullName;
788
- depPrefix = depArg.prefix;
789
-
790
- //Fix the name in depArray to be just the name, since
791
- //that is how it will be called back later.
792
- depArray[i] = depName;
793
-
794
- //Fast path CommonJS standard dependencies.
795
- if (depName === "require") {
796
- deps[i] = makeRequire(moduleMap);
797
- } else if (depName === "exports") {
798
- //CommonJS module spec 1.1
799
- deps[i] = defined[fullName] = {};
800
- manager.usingExports = true;
801
- } else if (depName === "module") {
802
- //CommonJS module spec 1.1
803
- manager.cjsModule = cjsMod = deps[i] = {
804
- id: name,
805
- uri: name ? context.nameToUrl(name, null, relModuleMap) : undefined,
806
- exports: defined[fullName]
807
- };
808
- } else if (depName in defined && !(depName in waiting) &&
809
- (!(fullName in needFullExec) ||
810
- (fullName in needFullExec && fullExec[depName]))) {
811
- //Module already defined, and not in a build situation
812
- //where the module is a something that needs full
813
- //execution and this dependency has not been fully
814
- //executed. See r.js's requirePatch.js for more info
815
- //on fullExec.
816
- deps[i] = defined[depName];
817
- } else {
818
- //Mark this dependency as needing full exec if
819
- //the current module needs full exec.
820
- if (fullName in needFullExec) {
821
- needFullExec[depName] = true;
822
- //Reset state so fully executed code will get
823
- //picked up correctly.
824
- delete defined[depName];
825
- urlFetched[depArg.url] = false;
736
+ if (!mod.error) {
737
+ //If the module should be executed, and it has not
738
+ //been inited and time is up, remember it.
739
+ if (!mod.inited && expired) {
740
+ if (hasPathFallback(modId)) {
741
+ usingPathFallback = true;
742
+ stillLoading = true;
743
+ } else {
744
+ noLoads.push(modId);
745
+ removeScript(modId);
746
+ }
747
+ } else if (!mod.inited && mod.fetched && map.isDefine) {
748
+ stillLoading = true;
749
+ if (!map.prefix) {
750
+ //No reason to keep looking for unfinished
751
+ //loading. If the only stillLoading is a
752
+ //plugin resource though, keep going,
753
+ //because it may be that a plugin resource
754
+ //is waiting on a non-plugin cycle.
755
+ return (needCycleCheck = false);
826
756
  }
827
-
828
- //Either a resource that is not loaded yet, or a plugin
829
- //resource for either a plugin that has not
830
- //loaded yet.
831
- manager.depCount += 1;
832
- manager.depCallbacks[i] = makeArgCallback(manager, i);
833
- getManager(depArg, true).add(manager.depCallbacks[i]);
834
757
  }
835
758
  }
836
- }
759
+ });
837
760
 
838
- //Do not bother tracking the manager if it is all done.
839
- if (!manager.depCount) {
840
- //All done, execute!
841
- execManager(manager);
842
- } else {
843
- addWait(manager);
761
+ if (expired && noLoads.length) {
762
+ //If wait time expired, throw error of unloaded modules.
763
+ err = makeError('timeout', 'Load timeout for modules: ' + noLoads, null, noLoads);
764
+ err.contextName = context.contextName;
765
+ return onError(err);
844
766
  }
845
- }
846
767
 
847
- /**
848
- * Convenience method to call main for a define call that was put on
849
- * hold in the defQueue.
850
- */
851
- function callDefMain(args) {
852
- main.apply(null, args);
853
- }
768
+ //Not expired, check for a cycle.
769
+ if (needCycleCheck) {
854
770
 
855
- /**
856
- * jQuery 1.4.3+ supports ways to hold off calling
857
- * calling jQuery ready callbacks until all scripts are loaded. Be sure
858
- * to track it if the capability exists.. Also, since jQuery 1.4.3 does
859
- * not register as a module, need to do some global inference checking.
860
- * Even if it does register as a module, not guaranteed to be the precise
861
- * name of the global. If a jQuery is tracked for this context, then go
862
- * ahead and register it as a module too, if not already in process.
863
- */
864
- jQueryCheck = function (jqCandidate) {
865
- if (!context.jQuery) {
866
- var $ = jqCandidate || (typeof jQuery !== "undefined" ? jQuery : null);
867
-
868
- if ($) {
869
- //If a specific version of jQuery is wanted, make sure to only
870
- //use this jQuery if it matches.
871
- if (config.jQuery && $.fn.jquery !== config.jQuery) {
771
+ each(waitAry, function (mod) {
772
+ if (mod.defined) {
872
773
  return;
873
774
  }
874
775
 
875
- if ("holdReady" in $ || "readyWait" in $) {
876
- context.jQuery = $;
877
-
878
- //Manually create a "jquery" module entry if not one already
879
- //or in process. Note this could trigger an attempt at
880
- //a second jQuery registration, but does no harm since
881
- //the first one wins, and it is the same value anyway.
882
- callDefMain(["jquery", [], function () {
883
- return jQuery;
884
- }]);
885
-
886
- //Ask jQuery to hold DOM ready callbacks.
887
- if (context.scriptCount) {
888
- jQueryHoldReady($, true);
889
- context.jQueryIncremented = true;
890
- }
891
- }
892
- }
893
- }
894
- };
776
+ var cycleMod = findCycle(mod, {}),
777
+ traced = {};
895
778
 
896
- function findCycle(manager, traced) {
897
- var fullName = manager.map.fullName,
898
- depArray = manager.depArray,
899
- fullyLoaded = true,
900
- i, depName, depManager, result;
779
+ if (cycleMod) {
780
+ forceExec(cycleMod, traced, {});
901
781
 
902
- if (manager.isDone || !fullName || !loaded[fullName]) {
903
- return result;
904
- }
782
+ //traced modules may have been
783
+ //removed from the registry, but
784
+ //their listeners still need to
785
+ //be called.
786
+ eachProp(traced, modCheck);
787
+ }
788
+ });
905
789
 
906
- //Found the cycle.
907
- if (traced[fullName]) {
908
- return manager;
790
+ //Now that dependencies have
791
+ //been satisfied, trigger the
792
+ //completion check that then
793
+ //notifies listeners.
794
+ eachProp(registry, modCheck);
909
795
  }
910
796
 
911
- traced[fullName] = true;
912
-
913
- //Trace through the dependencies.
914
- if (depArray) {
915
- for (i = 0; i < depArray.length; i++) {
916
- //Some array members may be null, like if a trailing comma
917
- //IE, so do the explicit [i] access and check if it has a value.
918
- depName = depArray[i];
919
- if (!loaded[depName] && !reservedDependencies[depName]) {
920
- fullyLoaded = false;
921
- break;
922
- }
923
- depManager = waiting[depName];
924
- if (depManager && !depManager.isDone && loaded[depName]) {
925
- result = findCycle(depManager, traced);
926
- if (result) {
927
- break;
928
- }
929
- }
930
- }
931
- if (!fullyLoaded) {
932
- //Discard the cycle that was found, since it cannot
933
- //be forced yet. Also clear this module from traced.
934
- result = undefined;
935
- delete traced[fullName];
797
+ //If still waiting on loads, and the waiting load is something
798
+ //other than a plugin resource, or there are still outstanding
799
+ //scripts, then just try back later.
800
+ if ((!expired || usingPathFallback) && stillLoading) {
801
+ //Something is still waiting to load. Wait for it, but only
802
+ //if a timeout is not already in effect.
803
+ if ((isBrowser || isWebWorker) && !checkLoadedTimeoutId) {
804
+ checkLoadedTimeoutId = setTimeout(function () {
805
+ checkLoadedTimeoutId = 0;
806
+ checkLoaded();
807
+ }, 50);
936
808
  }
937
809
  }
938
810
 
939
- return result;
811
+ inCheckLoaded = false;
940
812
  }
941
813
 
942
- function forceExec(manager, traced) {
943
- var fullName = manager.map.fullName,
944
- depArray = manager.depArray,
945
- i, depName, depManager, prefix, prefixManager, value;
814
+ Module = function (map) {
815
+ this.events = undefEvents[map.id] || {};
816
+ this.map = map;
817
+ this.shim = config.shim[map.id];
818
+ this.depExports = [];
819
+ this.depMaps = [];
820
+ this.depMatched = [];
821
+ this.pluginMaps = {};
822
+ this.depCount = 0;
823
+
824
+ /* this.exports this.factory
825
+ this.depMaps = [],
826
+ this.enabled, this.fetched
827
+ */
828
+ };
946
829
 
830
+ Module.prototype = {
831
+ init: function(depMaps, factory, errback, options) {
832
+ options = options || {};
947
833
 
948
- if (manager.isDone || !fullName || !loaded[fullName]) {
949
- return undefined;
950
- }
834
+ //Do not do more inits if already done. Can happen if there
835
+ //are multiple define calls for the same module. That is not
836
+ //a normal, common case, but it is also not unexpected.
837
+ if (this.inited) {
838
+ return;
839
+ }
951
840
 
952
- if (fullName) {
953
- if (traced[fullName]) {
954
- return defined[fullName];
841
+ this.factory = factory;
842
+
843
+ if (errback) {
844
+ //Register for errors on this module.
845
+ this.on('error', errback);
846
+ } else if (this.events.error) {
847
+ //If no errback already, but there are error listeners
848
+ //on this module, set up an errback to pass to the deps.
849
+ errback = bind(this, function (err) {
850
+ this.emit('error', err);
851
+ });
955
852
  }
956
853
 
957
- traced[fullName] = true;
958
- }
854
+ //Do a copy of the dependency array, so that
855
+ //source inputs are not modified. For example
856
+ //"shim" deps are passed in here directly, and
857
+ //doing a direct modification of the depMaps array
858
+ //would affect that config.
859
+ this.depMaps = depMaps && depMaps.slice(0);
860
+ this.depMaps.rjsSkipMap = depMaps.rjsSkipMap;
861
+
862
+ this.errback = errback;
863
+
864
+ //Indicate this module has be initialized
865
+ this.inited = true;
866
+
867
+ this.ignore = options.ignore;
868
+
869
+ //Could have option to init this module in enabled mode,
870
+ //or could have been previously marked as enabled. However,
871
+ //the dependencies are not known until init is called. So
872
+ //if enabled previously, now trigger dependencies as enabled.
873
+ if (options.enabled || this.enabled) {
874
+ //Enable this module and dependencies.
875
+ //Will call this.check()
876
+ this.enable();
877
+ } else {
878
+ this.check();
879
+ }
880
+ },
959
881
 
960
- //Trace through the dependencies.
961
- if (depArray) {
962
- for (i = 0; i < depArray.length; i++) {
963
- //Some array members may be null, like if a trailing comma
964
- //IE, so do the explicit [i] access and check if it has a value.
965
- depName = depArray[i];
966
- if (depName) {
967
- //First, make sure if it is a plugin resource that the
968
- //plugin is not blocked.
969
- prefix = makeModuleMap(depName).prefix;
970
- if (prefix && (prefixManager = waiting[prefix])) {
971
- forceExec(prefixManager, traced);
972
- }
973
- depManager = waiting[depName];
974
- if (depManager && !depManager.isDone && loaded[depName]) {
975
- value = forceExec(depManager, traced);
976
- manager.depCallbacks[i](value);
977
- }
882
+ defineDepById: function (id, depExports) {
883
+ var i;
884
+
885
+ //Find the index for this dependency.
886
+ each(this.depMaps, function (map, index) {
887
+ if (map.id === id) {
888
+ i = index;
889
+ return true;
978
890
  }
891
+ });
892
+
893
+ return this.defineDep(i, depExports);
894
+ },
895
+
896
+ defineDep: function (i, depExports) {
897
+ //Because of cycles, defined callback for a given
898
+ //export can be called more than once.
899
+ if (!this.depMatched[i]) {
900
+ this.depMatched[i] = true;
901
+ this.depCount -= 1;
902
+ this.depExports[i] = depExports;
979
903
  }
980
- }
904
+ },
981
905
 
982
- return defined[fullName];
983
- }
906
+ fetch: function () {
907
+ if (this.fetched) {
908
+ return;
909
+ }
910
+ this.fetched = true;
984
911
 
985
- /**
986
- * Checks if all modules for a context are loaded, and if so, evaluates the
987
- * new ones in right dependency order.
988
- *
989
- * @private
990
- */
991
- function checkLoaded() {
992
- var waitInterval = config.waitSeconds * 1000,
993
- //It is possible to disable the wait interval by using waitSeconds of 0.
994
- expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(),
995
- noLoads = "", hasLoadedProp = false, stillLoading = false,
996
- cycleDeps = [],
997
- i, prop, err, manager, cycleManager, moduleDeps;
998
-
999
- //If there are items still in the paused queue processing wait.
1000
- //This is particularly important in the sync case where each paused
1001
- //item is processed right away but there may be more waiting.
1002
- if (context.pausedCount > 0) {
1003
- return undefined;
1004
- }
912
+ context.startTime = (new Date()).getTime();
913
+
914
+ var map = this.map;
1005
915
 
1006
- //Determine if priority loading is done. If so clear the priority. If
1007
- //not, then do not check
1008
- if (config.priorityWait) {
1009
- if (isPriorityDone()) {
1010
- //Call resume, since it could have
1011
- //some waiting dependencies to trace.
1012
- resume();
916
+ //If the manager is for a plugin managed resource,
917
+ //ask the plugin to load it now.
918
+ if (this.shim) {
919
+ makeRequire(this, true)(this.shim.deps || [], bind(this, function () {
920
+ return map.prefix ? this.callPlugin() : this.load();
921
+ }));
1013
922
  } else {
1014
- return undefined;
923
+ //Regular dependency.
924
+ return map.prefix ? this.callPlugin() : this.load();
1015
925
  }
1016
- }
926
+ },
1017
927
 
1018
- //See if anything is still in flight.
1019
- for (prop in loaded) {
1020
- if (!(prop in empty)) {
1021
- hasLoadedProp = true;
1022
- if (!loaded[prop]) {
1023
- if (expired) {
1024
- noLoads += prop + " ";
1025
- } else {
1026
- stillLoading = true;
1027
- if (prop.indexOf('!') === -1) {
1028
- //No reason to keep looking for unfinished
1029
- //loading. If the only stillLoading is a
1030
- //plugin resource though, keep going,
1031
- //because it may be that a plugin resource
1032
- //is waiting on a non-plugin cycle.
1033
- cycleDeps = [];
1034
- break;
928
+ load: function() {
929
+ var url = this.map.url;
930
+
931
+ //Regular dependency.
932
+ if (!urlFetched[url]) {
933
+ urlFetched[url] = true;
934
+ context.load(this.map.id, url);
935
+ }
936
+ },
937
+
938
+ /**
939
+ * Checks is the module is ready to define itself, and if so,
940
+ * define it. If the silent argument is true, then it will just
941
+ * define, but not notify listeners, and not ask for a context-wide
942
+ * check of all loaded modules. That is useful for cycle breaking.
943
+ */
944
+ check: function (silent) {
945
+ if (!this.enabled || this.enabling) {
946
+ return;
947
+ }
948
+
949
+ var id = this.map.id,
950
+ depExports = this.depExports,
951
+ exports = this.exports,
952
+ factory = this.factory,
953
+ err, cjsModule;
954
+
955
+ if (!this.inited) {
956
+ this.fetch();
957
+ } else if (this.error) {
958
+ this.emit('error', this.error);
959
+ } else if (!this.defining) {
960
+ //The factory could trigger another require call
961
+ //that would result in checking this module to
962
+ //define itself again. If already in the process
963
+ //of doing that, skip this work.
964
+ this.defining = true;
965
+
966
+ if (this.depCount < 1 && !this.defined) {
967
+ if (isFunction(factory)) {
968
+ //If there is an error listener, favor passing
969
+ //to that instead of throwing an error.
970
+ if (this.events.error) {
971
+ try {
972
+ exports = context.execCb(id, factory, depExports, exports);
973
+ } catch (e) {
974
+ err = e;
975
+ }
1035
976
  } else {
1036
- moduleDeps = managerCallbacks[prop] && managerCallbacks[prop].moduleDeps;
1037
- if (moduleDeps) {
1038
- cycleDeps.push.apply(cycleDeps, moduleDeps);
977
+ exports = context.execCb(id, factory, depExports, exports);
978
+ }
979
+
980
+ if (this.map.isDefine) {
981
+ //If setting exports via 'module' is in play,
982
+ //favor that over return value and exports. After that,
983
+ //favor a non-undefined return value over exports use.
984
+ cjsModule = this.module;
985
+ if (cjsModule &&
986
+ cjsModule.exports !== undefined &&
987
+ //Make sure it is not already the exports value
988
+ cjsModule.exports !== this.exports) {
989
+ exports = cjsModule.exports;
990
+ } else if (exports === undefined && this.usingExports) {
991
+ //exports already set the defined value.
992
+ exports = this.exports;
1039
993
  }
1040
994
  }
995
+
996
+ if (err) {
997
+ err.requireMap = this.map;
998
+ err.requireModules = [this.map.id];
999
+ err.requireType = 'define';
1000
+ return onError((this.error = err));
1001
+ }
1002
+
1003
+ } else {
1004
+ //Just a literal value
1005
+ exports = factory;
1006
+ }
1007
+
1008
+ this.exports = exports;
1009
+
1010
+ if (this.map.isDefine && !this.ignore) {
1011
+ defined[id] = exports;
1012
+
1013
+ if (req.onResourceLoad) {
1014
+ req.onResourceLoad(context, this.map, this.depMaps);
1015
+ }
1016
+ }
1017
+
1018
+ //Clean up
1019
+ delete registry[id];
1020
+
1021
+ this.defined = true;
1022
+ context.waitCount -= 1;
1023
+ if (context.waitCount === 0) {
1024
+ //Clear the wait array used for cycles.
1025
+ waitAry = [];
1041
1026
  }
1042
1027
  }
1043
- }
1044
- }
1045
1028
 
1046
- //Check for exit conditions.
1047
- if (!hasLoadedProp && !context.waitCount) {
1048
- //If the loaded object had no items, then the rest of
1049
- //the work below does not need to be done.
1050
- return undefined;
1051
- }
1052
- if (expired && noLoads) {
1053
- //If wait time expired, throw error of unloaded modules.
1054
- err = makeError("timeout", "Load timeout for modules: " + noLoads);
1055
- err.requireType = "timeout";
1056
- err.requireModules = noLoads;
1057
- err.contextName = context.contextName;
1058
- return req.onError(err);
1059
- }
1029
+ //Finished the define stage. Allow calling check again
1030
+ //to allow define notifications below in the case of a
1031
+ //cycle.
1032
+ this.defining = false;
1060
1033
 
1061
- //If still loading but a plugin is waiting on a regular module cycle
1062
- //break the cycle.
1063
- if (stillLoading && cycleDeps.length) {
1064
- for (i = 0; (manager = waiting[cycleDeps[i]]); i++) {
1065
- if ((cycleManager = findCycle(manager, {}))) {
1066
- forceExec(cycleManager, {});
1067
- break;
1034
+ if (!silent) {
1035
+ if (this.defined && !this.defineEmitted) {
1036
+ this.defineEmitted = true;
1037
+ this.emit('defined', this.exports);
1038
+ this.defineEmitComplete = true;
1039
+ }
1068
1040
  }
1069
1041
  }
1042
+ },
1070
1043
 
1071
- }
1044
+ callPlugin: function() {
1045
+ var map = this.map,
1046
+ id = map.id,
1047
+ pluginMap = makeModuleMap(map.prefix, null, false, true);
1048
+
1049
+ on(pluginMap, 'defined', bind(this, function (plugin) {
1050
+ var name = this.map.name,
1051
+ parentName = this.map.parentMap ? this.map.parentMap.name : null,
1052
+ load, normalizedMap, normalizedMod;
1053
+
1054
+ //If current map is not normalized, wait for that
1055
+ //normalized name to load instead of continuing.
1056
+ if (this.map.unnormalized) {
1057
+ //Normalize the ID if the plugin allows it.
1058
+ if (plugin.normalize) {
1059
+ name = plugin.normalize(name, function (name) {
1060
+ return normalize(name, parentName, true);
1061
+ }) || '';
1062
+ }
1072
1063
 
1073
- //If still waiting on loads, and the waiting load is something
1074
- //other than a plugin resource, or there are still outstanding
1075
- //scripts, then just try back later.
1076
- if (!expired && (stillLoading || context.scriptCount)) {
1077
- //Something is still waiting to load. Wait for it, but only
1078
- //if a timeout is not already in effect.
1079
- if ((isBrowser || isWebWorker) && !checkLoadedTimeoutId) {
1080
- checkLoadedTimeoutId = setTimeout(function () {
1081
- checkLoadedTimeoutId = 0;
1082
- checkLoaded();
1083
- }, 50);
1084
- }
1085
- return undefined;
1086
- }
1064
+ normalizedMap = makeModuleMap(map.prefix + '!' + name,
1065
+ this.map.parentMap,
1066
+ false,
1067
+ true);
1068
+ on(normalizedMap,
1069
+ 'defined', bind(this, function (value) {
1070
+ this.init([], function () { return value; }, null, {
1071
+ enabled: true,
1072
+ ignore: true
1073
+ });
1074
+ }));
1075
+ normalizedMod = registry[normalizedMap.id];
1076
+ if (normalizedMod) {
1077
+ if (this.events.error) {
1078
+ normalizedMod.on('error', bind(this, function (err) {
1079
+ this.emit('error', err);
1080
+ }));
1081
+ }
1082
+ normalizedMod.enable();
1083
+ }
1087
1084
 
1088
- //If still have items in the waiting cue, but all modules have
1089
- //been loaded, then it means there are some circular dependencies
1090
- //that need to be broken.
1091
- //However, as a waiting thing is fired, then it can add items to
1092
- //the waiting cue, and those items should not be fired yet, so
1093
- //make sure to redo the checkLoaded call after breaking a single
1094
- //cycle, if nothing else loaded then this logic will pick it up
1095
- //again.
1096
- if (context.waitCount) {
1097
- //Cycle through the waitAry, and call items in sequence.
1098
- for (i = 0; (manager = waitAry[i]); i++) {
1099
- forceExec(manager, {});
1100
- }
1085
+ return;
1086
+ }
1101
1087
 
1102
- //If anything got placed in the paused queue, run it down.
1103
- if (context.paused.length) {
1104
- resume();
1105
- }
1088
+ load = bind(this, function (value) {
1089
+ this.init([], function () { return value; }, null, {
1090
+ enabled: true
1091
+ });
1092
+ });
1106
1093
 
1107
- //Only allow this recursion to a certain depth. Only
1108
- //triggered by errors in calling a module in which its
1109
- //modules waiting on it cannot finish loading, or some circular
1110
- //dependencies that then may add more dependencies.
1111
- //The value of 5 is a bit arbitrary. Hopefully just one extra
1112
- //pass, or two for the case of circular dependencies generating
1113
- //more work that gets resolved in the sync node case.
1114
- if (checkLoadedDepth < 5) {
1115
- checkLoadedDepth += 1;
1116
- checkLoaded();
1117
- }
1118
- }
1094
+ load.error = bind(this, function (err) {
1095
+ this.inited = true;
1096
+ this.error = err;
1097
+ err.requireModules = [id];
1119
1098
 
1120
- checkLoadedDepth = 0;
1099
+ //Remove temp unnormalized modules for this module,
1100
+ //since they will never be resolved otherwise now.
1101
+ eachProp(registry, function (mod) {
1102
+ if (mod.map.id.indexOf(id + '_unnormalized') === 0) {
1103
+ removeWaiting(mod.map.id);
1104
+ }
1105
+ });
1121
1106
 
1122
- //Check for DOM ready, and nothing is waiting across contexts.
1123
- req.checkReadyState();
1107
+ onError(err);
1108
+ });
1124
1109
 
1125
- return undefined;
1126
- }
1110
+ //Allow plugins to load other code without having to know the
1111
+ //context or how to 'complete' the load.
1112
+ load.fromText = function (moduleName, text) {
1113
+ /*jslint evil: true */
1114
+ var hasInteractive = useInteractive;
1127
1115
 
1128
- /**
1129
- * Resumes tracing of dependencies and then checks if everything is loaded.
1130
- */
1131
- resume = function () {
1132
- var manager, map, url, i, p, args, fullName;
1116
+ //Turn off interactive script matching for IE for any define
1117
+ //calls in the text, then turn it back on at the end.
1118
+ if (hasInteractive) {
1119
+ useInteractive = false;
1120
+ }
1133
1121
 
1134
- //Any defined modules in the global queue, intake them now.
1135
- context.takeGlobalQueue();
1122
+ //Prime the system by creating a module instance for
1123
+ //it.
1124
+ getModule(makeModuleMap(moduleName));
1136
1125
 
1137
- resumeDepth += 1;
1126
+ req.exec(text);
1138
1127
 
1139
- if (context.scriptCount <= 0) {
1140
- //Synchronous envs will push the number below zero with the
1141
- //decrement above, be sure to set it back to zero for good measure.
1142
- //require() calls that also do not end up loading scripts could
1143
- //push the number negative too.
1144
- context.scriptCount = 0;
1145
- }
1128
+ if (hasInteractive) {
1129
+ useInteractive = true;
1130
+ }
1146
1131
 
1147
- //Make sure any remaining defQueue items get properly processed.
1148
- while (defQueue.length) {
1149
- args = defQueue.shift();
1150
- if (args[0] === null) {
1151
- return req.onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1]));
1152
- } else {
1153
- callDefMain(args);
1132
+ //Support anonymous modules.
1133
+ context.completeLoad(moduleName);
1134
+ };
1135
+
1136
+ //Use parentName here since the plugin's name is not reliable,
1137
+ //could be some weird string with no path that actually wants to
1138
+ //reference the parentName's path.
1139
+ plugin.load(map.name, makeRequire(map.parentMap, true, function (deps, cb) {
1140
+ deps.rjsSkipMap = true;
1141
+ return context.require(deps, cb);
1142
+ }), load, config);
1143
+ }));
1144
+
1145
+ context.enable(pluginMap, this);
1146
+ this.pluginMaps[pluginMap.id] = pluginMap;
1147
+ },
1148
+
1149
+ enable: function () {
1150
+ this.enabled = true;
1151
+
1152
+ if (!this.waitPushed) {
1153
+ waitAry.push(this);
1154
+ context.waitCount += 1;
1155
+ this.waitPushed = true;
1154
1156
  }
1155
- }
1156
1157
 
1157
- //Skip the resume of paused dependencies
1158
- //if current context is in priority wait.
1159
- if (!config.priorityWait || isPriorityDone()) {
1160
- while (context.paused.length) {
1161
- p = context.paused;
1162
- context.pausedCount += p.length;
1163
- //Reset paused list
1164
- context.paused = [];
1165
-
1166
- for (i = 0; (manager = p[i]); i++) {
1167
- map = manager.map;
1168
- url = map.url;
1169
- fullName = map.fullName;
1170
-
1171
- //If the manager is for a plugin managed resource,
1172
- //ask the plugin to load it now.
1173
- if (map.prefix) {
1174
- callPlugin(map.prefix, manager);
1175
- } else {
1176
- //Regular dependency.
1177
- if (!urlFetched[url] && !loaded[fullName]) {
1178
- (config.requireLoad || req.load)(context, fullName, url);
1179
-
1180
- //Mark the URL as fetched, but only if it is
1181
- //not an empty: URL, used by the optimizer.
1182
- //In that case we need to be sure to call
1183
- //load() for each module that is mapped to
1184
- //empty: so that dependencies are satisfied
1185
- //correctly.
1186
- if (url.indexOf('empty:') !== 0) {
1187
- urlFetched[url] = true;
1188
- }
1189
- }
1158
+ //Set flag mentioning that the module is enabling,
1159
+ //so that immediate calls to the defined callbacks
1160
+ //for dependencies do not trigger inadvertent load
1161
+ //with the depCount still being zero.
1162
+ this.enabling = true;
1163
+
1164
+ //Enable each dependency
1165
+ each(this.depMaps, bind(this, function (depMap, i) {
1166
+ var id, mod, handler;
1167
+
1168
+ if (typeof depMap === 'string') {
1169
+ //Dependency needs to be converted to a depMap
1170
+ //and wired up to this module.
1171
+ depMap = makeModuleMap(depMap,
1172
+ (this.map.isDefine ? this.map : this.map.parentMap),
1173
+ false,
1174
+ !this.depMaps.rjsSkipMap);
1175
+ this.depMaps[i] = depMap;
1176
+
1177
+ handler = handlers[depMap.id];
1178
+
1179
+ if (handler) {
1180
+ this.depExports[i] = handler(this);
1181
+ return;
1182
+ }
1183
+
1184
+ this.depCount += 1;
1185
+
1186
+ on(depMap, 'defined', bind(this, function (depExports) {
1187
+ this.defineDep(i, depExports);
1188
+ this.check();
1189
+ }));
1190
+
1191
+ if (this.errback) {
1192
+ on(depMap, 'error', this.errback);
1190
1193
  }
1191
1194
  }
1192
1195
 
1193
- //Move the start time for timeout forward.
1194
- context.startTime = (new Date()).getTime();
1195
- context.pausedCount -= p.length;
1196
+ id = depMap.id;
1197
+ mod = registry[id];
1198
+
1199
+ //Skip special modules like 'require', 'exports', 'module'
1200
+ //Also, don't call enable if it is already enabled,
1201
+ //important in circular dependency cases.
1202
+ if (!handlers[id] && mod && !mod.enabled) {
1203
+ context.enable(depMap, this);
1204
+ }
1205
+ }));
1206
+
1207
+ //Enable each plugin that is used in
1208
+ //a dependency
1209
+ eachProp(this.pluginMaps, bind(this, function (pluginMap) {
1210
+ var mod = registry[pluginMap.id];
1211
+ if (mod && !mod.enabled) {
1212
+ context.enable(pluginMap, this);
1213
+ }
1214
+ }));
1215
+
1216
+ this.enabling = false;
1217
+
1218
+ this.check();
1219
+ },
1220
+
1221
+ on: function(name, cb) {
1222
+ var cbs = this.events[name];
1223
+ if (!cbs) {
1224
+ cbs = this.events[name] = [];
1225
+ }
1226
+ cbs.push(cb);
1227
+ },
1228
+
1229
+ emit: function (name, evt) {
1230
+ each(this.events[name], function (cb) {
1231
+ cb(evt);
1232
+ });
1233
+ if (name === 'error') {
1234
+ //Now that the error handler was triggered, remove
1235
+ //the listeners, since this broken Module instance
1236
+ //can stay around for a while in the registry/waitAry.
1237
+ delete this.events[name];
1196
1238
  }
1197
1239
  }
1240
+ };
1198
1241
 
1199
- //Only check if loaded when resume depth is 1. It is likely that
1200
- //it is only greater than 1 in sync environments where a factory
1201
- //function also then calls the callback-style require. In those
1202
- //cases, the checkLoaded should not occur until the resume
1203
- //depth is back at the top level.
1204
- if (resumeDepth === 1) {
1205
- checkLoaded();
1242
+ function callGetModule(args) {
1243
+ getModule(makeModuleMap(args[0], null, true)).init(args[1], args[2]);
1244
+ }
1245
+
1246
+ function removeListener(node, func, name, ieName) {
1247
+ //Favor detachEvent because of IE9
1248
+ //issue, see attachEvent/addEventListener comment elsewhere
1249
+ //in this file.
1250
+ if (node.detachEvent && !isOpera) {
1251
+ //Probably IE. If not it will throw an error, which will be
1252
+ //useful to know.
1253
+ if (ieName) {
1254
+ node.detachEvent(ieName, func);
1255
+ }
1256
+ } else {
1257
+ node.removeEventListener(name, func, false);
1206
1258
  }
1259
+ }
1207
1260
 
1208
- resumeDepth -= 1;
1261
+ /**
1262
+ * Given an event from a script node, get the requirejs info from it,
1263
+ * and then removes the event listeners on the node.
1264
+ * @param {Event} evt
1265
+ * @returns {Object}
1266
+ */
1267
+ function getScriptData(evt) {
1268
+ //Using currentTarget instead of target for Firefox 2.0's sake. Not
1269
+ //all old browsers will be supported, but this one was easy enough
1270
+ //to support and still makes sense.
1271
+ var node = evt.currentTarget || evt.srcElement;
1209
1272
 
1210
- return undefined;
1211
- };
1273
+ //Remove the listeners once here.
1274
+ removeListener(node, context.onScriptLoad, 'load', 'onreadystatechange');
1275
+ removeListener(node, context.onScriptError, 'error');
1212
1276
 
1213
- //Define the context object. Many of these fields are on here
1214
- //just to make debugging easier.
1215
- context = {
1216
- contextName: contextName,
1277
+ return {
1278
+ node: node,
1279
+ id: node && node.getAttribute('data-requiremodule')
1280
+ };
1281
+ }
1282
+
1283
+ return (context = {
1217
1284
  config: config,
1218
- defQueue: defQueue,
1219
- waiting: waiting,
1220
- waitCount: 0,
1221
- specified: specified,
1222
- loaded: loaded,
1223
- urlMap: urlMap,
1224
- urlFetched: urlFetched,
1225
- scriptCount: 0,
1285
+ contextName: contextName,
1286
+ registry: registry,
1226
1287
  defined: defined,
1227
- paused: [],
1228
- pausedCount: 0,
1229
- plugins: plugins,
1230
- needFullExec: needFullExec,
1231
- fake: {},
1232
- fullExec: fullExec,
1233
- managerCallbacks: managerCallbacks,
1288
+ urlFetched: urlFetched,
1289
+ waitCount: 0,
1290
+ defQueue: defQueue,
1291
+ Module: Module,
1234
1292
  makeModuleMap: makeModuleMap,
1235
- normalize: normalize,
1293
+
1236
1294
  /**
1237
1295
  * Set a configuration for the context.
1238
1296
  * @param {Object} cfg config object to integrate.
1239
1297
  */
1240
1298
  configure: function (cfg) {
1241
- var paths, prop, packages, pkgs, packagePaths, requireWait;
1242
-
1243
1299
  //Make sure the baseUrl ends in a slash.
1244
1300
  if (cfg.baseUrl) {
1245
- if (cfg.baseUrl.charAt(cfg.baseUrl.length - 1) !== "/") {
1246
- cfg.baseUrl += "/";
1301
+ if (cfg.baseUrl.charAt(cfg.baseUrl.length - 1) !== '/') {
1302
+ cfg.baseUrl += '/';
1247
1303
  }
1248
1304
  }
1249
1305
 
1250
1306
  //Save off the paths and packages since they require special processing,
1251
1307
  //they are additive.
1252
- paths = config.paths;
1253
- packages = config.packages;
1254
- pkgs = config.pkgs;
1308
+ var pkgs = config.pkgs,
1309
+ shim = config.shim,
1310
+ paths = config.paths,
1311
+ map = config.map;
1255
1312
 
1256
1313
  //Mix in the config values, favoring the new values over
1257
1314
  //existing ones in context.config.
1258
1315
  mixin(config, cfg, true);
1259
1316
 
1260
- //Adjust paths if necessary.
1261
- if (cfg.paths) {
1262
- for (prop in cfg.paths) {
1263
- if (!(prop in empty)) {
1264
- paths[prop] = cfg.paths[prop];
1265
- }
1266
- }
1267
- config.paths = paths;
1317
+ //Merge paths.
1318
+ config.paths = mixin(paths, cfg.paths, true);
1319
+
1320
+ //Merge map
1321
+ if (cfg.map) {
1322
+ config.map = mixin(map || {}, cfg.map, true, true);
1268
1323
  }
1269
1324
 
1270
- packagePaths = cfg.packagePaths;
1271
- if (packagePaths || cfg.packages) {
1272
- //Convert packagePaths into a packages config.
1273
- if (packagePaths) {
1274
- for (prop in packagePaths) {
1275
- if (!(prop in empty)) {
1276
- configurePackageDir(pkgs, packagePaths[prop], prop);
1277
- }
1325
+ //Merge shim
1326
+ if (cfg.shim) {
1327
+ eachProp(cfg.shim, function (value, id) {
1328
+ //Normalize the structure
1329
+ if (isArray(value)) {
1330
+ value = {
1331
+ deps: value
1332
+ };
1278
1333
  }
1279
- }
1334
+ if (value.exports && !value.exports.__buildReady) {
1335
+ value.exports = context.makeShimExports(value.exports);
1336
+ }
1337
+ shim[id] = value;
1338
+ });
1339
+ config.shim = shim;
1340
+ }
1280
1341
 
1281
- //Adjust packages if necessary.
1282
- if (cfg.packages) {
1283
- configurePackageDir(pkgs, cfg.packages);
1284
- }
1342
+ //Adjust packages if necessary.
1343
+ if (cfg.packages) {
1344
+ each(cfg.packages, function (pkgObj) {
1345
+ var location;
1346
+
1347
+ pkgObj = typeof pkgObj === 'string' ? { name: pkgObj } : pkgObj;
1348
+ location = pkgObj.location;
1349
+
1350
+ //Create a brand new object on pkgs, since currentPackages can
1351
+ //be passed in again, and config.pkgs is the internal transformed
1352
+ //state for all package configs.
1353
+ pkgs[pkgObj.name] = {
1354
+ name: pkgObj.name,
1355
+ location: location || pkgObj.name,
1356
+ //Remove leading dot in main, so main paths are normalized,
1357
+ //and remove any trailing .js, since different package
1358
+ //envs have different conventions: some use a module name,
1359
+ //some use a file name.
1360
+ main: (pkgObj.main || 'main')
1361
+ .replace(currDirRegExp, '')
1362
+ .replace(jsSuffixRegExp, '')
1363
+ };
1364
+ });
1285
1365
 
1286
1366
  //Done with modifications, assing packages back to context config
1287
1367
  config.pkgs = pkgs;
1288
1368
  }
1289
1369
 
1290
- //If priority loading is in effect, trigger the loads now
1291
- if (cfg.priority) {
1292
- //Hold on to requireWait value, and reset it after done
1293
- requireWait = context.requireWait;
1294
-
1295
- //Allow tracing some require calls to allow the fetching
1296
- //of the priority config.
1297
- context.requireWait = false;
1298
- //But first, call resume to register any defined modules that may
1299
- //be in a data-main built file before the priority config
1300
- //call.
1301
- resume();
1302
-
1303
- context.require(cfg.priority);
1304
-
1305
- //Trigger a resume right away, for the case when
1306
- //the script with the priority load is done as part
1307
- //of a data-main call. In that case the normal resume
1308
- //call will not happen because the scriptCount will be
1309
- //at 1, since the script for data-main is being processed.
1310
- resume();
1311
-
1312
- //Restore previous state.
1313
- context.requireWait = requireWait;
1314
- config.priorityWait = cfg.priority;
1315
- }
1370
+ //If there are any "waiting to execute" modules in the registry,
1371
+ //update the maps for them, since their info, like URLs to load,
1372
+ //may have changed.
1373
+ eachProp(registry, function (mod, id) {
1374
+ mod.map = makeModuleMap(id);
1375
+ });
1316
1376
 
1317
1377
  //If a deps array or a config callback is specified, then call
1318
1378
  //require with those args. This is useful when require is defined as a
@@ -1322,77 +1382,132 @@ var requirejs, require, define;
1322
1382
  }
1323
1383
  },
1324
1384
 
1325
- requireDefined: function (moduleName, relModuleMap) {
1326
- return makeModuleMap(moduleName, relModuleMap).fullName in defined;
1385
+ makeShimExports: function (exports) {
1386
+ var func;
1387
+ if (typeof exports === 'string') {
1388
+ func = function () {
1389
+ return getGlobal(exports);
1390
+ };
1391
+ //Save the exports for use in nodefine checking.
1392
+ func.exports = exports;
1393
+ return func;
1394
+ } else {
1395
+ return function () {
1396
+ return exports.apply(global, arguments);
1397
+ };
1398
+ }
1399
+ },
1400
+
1401
+ requireDefined: function (id, relMap) {
1402
+ return hasProp(defined, makeModuleMap(id, relMap, false, true).id);
1327
1403
  },
1328
1404
 
1329
- requireSpecified: function (moduleName, relModuleMap) {
1330
- return makeModuleMap(moduleName, relModuleMap).fullName in specified;
1405
+ requireSpecified: function (id, relMap) {
1406
+ id = makeModuleMap(id, relMap, false, true).id;
1407
+ return hasProp(defined, id) || hasProp(registry, id);
1331
1408
  },
1332
1409
 
1333
- require: function (deps, callback, relModuleMap) {
1334
- var moduleName, fullName, moduleMap;
1335
- if (typeof deps === "string") {
1410
+ require: function (deps, callback, errback, relMap) {
1411
+ var moduleName, id, map, requireMod, args;
1412
+ if (typeof deps === 'string') {
1336
1413
  if (isFunction(callback)) {
1337
1414
  //Invalid call
1338
- return req.onError(makeError("requireargs", "Invalid require call"));
1415
+ return onError(makeError('requireargs', 'Invalid require call'), errback);
1339
1416
  }
1340
1417
 
1341
1418
  //Synchronous access to one module. If require.get is
1342
1419
  //available (as in the Node adapter), prefer that.
1343
1420
  //In this case deps is the moduleName and callback is
1344
- //the relModuleMap
1421
+ //the relMap
1345
1422
  if (req.get) {
1346
1423
  return req.get(context, deps, callback);
1347
1424
  }
1348
1425
 
1349
1426
  //Just return the module wanted. In this scenario, the
1350
- //second arg (if passed) is just the relModuleMap.
1427
+ //second arg (if passed) is just the relMap.
1351
1428
  moduleName = deps;
1352
- relModuleMap = callback;
1429
+ relMap = callback;
1353
1430
 
1354
1431
  //Normalize module name, if it contains . or ..
1355
- moduleMap = makeModuleMap(moduleName, relModuleMap);
1356
- fullName = moduleMap.fullName;
1432
+ map = makeModuleMap(moduleName, relMap, false, true);
1433
+ id = map.id;
1357
1434
 
1358
- if (!(fullName in defined)) {
1359
- return req.onError(makeError("notloaded", "Module name '" +
1360
- moduleMap.fullName +
1361
- "' has not been loaded yet for context: " +
1435
+ if (!hasProp(defined, id)) {
1436
+ return onError(makeError('notloaded', 'Module name "' +
1437
+ id +
1438
+ '" has not been loaded yet for context: ' +
1362
1439
  contextName));
1363
1440
  }
1364
- return defined[fullName];
1441
+ return defined[id];
1365
1442
  }
1366
1443
 
1367
- //Call main but only if there are dependencies or
1368
- //a callback to call.
1369
- if (deps && deps.length || callback) {
1370
- main(null, deps, callback, relModuleMap);
1444
+ //Callback require. Normalize args. if callback or errback is
1445
+ //not a function, it means it is a relMap. Test errback first.
1446
+ if (errback && !isFunction(errback)) {
1447
+ relMap = errback;
1448
+ errback = undefined;
1371
1449
  }
1450
+ if (callback && !isFunction(callback)) {
1451
+ relMap = callback;
1452
+ callback = undefined;
1453
+ }
1454
+
1455
+ //Any defined modules in the global queue, intake them now.
1456
+ takeGlobalQueue();
1372
1457
 
1373
- //If the require call does not trigger anything new to load,
1374
- //then resume the dependency processing.
1375
- if (!context.requireWait) {
1376
- while (!context.scriptCount && context.paused.length) {
1377
- resume();
1458
+ //Make sure any remaining defQueue items get properly processed.
1459
+ while (defQueue.length) {
1460
+ args = defQueue.shift();
1461
+ if (args[0] === null) {
1462
+ return onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1]));
1463
+ } else {
1464
+ //args are id, deps, factory. Should be normalized by the
1465
+ //define() function.
1466
+ callGetModule(args);
1378
1467
  }
1379
1468
  }
1469
+
1470
+ //Mark all the dependencies as needing to be loaded.
1471
+ requireMod = getModule(makeModuleMap(null, relMap));
1472
+
1473
+ requireMod.init(deps, callback, errback, {
1474
+ enabled: true
1475
+ });
1476
+
1477
+ checkLoaded();
1478
+
1380
1479
  return context.require;
1381
1480
  },
1382
1481
 
1482
+ undef: function (id) {
1483
+ var map = makeModuleMap(id, null, true),
1484
+ mod = registry[id];
1485
+
1486
+ delete defined[id];
1487
+ delete urlFetched[map.url];
1488
+ delete undefEvents[id];
1489
+
1490
+ if (mod) {
1491
+ //Hold on to listeners in case the
1492
+ //module will be attempted to be reloaded
1493
+ //using a different config.
1494
+ if (mod.events.defined) {
1495
+ undefEvents[id] = mod.events;
1496
+ }
1497
+
1498
+ removeWaiting(id);
1499
+ }
1500
+ },
1501
+
1383
1502
  /**
1384
- * Internal method to transfer globalQueue items to this context's
1385
- * defQueue.
1503
+ * Called to enable a module if it is still in the registry
1504
+ * awaiting enablement. parent module is passed in for context,
1505
+ * used by the optimizer.
1386
1506
  */
1387
- takeGlobalQueue: function () {
1388
- //Push all the globalDefQueue items into the context's defQueue
1389
- if (globalDefQueue.length) {
1390
- //Array splice in the values since the context code has a
1391
- //local var ref to defQueue, so cannot just reassign the one
1392
- //on context.
1393
- apsp.apply(context.defQueue,
1394
- [context.defQueue.length - 1, 0].concat(globalDefQueue));
1395
- globalDefQueue = [];
1507
+ enable: function (depMap, parent) {
1508
+ var mod = registry[depMap.id];
1509
+ if (mod) {
1510
+ getModule(depMap).enable();
1396
1511
  }
1397
1512
  },
1398
1513
 
@@ -1403,51 +1518,55 @@ var requirejs, require, define;
1403
1518
  * @param {String} moduleName the name of the module to potentially complete.
1404
1519
  */
1405
1520
  completeLoad: function (moduleName) {
1406
- var args;
1521
+ var shim = config.shim[moduleName] || {},
1522
+ shExports = shim.exports && shim.exports.exports,
1523
+ found, args, mod;
1407
1524
 
1408
- context.takeGlobalQueue();
1525
+ takeGlobalQueue();
1409
1526
 
1410
1527
  while (defQueue.length) {
1411
1528
  args = defQueue.shift();
1412
-
1413
1529
  if (args[0] === null) {
1414
1530
  args[0] = moduleName;
1415
- break;
1531
+ //If already found an anonymous module and bound it
1532
+ //to this name, then this is some other anon module
1533
+ //waiting for its completeLoad to fire.
1534
+ if (found) {
1535
+ break;
1536
+ }
1537
+ found = true;
1416
1538
  } else if (args[0] === moduleName) {
1417
1539
  //Found matching define call for this script!
1418
- break;
1419
- } else {
1420
- //Some other named define call, most likely the result
1421
- //of a build layer that included many define calls.
1422
- callDefMain(args);
1423
- args = null;
1540
+ found = true;
1424
1541
  }
1425
- }
1426
- if (args) {
1427
- callDefMain(args);
1428
- } else {
1429
- //A script that does not call define(), so just simulate
1430
- //the call for it. Special exception for jQuery dynamic load.
1431
- callDefMain([moduleName, [],
1432
- moduleName === "jquery" && typeof jQuery !== "undefined" ?
1433
- function () {
1434
- return jQuery;
1435
- } : null]);
1436
- }
1437
1542
 
1438
- //Doing this scriptCount decrement branching because sync envs
1439
- //need to decrement after resume, otherwise it looks like
1440
- //loading is complete after the first dependency is fetched.
1441
- //For browsers, it works fine to decrement after, but it means
1442
- //the checkLoaded setTimeout 50 ms cost is taken. To avoid
1443
- //that cost, decrement beforehand.
1444
- if (req.isAsync) {
1445
- context.scriptCount -= 1;
1543
+ callGetModule(args);
1446
1544
  }
1447
- resume();
1448
- if (!req.isAsync) {
1449
- context.scriptCount -= 1;
1545
+
1546
+ //Do this after the cycle of callGetModule in case the result
1547
+ //of those calls/init calls changes the registry.
1548
+ mod = registry[moduleName];
1549
+
1550
+ if (!found &&
1551
+ !defined[moduleName] &&
1552
+ mod && !mod.inited) {
1553
+ if (config.enforceDefine && (!shExports || !getGlobal(shExports))) {
1554
+ if (hasPathFallback(moduleName)) {
1555
+ return;
1556
+ } else {
1557
+ return onError(makeError('nodefine',
1558
+ 'No define call for ' + moduleName,
1559
+ null,
1560
+ [moduleName]));
1561
+ }
1562
+ } else {
1563
+ //A script that does not call define(), so just simulate
1564
+ //the call for it.
1565
+ callGetModule([moduleName, (shim.deps || []), shim.exports]);
1566
+ }
1450
1567
  }
1568
+
1569
+ checkLoaded();
1451
1570
  },
1452
1571
 
1453
1572
  /**
@@ -1456,7 +1575,7 @@ var requirejs, require, define;
1456
1575
  * plain URLs like nameToUrl.
1457
1576
  */
1458
1577
  toUrl: function (moduleNamePlusExt, relModuleMap) {
1459
- var index = moduleNamePlusExt.lastIndexOf("."),
1578
+ var index = moduleNamePlusExt.lastIndexOf('.'),
1460
1579
  ext = null;
1461
1580
 
1462
1581
  if (index !== -1) {
@@ -1473,10 +1592,10 @@ var requirejs, require, define;
1473
1592
  */
1474
1593
  nameToUrl: function (moduleName, ext, relModuleMap) {
1475
1594
  var paths, pkgs, pkg, pkgPath, syms, i, parentModule, url,
1476
- config = context.config;
1595
+ parentPath;
1477
1596
 
1478
1597
  //Normalize module name if have a base relative module name to work from.
1479
- moduleName = normalize(moduleName, relModuleMap && relModuleMap.fullName);
1598
+ moduleName = normalize(moduleName, relModuleMap && relModuleMap.id, true);
1480
1599
 
1481
1600
  //If a colon is in the URL, it indicates a protocol is used and it is just
1482
1601
  //an URL to a file, or if it starts with a slash, contains a query arg (i.e. ?)
@@ -1486,22 +1605,29 @@ var requirejs, require, define;
1486
1605
  //Just a plain path, not module name lookup, so just return it.
1487
1606
  //Add extension if it is included. This is a bit wonky, only non-.js things pass
1488
1607
  //an extension, this method probably needs to be reworked.
1489
- url = moduleName + (ext ? ext : "");
1608
+ url = moduleName + (ext || '');
1490
1609
  } else {
1491
1610
  //A module that needs to be converted to a path.
1492
1611
  paths = config.paths;
1493
1612
  pkgs = config.pkgs;
1494
1613
 
1495
- syms = moduleName.split("/");
1614
+ syms = moduleName.split('/');
1496
1615
  //For each module name segment, see if there is a path
1497
1616
  //registered for it. Start with most specific name
1498
1617
  //and work up from it.
1499
- for (i = syms.length; i > 0; i--) {
1500
- parentModule = syms.slice(0, i).join("/");
1501
- if (paths[parentModule]) {
1502
- syms.splice(0, i, paths[parentModule]);
1618
+ for (i = syms.length; i > 0; i -= 1) {
1619
+ parentModule = syms.slice(0, i).join('/');
1620
+ pkg = pkgs[parentModule];
1621
+ parentPath = paths[parentModule];
1622
+ if (parentPath) {
1623
+ //If an array, it means there are a few choices,
1624
+ //Choose the one that is desired
1625
+ if (isArray(parentPath)) {
1626
+ parentPath = parentPath[0];
1627
+ }
1628
+ syms.splice(0, i, parentPath);
1503
1629
  break;
1504
- } else if ((pkg = pkgs[parentModule])) {
1630
+ } else if (pkg) {
1505
1631
  //If module name is just the package name, then looking
1506
1632
  //for the main module.
1507
1633
  if (moduleName === pkg.name) {
@@ -1515,22 +1641,64 @@ var requirejs, require, define;
1515
1641
  }
1516
1642
 
1517
1643
  //Join the path parts together, then figure out if baseUrl is needed.
1518
- url = syms.join("/") + (ext || ".js");
1519
- url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? "" : config.baseUrl) + url;
1644
+ url = syms.join('/') + (ext || '.js');
1645
+ url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url;
1520
1646
  }
1521
1647
 
1522
1648
  return config.urlArgs ? url +
1523
1649
  ((url.indexOf('?') === -1 ? '?' : '&') +
1524
1650
  config.urlArgs) : url;
1525
- }
1526
- };
1651
+ },
1652
+
1653
+ //Delegates to req.load. Broken out as a separate function to
1654
+ //allow overriding in the optimizer.
1655
+ load: function (id, url) {
1656
+ req.load(context, id, url);
1657
+ },
1658
+
1659
+ /**
1660
+ * Executes a module callack function. Broken out as a separate function
1661
+ * solely to allow the build system to sequence the files in the built
1662
+ * layer in the right sequence.
1663
+ *
1664
+ * @private
1665
+ */
1666
+ execCb: function (name, callback, args, exports) {
1667
+ return callback.apply(exports, args);
1668
+ },
1527
1669
 
1528
- //Make these visible on the context so can be called at the very
1529
- //end of the file to bootstrap
1530
- context.jQueryCheck = jQueryCheck;
1531
- context.resume = resume;
1670
+ /**
1671
+ * callback for script loads, used to check status of loading.
1672
+ *
1673
+ * @param {Event} evt the event from the browser for the script
1674
+ * that was loaded.
1675
+ */
1676
+ onScriptLoad: function (evt) {
1677
+ //Using currentTarget instead of target for Firefox 2.0's sake. Not
1678
+ //all old browsers will be supported, but this one was easy enough
1679
+ //to support and still makes sense.
1680
+ if (evt.type === 'load' ||
1681
+ (readyRegExp.test((evt.currentTarget || evt.srcElement).readyState))) {
1682
+ //Reset interactive script so a script node is not held onto for
1683
+ //to long.
1684
+ interactiveScript = null;
1685
+
1686
+ //Pull out the name of the module and the context.
1687
+ var data = getScriptData(evt);
1688
+ context.completeLoad(data.id);
1689
+ }
1690
+ },
1532
1691
 
1533
- return context;
1692
+ /**
1693
+ * Callback for script errors.
1694
+ */
1695
+ onScriptError: function (evt) {
1696
+ var data = getScriptData(evt);
1697
+ if (!hasPathFallback(data.id)) {
1698
+ return onError(makeError('scripterror', 'Script error', evt, [data.id]));
1699
+ }
1700
+ }
1701
+ });
1534
1702
  }
1535
1703
 
1536
1704
  /**
@@ -1547,20 +1715,21 @@ var requirejs, require, define;
1547
1715
  * on a require that are not standardized), and to give a short
1548
1716
  * name for minification/local scope use.
1549
1717
  */
1550
- req = requirejs = function (deps, callback) {
1718
+ req = requirejs = function (deps, callback, errback, optional) {
1551
1719
 
1552
1720
  //Find the right context, use default
1553
1721
  var contextName = defContextName,
1554
1722
  context, config;
1555
1723
 
1556
1724
  // Determine if have config object in the call.
1557
- if (!isArray(deps) && typeof deps !== "string") {
1725
+ if (!isArray(deps) && typeof deps !== 'string') {
1558
1726
  // deps is a config object
1559
1727
  config = deps;
1560
1728
  if (isArray(callback)) {
1561
1729
  // Adjust args if there are dependencies
1562
1730
  deps = callback;
1563
- callback = arguments[2];
1731
+ callback = errback;
1732
+ errback = optional;
1564
1733
  } else {
1565
1734
  deps = [];
1566
1735
  }
@@ -1570,14 +1739,16 @@ var requirejs, require, define;
1570
1739
  contextName = config.context;
1571
1740
  }
1572
1741
 
1573
- context = contexts[contextName] ||
1574
- (contexts[contextName] = newContext(contextName));
1742
+ context = contexts[contextName];
1743
+ if (!context) {
1744
+ context = contexts[contextName] = req.s.newContext(contextName);
1745
+ }
1575
1746
 
1576
1747
  if (config) {
1577
1748
  context.configure(config);
1578
1749
  }
1579
1750
 
1580
- return context.require(deps, callback);
1751
+ return context.require(deps, callback, errback);
1581
1752
  };
1582
1753
 
1583
1754
  /**
@@ -1595,31 +1766,29 @@ var requirejs, require, define;
1595
1766
  require = req;
1596
1767
  }
1597
1768
 
1598
- /**
1599
- * Global require.toUrl(), to match global require, mostly useful
1600
- * for debugging/work in the global space.
1601
- */
1602
- req.toUrl = function (moduleNamePlusExt) {
1603
- return contexts[defContextName].toUrl(moduleNamePlusExt);
1604
- };
1605
-
1606
1769
  req.version = version;
1607
1770
 
1608
1771
  //Used to filter out dependencies that are already paths.
1609
1772
  req.jsExtRegExp = /^\/|:|\?|\.js$/;
1773
+ req.isBrowser = isBrowser;
1610
1774
  s = req.s = {
1611
1775
  contexts: contexts,
1612
- //Stores a list of URLs that should not get async script tag treatment.
1613
- skipAsync: {}
1776
+ newContext: newContext
1614
1777
  };
1615
1778
 
1616
- req.isAsync = req.isBrowser = isBrowser;
1779
+ //Create default context.
1780
+ req({});
1781
+
1782
+ //Exports some context-sensitive methods on global require, using
1783
+ //default context if no context specified.
1784
+ addRequireMethods(req);
1785
+
1617
1786
  if (isBrowser) {
1618
- head = s.head = document.getElementsByTagName("head")[0];
1787
+ head = s.head = document.getElementsByTagName('head')[0];
1619
1788
  //If BASE tag is in play, using appendChild is a problem for IE6.
1620
1789
  //When that browser dies, this can be removed. Details in this jQuery bug:
1621
1790
  //http://dev.jquery.com/ticket/2709
1622
- baseElement = document.getElementsByTagName("base")[0];
1791
+ baseElement = document.getElementsByTagName('base')[0];
1623
1792
  if (baseElement) {
1624
1793
  head = s.head = baseElement.parentNode;
1625
1794
  }
@@ -1644,34 +1813,139 @@ var requirejs, require, define;
1644
1813
  * @param {Object} url the URL to the module.
1645
1814
  */
1646
1815
  req.load = function (context, moduleName, url) {
1647
- req.resourcesReady(false);
1816
+ var config = (context && context.config) || {},
1817
+ node;
1818
+ if (isBrowser) {
1819
+ //In the browser so use a script tag
1820
+ node = config.xhtml ?
1821
+ document.createElementNS('http://www.w3.org/1999/xhtml', 'html:script') :
1822
+ document.createElement('script');
1823
+ node.type = config.scriptType || 'text/javascript';
1824
+ node.charset = 'utf-8';
1825
+
1826
+ node.setAttribute('data-requirecontext', context.contextName);
1827
+ node.setAttribute('data-requiremodule', moduleName);
1828
+
1829
+ //Set up load listener. Test attachEvent first because IE9 has
1830
+ //a subtle issue in its addEventListener and script onload firings
1831
+ //that do not match the behavior of all other browsers with
1832
+ //addEventListener support, which fire the onload event for a
1833
+ //script right after the script execution. See:
1834
+ //https://connect.microsoft.com/IE/feedback/details/648057/script-onload-event-is-not-fired-immediately-after-script-execution
1835
+ //UNFORTUNATELY Opera implements attachEvent but does not follow the script
1836
+ //script execution mode.
1837
+ if (node.attachEvent &&
1838
+ //Check if node.attachEvent is artificially added by custom script or
1839
+ //natively supported by browser
1840
+ //read https://github.com/jrburke/requirejs/issues/187
1841
+ //if we can NOT find [native code] then it must NOT natively supported.
1842
+ //in IE8, node.attachEvent does not have toString()
1843
+ //Note the test for "[native code" with no closing brace, see:
1844
+ //https://github.com/jrburke/requirejs/issues/273
1845
+ !(node.attachEvent.toString && node.attachEvent.toString().indexOf('[native code') < 0) &&
1846
+ !isOpera) {
1847
+ //Probably IE. IE (at least 6-8) do not fire
1848
+ //script onload right after executing the script, so
1849
+ //we cannot tie the anonymous define call to a name.
1850
+ //However, IE reports the script as being in 'interactive'
1851
+ //readyState at the time of the define call.
1852
+ useInteractive = true;
1853
+
1854
+ node.attachEvent('onreadystatechange', context.onScriptLoad);
1855
+ //It would be great to add an error handler here to catch
1856
+ //404s in IE9+. However, onreadystatechange will fire before
1857
+ //the error handler, so that does not help. If addEvenListener
1858
+ //is used, then IE will fire error before load, but we cannot
1859
+ //use that pathway given the connect.microsoft.com issue
1860
+ //mentioned above about not doing the 'script execute,
1861
+ //then fire the script load event listener before execute
1862
+ //next script' that other browsers do.
1863
+ //Best hope: IE10 fixes the issues,
1864
+ //and then destroys all installs of IE 6-9.
1865
+ //node.attachEvent('onerror', context.onScriptError);
1866
+ } else {
1867
+ node.addEventListener('load', context.onScriptLoad, false);
1868
+ node.addEventListener('error', context.onScriptError, false);
1869
+ }
1870
+ node.src = url;
1871
+
1872
+ //For some cache cases in IE 6-8, the script executes before the end
1873
+ //of the appendChild execution, so to tie an anonymous define
1874
+ //call to the module name (which is stored on the node), hold on
1875
+ //to a reference to this node, but clear after the DOM insertion.
1876
+ currentlyAddingScript = node;
1877
+ if (baseElement) {
1878
+ head.insertBefore(node, baseElement);
1879
+ } else {
1880
+ head.appendChild(node);
1881
+ }
1882
+ currentlyAddingScript = null;
1648
1883
 
1649
- context.scriptCount += 1;
1650
- req.attach(url, context, moduleName);
1884
+ return node;
1885
+ } else if (isWebWorker) {
1886
+ //In a web worker, use importScripts. This is not a very
1887
+ //efficient use of importScripts, importScripts will block until
1888
+ //its script is downloaded and evaluated. However, if web workers
1889
+ //are in play, the expectation that a build has been done so that
1890
+ //only one script needs to be loaded anyway. This may need to be
1891
+ //reevaluated if other use cases become common.
1892
+ importScripts(url);
1651
1893
 
1652
- //If tracking a jQuery, then make sure its ready callbacks
1653
- //are put on hold to prevent its ready callbacks from
1654
- //triggering too soon.
1655
- if (context.jQuery && !context.jQueryIncremented) {
1656
- jQueryHoldReady(context.jQuery, true);
1657
- context.jQueryIncremented = true;
1894
+ //Account for anonymous modules
1895
+ context.completeLoad(moduleName);
1658
1896
  }
1659
1897
  };
1660
1898
 
1661
1899
  function getInteractiveScript() {
1662
- var scripts, i, script;
1663
1900
  if (interactiveScript && interactiveScript.readyState === 'interactive') {
1664
1901
  return interactiveScript;
1665
1902
  }
1666
1903
 
1667
- scripts = document.getElementsByTagName('script');
1668
- for (i = scripts.length - 1; i > -1 && (script = scripts[i]); i--) {
1904
+ eachReverse(scripts(), function (script) {
1669
1905
  if (script.readyState === 'interactive') {
1670
1906
  return (interactiveScript = script);
1671
1907
  }
1672
- }
1908
+ });
1909
+ return interactiveScript;
1910
+ }
1911
+
1912
+ //Look for a data-main script attribute, which could also adjust the baseUrl.
1913
+ if (isBrowser) {
1914
+ //Figure out baseUrl. Get it from the script tag with require.js in it.
1915
+ eachReverse(scripts(), function (script) {
1916
+ //Set the 'head' where we can append children by
1917
+ //using the script's parent.
1918
+ if (!head) {
1919
+ head = script.parentNode;
1920
+ }
1921
+
1922
+ //Look for a data-main attribute to set main script for the page
1923
+ //to load. If it is there, the path to data main becomes the
1924
+ //baseUrl, if it is not already set.
1925
+ dataMain = script.getAttribute('data-main');
1926
+ if (dataMain) {
1927
+
1928
+ //Pull off the directory of data-main for use as the
1929
+ //baseUrl.
1930
+ src = dataMain.split('/');
1931
+ mainScript = src.pop();
1932
+ subPath = src.length ? src.join('/') + '/' : './';
1933
+
1934
+ //Set final baseUrl if there is not already an explicit one.
1935
+ if (!cfg.baseUrl) {
1936
+ cfg.baseUrl = subPath;
1937
+ }
1938
+
1939
+ //Strip off any trailing .js since dataMain is now
1940
+ //like a module name.
1941
+ dataMain = mainScript.replace(jsSuffixRegExp, '');
1673
1942
 
1674
- return null;
1943
+ //Put the data-main script in the files to load.
1944
+ cfg.deps = cfg.deps ? cfg.deps.concat(dataMain) : [dataMain];
1945
+
1946
+ return true;
1947
+ }
1948
+ });
1675
1949
  }
1676
1950
 
1677
1951
  /**
@@ -1707,7 +1981,7 @@ var requirejs, require, define;
1707
1981
  if (callback.length) {
1708
1982
  callback
1709
1983
  .toString()
1710
- .replace(commentRegExp, "")
1984
+ .replace(commentRegExp, '')
1711
1985
  .replace(cjsRequireRegExp, function (match, dep) {
1712
1986
  deps.push(dep);
1713
1987
  });
@@ -1717,7 +1991,7 @@ var requirejs, require, define;
1717
1991
  //work though if it just needs require.
1718
1992
  //REQUIRES the function to expect the CommonJS variables in the
1719
1993
  //order listed below.
1720
- deps = (callback.length === 1 ? ["require"] : ["require", "exports", "module"]).concat(deps);
1994
+ deps = (callback.length === 1 ? ['require'] : ['require', 'exports', 'module']).concat(deps);
1721
1995
  }
1722
1996
  }
1723
1997
 
@@ -1727,9 +2001,9 @@ var requirejs, require, define;
1727
2001
  node = currentlyAddingScript || getInteractiveScript();
1728
2002
  if (node) {
1729
2003
  if (!name) {
1730
- name = node.getAttribute("data-requiremodule");
2004
+ name = node.getAttribute('data-requiremodule');
1731
2005
  }
1732
- context = contexts[node.getAttribute("data-requirecontext")];
2006
+ context = contexts[node.getAttribute('data-requirecontext')];
1733
2007
  }
1734
2008
  }
1735
2009
 
@@ -1740,324 +2014,24 @@ var requirejs, require, define;
1740
2014
  //occurs. If no context, use the global queue, and get it processed
1741
2015
  //in the onscript load callback.
1742
2016
  (context ? context.defQueue : globalDefQueue).push([name, deps, callback]);
1743
-
1744
- return undefined;
1745
2017
  };
1746
2018
 
1747
2019
  define.amd = {
1748
- multiversion: true,
1749
- plugins: true,
1750
2020
  jQuery: true
1751
2021
  };
1752
2022
 
2023
+
1753
2024
  /**
1754
2025
  * Executes the text. Normally just uses eval, but can be modified
1755
- * to use a more environment specific call.
2026
+ * to use a better, environment-specific call. Only used for transpiling
2027
+ * loader plugins, not for plain JS modules.
1756
2028
  * @param {String} text the text to execute/evaluate.
1757
2029
  */
1758
2030
  req.exec = function (text) {
2031
+ /*jslint evil: true */
1759
2032
  return eval(text);
1760
2033
  };
1761
2034
 
1762
- /**
1763
- * Executes a module callack function. Broken out as a separate function
1764
- * solely to allow the build system to sequence the files in the built
1765
- * layer in the right sequence.
1766
- *
1767
- * @private
1768
- */
1769
- req.execCb = function (name, callback, args, exports) {
1770
- return callback.apply(exports, args);
1771
- };
1772
-
1773
-
1774
- /**
1775
- * Adds a node to the DOM. Public function since used by the order plugin.
1776
- * This method should not normally be called by outside code.
1777
- */
1778
- req.addScriptToDom = function (node) {
1779
- //For some cache cases in IE 6-8, the script executes before the end
1780
- //of the appendChild execution, so to tie an anonymous define
1781
- //call to the module name (which is stored on the node), hold on
1782
- //to a reference to this node, but clear after the DOM insertion.
1783
- currentlyAddingScript = node;
1784
- if (baseElement) {
1785
- head.insertBefore(node, baseElement);
1786
- } else {
1787
- head.appendChild(node);
1788
- }
1789
- currentlyAddingScript = null;
1790
- };
1791
-
1792
- /**
1793
- * callback for script loads, used to check status of loading.
1794
- *
1795
- * @param {Event} evt the event from the browser for the script
1796
- * that was loaded.
1797
- *
1798
- * @private
1799
- */
1800
- req.onScriptLoad = function (evt) {
1801
- //Using currentTarget instead of target for Firefox 2.0's sake. Not
1802
- //all old browsers will be supported, but this one was easy enough
1803
- //to support and still makes sense.
1804
- var node = evt.currentTarget || evt.srcElement, contextName, moduleName,
1805
- context;
1806
-
1807
- if (evt.type === "load" || (node && readyRegExp.test(node.readyState))) {
1808
- //Reset interactive script so a script node is not held onto for
1809
- //to long.
1810
- interactiveScript = null;
1811
-
1812
- //Pull out the name of the module and the context.
1813
- contextName = node.getAttribute("data-requirecontext");
1814
- moduleName = node.getAttribute("data-requiremodule");
1815
- context = contexts[contextName];
1816
-
1817
- contexts[contextName].completeLoad(moduleName);
1818
-
1819
- //Clean up script binding. Favor detachEvent because of IE9
1820
- //issue, see attachEvent/addEventListener comment elsewhere
1821
- //in this file.
1822
- if (node.detachEvent && !isOpera) {
1823
- //Probably IE. If not it will throw an error, which will be
1824
- //useful to know.
1825
- node.detachEvent("onreadystatechange", req.onScriptLoad);
1826
- } else {
1827
- node.removeEventListener("load", req.onScriptLoad, false);
1828
- }
1829
- }
1830
- };
1831
-
1832
- /**
1833
- * Attaches the script represented by the URL to the current
1834
- * environment. Right now only supports browser loading,
1835
- * but can be redefined in other environments to do the right thing.
1836
- * @param {String} url the url of the script to attach.
1837
- * @param {Object} context the context that wants the script.
1838
- * @param {moduleName} the name of the module that is associated with the script.
1839
- * @param {Function} [callback] optional callback, defaults to require.onScriptLoad
1840
- * @param {String} [type] optional type, defaults to text/javascript
1841
- * @param {Function} [fetchOnlyFunction] optional function to indicate the script node
1842
- * should be set up to fetch the script but do not attach it to the DOM
1843
- * so that it can later be attached to execute it. This is a way for the
1844
- * order plugin to support ordered loading in IE. Once the script is fetched,
1845
- * but not executed, the fetchOnlyFunction will be called.
1846
- */
1847
- req.attach = function (url, context, moduleName, callback, type, fetchOnlyFunction) {
1848
- var node;
1849
- if (isBrowser) {
1850
- //In the browser so use a script tag
1851
- callback = callback || req.onScriptLoad;
1852
- node = context && context.config && context.config.xhtml ?
1853
- document.createElementNS("http://www.w3.org/1999/xhtml", "html:script") :
1854
- document.createElement("script");
1855
- node.type = type || (context && context.config.scriptType) ||
1856
- "text/javascript";
1857
- node.charset = "utf-8";
1858
- //Use async so Gecko does not block on executing the script if something
1859
- //like a long-polling comet tag is being run first. Gecko likes
1860
- //to evaluate scripts in DOM order, even for dynamic scripts.
1861
- //It will fetch them async, but only evaluate the contents in DOM
1862
- //order, so a long-polling script tag can delay execution of scripts
1863
- //after it. But telling Gecko we expect async gets us the behavior
1864
- //we want -- execute it whenever it is finished downloading. Only
1865
- //Helps Firefox 3.6+
1866
- //Allow some URLs to not be fetched async. Mostly helps the order!
1867
- //plugin
1868
- node.async = !s.skipAsync[url];
1869
-
1870
- if (context) {
1871
- node.setAttribute("data-requirecontext", context.contextName);
1872
- }
1873
- node.setAttribute("data-requiremodule", moduleName);
1874
-
1875
- //Set up load listener. Test attachEvent first because IE9 has
1876
- //a subtle issue in its addEventListener and script onload firings
1877
- //that do not match the behavior of all other browsers with
1878
- //addEventListener support, which fire the onload event for a
1879
- //script right after the script execution. See:
1880
- //https://connect.microsoft.com/IE/feedback/details/648057/script-onload-event-is-not-fired-immediately-after-script-execution
1881
- //UNFORTUNATELY Opera implements attachEvent but does not follow the script
1882
- //script execution mode.
1883
- if (node.attachEvent &&
1884
- // check if node.attachEvent is artificially added by custom script or
1885
- // natively supported by browser
1886
- // read https://github.com/jrburke/requirejs/issues/187
1887
- // if we can NOT find [native code] then it must NOT natively supported.
1888
- // in IE8, node.attachEvent does not have toString()
1889
- // TODO: a better way to check interactive mode
1890
- !(node.attachEvent.toString && node.attachEvent.toString().indexOf('[native code]') < 0) &&
1891
- !isOpera) {
1892
- //Probably IE. IE (at least 6-8) do not fire
1893
- //script onload right after executing the script, so
1894
- //we cannot tie the anonymous define call to a name.
1895
- //However, IE reports the script as being in "interactive"
1896
- //readyState at the time of the define call.
1897
- useInteractive = true;
1898
-
1899
-
1900
- if (fetchOnlyFunction) {
1901
- //Need to use old school onreadystate here since
1902
- //when the event fires and the node is not attached
1903
- //to the DOM, the evt.srcElement is null, so use
1904
- //a closure to remember the node.
1905
- node.onreadystatechange = function (evt) {
1906
- //Script loaded but not executed.
1907
- //Clear loaded handler, set the real one that
1908
- //waits for script execution.
1909
- if (node.readyState === 'loaded') {
1910
- node.onreadystatechange = null;
1911
- node.attachEvent("onreadystatechange", callback);
1912
- fetchOnlyFunction(node);
1913
- }
1914
- };
1915
- } else {
1916
- node.attachEvent("onreadystatechange", callback);
1917
- }
1918
- } else {
1919
- node.addEventListener("load", callback, false);
1920
- }
1921
- node.src = url;
1922
-
1923
- //Fetch only means waiting to attach to DOM after loaded.
1924
- if (!fetchOnlyFunction) {
1925
- req.addScriptToDom(node);
1926
- }
1927
-
1928
- return node;
1929
- } else if (isWebWorker) {
1930
- //In a web worker, use importScripts. This is not a very
1931
- //efficient use of importScripts, importScripts will block until
1932
- //its script is downloaded and evaluated. However, if web workers
1933
- //are in play, the expectation that a build has been done so that
1934
- //only one script needs to be loaded anyway. This may need to be
1935
- //reevaluated if other use cases become common.
1936
- importScripts(url);
1937
-
1938
- //Account for anonymous modules
1939
- context.completeLoad(moduleName);
1940
- }
1941
- return null;
1942
- };
1943
-
1944
- //Look for a data-main script attribute, which could also adjust the baseUrl.
1945
- if (isBrowser) {
1946
- //Figure out baseUrl. Get it from the script tag with require.js in it.
1947
- scripts = document.getElementsByTagName("script");
1948
-
1949
- for (globalI = scripts.length - 1; globalI > -1 && (script = scripts[globalI]); globalI--) {
1950
- //Set the "head" where we can append children by
1951
- //using the script's parent.
1952
- if (!head) {
1953
- head = script.parentNode;
1954
- }
1955
-
1956
- //Look for a data-main attribute to set main script for the page
1957
- //to load. If it is there, the path to data main becomes the
1958
- //baseUrl, if it is not already set.
1959
- if ((dataMain = script.getAttribute('data-main'))) {
1960
- if (!cfg.baseUrl) {
1961
- //Pull off the directory of data-main for use as the
1962
- //baseUrl.
1963
- src = dataMain.split('/');
1964
- mainScript = src.pop();
1965
- subPath = src.length ? src.join('/') + '/' : './';
1966
-
1967
- //Set final config.
1968
- cfg.baseUrl = subPath;
1969
- //Strip off any trailing .js since dataMain is now
1970
- //like a module name.
1971
- dataMain = mainScript.replace(jsSuffixRegExp, '');
1972
- }
1973
-
1974
- //Put the data-main script in the files to load.
1975
- cfg.deps = cfg.deps ? cfg.deps.concat(dataMain) : [dataMain];
1976
-
1977
- break;
1978
- }
1979
- }
1980
- }
1981
-
1982
- //See if there is nothing waiting across contexts, and if not, trigger
1983
- //resourcesReady.
1984
- req.checkReadyState = function () {
1985
- var contexts = s.contexts, prop;
1986
- for (prop in contexts) {
1987
- if (!(prop in empty)) {
1988
- if (contexts[prop].waitCount) {
1989
- return;
1990
- }
1991
- }
1992
- }
1993
- req.resourcesReady(true);
1994
- };
1995
-
1996
- /**
1997
- * Internal function that is triggered whenever all scripts/resources
1998
- * have been loaded by the loader. Can be overridden by other, for
1999
- * instance the domReady plugin, which wants to know when all resources
2000
- * are loaded.
2001
- */
2002
- req.resourcesReady = function (isReady) {
2003
- var contexts, context, prop;
2004
-
2005
- //First, set the public variable indicating that resources are loading.
2006
- req.resourcesDone = isReady;
2007
-
2008
- if (req.resourcesDone) {
2009
- //If jQuery with DOM ready delayed, release it now.
2010
- contexts = s.contexts;
2011
- for (prop in contexts) {
2012
- if (!(prop in empty)) {
2013
- context = contexts[prop];
2014
- if (context.jQueryIncremented) {
2015
- jQueryHoldReady(context.jQuery, false);
2016
- context.jQueryIncremented = false;
2017
- }
2018
- }
2019
- }
2020
- }
2021
- };
2022
-
2023
- //FF < 3.6 readyState fix. Needed so that domReady plugin
2024
- //works well in that environment, since require.js is normally
2025
- //loaded via an HTML script tag so it will be there before window load,
2026
- //where the domReady plugin is more likely to be loaded after window load.
2027
- req.pageLoaded = function () {
2028
- if (document.readyState !== "complete") {
2029
- document.readyState = "complete";
2030
- }
2031
- };
2032
- if (isBrowser) {
2033
- if (document.addEventListener) {
2034
- if (!document.readyState) {
2035
- document.readyState = "loading";
2036
- window.addEventListener("load", req.pageLoaded, false);
2037
- }
2038
- }
2039
- }
2040
-
2041
- //Set up default context. If require was a configuration object, use that as base config.
2035
+ //Set up with config info.
2042
2036
  req(cfg);
2043
-
2044
- //If modules are built into require.js, then need to make sure dependencies are
2045
- //traced. Use a setTimeout in the browser world, to allow all the modules to register
2046
- //themselves. In a non-browser env, assume that modules are not built into require.js,
2047
- //which seems odd to do on the server.
2048
- if (req.isAsync && typeof setTimeout !== "undefined") {
2049
- ctx = s.contexts[(cfg.context || defContextName)];
2050
- //Indicate that the script that includes require() is still loading,
2051
- //so that require()'d dependencies are not traced until the end of the
2052
- //file is parsed (approximated via the setTimeout call).
2053
- ctx.requireWait = true;
2054
- setTimeout(function () {
2055
- ctx.requireWait = false;
2056
-
2057
- if (!ctx.scriptCount) {
2058
- ctx.resume();
2059
- }
2060
- req.checkReadyState();
2061
- }, 0);
2062
- }
2063
- }());
2037
+ }(this));