diff options
Diffstat (limited to 'node_modules/montage/require/require.js')
-rwxr-xr-x | node_modules/montage/require/require.js | 205 |
1 files changed, 86 insertions, 119 deletions
diff --git a/node_modules/montage/require/require.js b/node_modules/montage/require/require.js index 8273b660..aad6cef7 100755 --- a/node_modules/montage/require/require.js +++ b/node_modules/montage/require/require.js | |||
@@ -33,45 +33,46 @@ | |||
33 | if (!this) | 33 | if (!this) |
34 | throw new Error("Require does not work in strict mode."); | 34 | throw new Error("Require does not work in strict mode."); |
35 | 35 | ||
36 | var global = this; | ||
37 | var globalEval = eval; // reassigning causes eval to not use lexical scope. | 36 | var globalEval = eval; // reassigning causes eval to not use lexical scope. |
38 | 37 | ||
39 | // Non-CommonJS speced extensions should be marked with an "// EXTENSION" comment. | 38 | // Non-CommonJS speced extensions should be marked with an "// EXTENSION" comment. |
40 | 39 | ||
41 | // Sandbox is an instance of the loader system. Different sandboxes will have different instances of modules. | 40 | Require.makeRequire = function (config) { |
42 | // Returns the root "require" function. If this root "require()" function is called the loader will be in synchronous mode. | 41 | var require; |
43 | // To get asynchronous loading you MUST call the root "require.async()". In async mode all subsequent calls to "require()" will | 42 | |
44 | // be asynchronously loaded, and synchronously executed. | ||
45 | Require.Sandbox = function(config) { | ||
46 | // Configuration defaults: | 43 | // Configuration defaults: |
47 | config = config || {}; | 44 | config = config || {}; |
48 | config.location = URL.resolve(config.location || Require.getLocation(), "."); | 45 | config.location = URL.resolve(config.location || Require.getLocation(), "."); |
49 | config.lib = URL.resolve(config.location, config.lib || "."); | 46 | config.lib = URL.resolve(config.location, config.lib || "."); |
50 | config.paths = config.paths || [config.lib]; | 47 | config.paths = config.paths || [config.lib]; |
51 | config.mappings = config.mappings || {}; // EXTENSION | 48 | config.mappings = config.mappings || {}; // EXTENSION |
52 | config.exposedConfigs = config.exposedConfigs || Require.defaultExposedConfigs; | 49 | config.exposedConfigs = config.exposedConfigs || Require.exposedConfigs; |
53 | config.makeLoader = config.makeLoader || Require.DefaultLoaderConstructor; | 50 | config.makeLoader = config.makeLoader || Require.makeLoader; |
54 | config.load = config.load || config.makeLoader(config); | 51 | config.load = config.load || config.makeLoader(config); |
55 | config.makeCompiler = config.makeCompiler || Require.DefaultCompilerConstructor; | 52 | config.makeCompiler = config.makeCompiler || Require.makeCompiler; |
56 | config.compile = config.compile || config.makeCompiler(config); | 53 | config.compile = config.compile || config.makeCompiler(config); |
57 | 54 | ||
58 | // Sandbox state: | ||
59 | // Modules: { exports, id, location, directory, factory, dependencies, dependees, text, type } | 55 | // Modules: { exports, id, location, directory, factory, dependencies, dependees, text, type } |
60 | var modules = config.modules = config.modules || {}; | 56 | var modules = config.modules = config.modules || {}; |
61 | // Mapping from canonical IDs to the initial top ID used to load module | ||
62 | var locationsToIds = {}; | ||
63 | 57 | ||
58 | // produces an entry in the module state table, which gets built | ||
59 | // up through loading and execution, ultimately serving as the | ||
60 | // ``module`` free variable inside the corresponding module. | ||
64 | function getModule(id) { | 61 | function getModule(id) { |
65 | if (!has(modules, id)) { | 62 | if (!has(modules, id)) { |
66 | modules[id] = { | 63 | modules[id] = { |
67 | id: id, | 64 | id: id, |
68 | display: config.location + "#" + id // EXTENSION | 65 | display: config.location + "#" + id, // EXTENSION |
66 | require: require, | ||
69 | }; | 67 | }; |
70 | } | 68 | } |
71 | return modules[id]; | 69 | return modules[id]; |
72 | } | 70 | } |
73 | config.module = getModule; | ||
74 | 71 | ||
72 | // for preloading modules by their id and exports, useful to | ||
73 | // prevent wasteful multiple instantiation if a module was loaded | ||
74 | // in the bootstrapping process and can be trivially injected into | ||
75 | // the system. | ||
75 | function inject(id, exports) { | 76 | function inject(id, exports) { |
76 | var module = getModule(id) | 77 | var module = getModule(id) |
77 | module.exports = exports; | 78 | module.exports = exports; |
@@ -83,59 +84,55 @@ | |||
83 | var load = memoize(function (topId, viaId) { | 84 | var load = memoize(function (topId, viaId) { |
84 | var module = getModule(topId); | 85 | var module = getModule(topId); |
85 | return Promise.call(function () { | 86 | return Promise.call(function () { |
86 | // already loaded, already instantiated, or redirection | 87 | // if not already loaded, already instantiated, or |
88 | // configured as a redirection to another module | ||
87 | if ( | 89 | if ( |
88 | module.factory !== void 0 || | 90 | module.factory === void 0 && |
89 | module.exports !== void 0 || | 91 | module.exports === void 0 && |
90 | module.redirect !== void 0 | 92 | module.redirect === void 0 |
91 | ) { | 93 | ) { |
92 | return module; | 94 | // load and |
93 | // load | 95 | // trace progress |
94 | } else { | ||
95 | Require.progress.requiredModules.push(module.display); | 96 | Require.progress.requiredModules.push(module.display); |
96 | return Promise.call(config.load, null, topId, module) | 97 | return Promise.call(config.load, null, topId, module) |
97 | .then(function () { | 98 | .then(function () { |
98 | Require.progress.loadedModules.push(module.display); | 99 | Require.progress.loadedModules.push(module.display); |
99 | return module; | ||
100 | }); | 100 | }); |
101 | } | 101 | } |
102 | }) | 102 | }) |
103 | .then(function (module) { | 103 | .then(function () { |
104 | // analyze dependencies | 104 | // compile and analyze dependencies |
105 | config.compile(module); | 105 | config.compile(module); |
106 | var dependencies = module.dependencies = module.dependencies || []; | 106 | var dependencies = module.dependencies = module.dependencies || []; |
107 | if (module.redirect !== void 0) { | 107 | if (module.redirect !== void 0) { |
108 | dependencies.push(module.redirect); | 108 | dependencies.push(module.redirect); |
109 | } | 109 | } |
110 | return module; | ||
111 | }); | 110 | }); |
112 | }); | 111 | }); |
113 | 112 | ||
114 | // Load a module definition, and the definitions of its transitive | 113 | // Load a module definition, and the definitions of its transitive |
115 | // dependencies | 114 | // dependencies |
116 | function deepLoad(id, viaId, loading) { | 115 | function deepLoad(topId, viaId, loading) { |
116 | var module = getModule(topId); | ||
117 | // this is a memo of modules already being loaded so we don’t | 117 | // this is a memo of modules already being loaded so we don’t |
118 | // data-lock on a cycle of dependencies. | 118 | // data-lock on a cycle of dependencies. |
119 | loading = loading || {}; | 119 | loading = loading || {}; |
120 | // has this all happened before? will it happen again? | 120 | // has this all happened before? will it happen again? |
121 | if (has(loading, id)) | 121 | if (has(loading, topId)) |
122 | return; // break the cycle of violence. | 122 | return; // break the cycle of violence. |
123 | loading[id] = true; // this has happened before | 123 | loading[topId] = true; // this has happened before |
124 | return load(id, viaId) | 124 | return load(topId, viaId) |
125 | .then(function (module) { | 125 | .then(function () { |
126 | // load the transitive dependencies using the magic of | 126 | // load the transitive dependencies using the magic of |
127 | // recursion. | 127 | // recursion. |
128 | return Promise.all(module.dependencies.map(function (depId) { | 128 | return Promise.all(module.dependencies.map(function (depId) { |
129 | depId = resolve(depId, id) | 129 | depId = resolve(depId, topId) |
130 | // create dependees set, purely for debug purposes | 130 | // create dependees set, purely for debug purposes |
131 | var module = getModule(depId); | 131 | var module = getModule(depId); |
132 | var dependees = module.dependees = module.dependees || {}; | 132 | var dependees = module.dependees = module.dependees || {}; |
133 | dependees[id] = true; | 133 | dependees[topId] = true; |
134 | return deepLoad(depId, id, loading); | 134 | return deepLoad(depId, topId, loading); |
135 | })) | 135 | })) |
136 | .then(function () { | ||
137 | return module; | ||
138 | }) | ||
139 | }) | 136 | }) |
140 | } | 137 | } |
141 | 138 | ||
@@ -169,7 +166,7 @@ | |||
169 | // Execute the factory function: | 166 | // Execute the factory function: |
170 | var returnValue = module.factory.call( | 167 | var returnValue = module.factory.call( |
171 | // in the context of the module: | 168 | // in the context of the module: |
172 | global, // this | 169 | void 0, // this (defaults to global) |
173 | makeRequire(topId), // require | 170 | makeRequire(topId), // require |
174 | module.exports, // exports | 171 | module.exports, // exports |
175 | module // module | 172 | module // module |
@@ -177,7 +174,10 @@ | |||
177 | 174 | ||
178 | // Modules should never have a return value. | 175 | // Modules should never have a return value. |
179 | if (returnValue !== void 0) { | 176 | if (returnValue !== void 0) { |
180 | console.warn('require: module "'+topId+'" returned a value.'); | 177 | console.warn( |
178 | 'require: module ' + JSON.stringify(topId) + | ||
179 | ' returned a value.' | ||
180 | ); | ||
181 | } | 181 | } |
182 | 182 | ||
183 | // Update the list of modules that are ready to use | 183 | // Update the list of modules that are ready to use |
@@ -262,7 +262,8 @@ | |||
262 | return require; | 262 | return require; |
263 | } | 263 | } |
264 | 264 | ||
265 | return makeRequire(""); | 265 | require = makeRequire(""); |
266 | return require; | ||
266 | }; | 267 | }; |
267 | 268 | ||
268 | Require.progress = { | 269 | Require.progress = { |
@@ -271,7 +272,7 @@ | |||
271 | initializedModules: [] | 272 | initializedModules: [] |
272 | }; | 273 | }; |
273 | 274 | ||
274 | Require.PackageSandbox = function (location, config) { | 275 | Require.loadPackage = function (location, config) { |
275 | location = URL.resolve(location, "."); | 276 | location = URL.resolve(location, "."); |
276 | config = config || {}; | 277 | config = config || {}; |
277 | var loadingPackages = config.loadingPackages = config.loadingPackages || {}; | 278 | var loadingPackages = config.loadingPackages = config.loadingPackages || {}; |
@@ -304,7 +305,7 @@ | |||