Skip to content
This repository
Browse code

Add support for transport/C and D modules

  • Loading branch information...
commit 7c0453f852a38303911937ddf420ffeee5cb6dc2 1 parent 26917b1
Kris Zyp authored

Showing 1 changed file with 41 additions and 11 deletions. Show diff stats Hide diff stats

  1. +41 11 lib/nodules.js
52 lib/nodules.js
@@ -186,16 +186,18 @@ function resolveUri(currentId, uri, mappings){
186 186 return uri;
187 187 }
188 188 }
189   -function getPackage(uri){
  189 +function getPackageUri(uri){
190 190 if(uri.substring(0,4) == "jar:"){
191 191 // if it is an archive, the root should be the package URI
192   - uri = uri.substring(0, uri.lastIndexOf('!') + 2);
  192 + return uri.substring(0, uri.lastIndexOf('!') + 2);
193 193 }
194 194 else{
195 195 // else try to base it on the path
196   - uri = uri.substring(0, uri.lastIndexOf('/lib/') + 1);
  196 + return uri.substring(0, uri.lastIndexOf('/lib/') + 1);
197 197 }
198   - return packages[uri] || {mappings: packages[""].mappings};
  198 +}
  199 +function getPackage(uri){
  200 + return packages[getPackageUri(uri)] || {mappings: packages[""].mappings};
199 201 }
200 202 function makeWorker(Constructor, currentId){
201 203 return Constructor && function(script, name){
@@ -213,14 +215,22 @@ function makeRequire(currentId){
213 215 return moduleExports[uri];
214 216 }
215 217 if(factories[uri]){
216   - var exports = moduleExports[uri] = {};
217 218 try{
218   - var module = modules[uri] = modules[uri] || new Module(uri);
  219 + var exports = moduleExports[uri] = {},
  220 + module = modules[uri] = modules[uri] || new Module(uri),
  221 + currentFile = cachePath(uri),
  222 + factory = factories[uri],
  223 + nextRequire = makeRequire(uri);
219 224 module.dependents[currentId] = true;
220   - var currentFile = cachePath(uri);
221   - exports = factories[uri].call(exports, makeRequire(uri), exports, module,
  225 + exports = factory.call(exports, nextRequire, exports, module,
222 226 currentFile, currentFile.replace(/\/[^\/]*$/,''))
223 227 || exports;
  228 + if(factory != factories[uri]){
  229 + // if a module was wrapped with the transport/D than the factory will get replaced
  230 + exports = factories[uri].call(exports, nextRequire, exports, module,
  231 + currentFile, currentFile.replace(/\/[^\/]*$/,''))
  232 + || exports;
  233 + }
224 234 var successful = true;
225 235 }
226 236 finally{
@@ -247,12 +257,32 @@ function makeRequire(currentId){
247 257 };
248 258 require.main = main;
249 259 require.define = function(moduleSet, dependencies){
250   - require.ensure(dependencies);
  260 + if(dependencies){
  261 + require.ensure(dependencies);
  262 + }
  263 + var context = getPackageUri(currentId) + "lib/";
251 264 for(var i in moduleSet){
252   - // TODO: Verify that id is an acceptably defined by the requested URL (shouldn't allow cross-domain definitions)
253   - factories[i] = moduleSet[i];
  265 + var moduleDef = moduleSet[i];
  266 + factories[context + i + ".js"] = moduleDef.factory || moduleDef;
254 267 }
255 268 };
  269 + require.def = function(id, dependencies, factory){
  270 + if(dependencies){
  271 + require.ensure(dependencies);
  272 + }else{
  273 + factory = dependencies;
  274 + }
  275 + factories[getPackageUri(currentId) + "lib/" + id + ".js"] = function(require, exports, module){
  276 + return factory.apply(exports, dependencies ? dependencies.map(function(id){
  277 + switch(id){
  278 + case "require": return require;
  279 + case "exports" : return exports;
  280 + case "module" : return module;
  281 + default: return require(id);
  282 + }
  283 + }) : []);
  284 + };
  285 + };
256 286 require.paths = paths;
257 287 require.reloadable = reloadable;
258 288 require.resource = function(uri){

0 comments on commit 7c0453f

Please sign in to comment.
Something went wrong with that request. Please try again.