Skip to content
Browse files

Support defining a loader

only require file if necessary
  • Loading branch information...
1 parent 1a7f6d1 commit cc6ed2e5334e35ed392a467a73a72844e07d8427 @kriszyp committed
Showing with 103 additions and 47 deletions.
  1. +103 −47 lib/jsgi/transporter.js
View
150 lib/jsgi/transporter.js
@@ -1,13 +1,24 @@
-var file = require("file");
-exports.Transporter = function(app, options) {
+//TODO: optionally write to target file
+exports.Transporter = function(options, app) {
var options = options || {},
prefix = options.urlPrefix || "/lib/",
+ resolveDeps = "resolveDeps" in options ? options.resolveDeps : true,
paths = options.paths || require.paths.map(function(path){
return path.replace(/[\\\/]engines[\\\/](\w*)/,function(t, engine){
return "/engines/" + (engine === "default" ? "default" : "browser");
});
- });
+ }),
+ loader = options.loader || function(id){
+ var file = require("file");
+ for (var i = 0; i < paths.length; i++){
+ var path = file.join(paths[i], id);
+ if(file.isFile(path)){
+ return file.read(path)
+ }
+ }
+ },
+ converter = options.converter || exports.CommonJS;
return function(request) {
var modules, isPaused, path = request.pathInfo;
@@ -30,9 +41,7 @@ exports.Transporter = function(app, options) {
}
return module;
});
- if(findPath(requestedModules[0] + ".js")){
- requestedModules = requestedModules;
-
+ if(loader(requestedModules[0] + ".js")){
modules = {};
return {
status: 200,
@@ -43,13 +52,13 @@ exports.Transporter = function(app, options) {
if(module.charAt(0) === "-"){
module = module.substring(1);
// find all the module and deps treat them as already loaded
- load(module);
+ loadModule(module);
return;
}
return true;
}).forEach(function(module){
if(module){
- load(module, write);
+ loadModule(module, write);
}
});
if(isPaused){
@@ -68,15 +77,7 @@ exports.Transporter = function(app, options) {
headers: {},
body: ["Not found"]
};
- function findPath(moduleName){
- for (var i = 0; i < paths.length; i++){
- var path = file.join(paths[i], moduleName);
- if(file.isFile(path)){
- return path;
- }
- }
- }
- function load(moduleName, write){
+ function loadModule(moduleName, write){
if(modules[moduleName]){
if(modules[moduleName] === 1 && !isPaused && write){
// we need to pause to get all the dependencies loaded
@@ -86,47 +87,102 @@ exports.Transporter = function(app, options) {
return;
}
modules[moduleName] = 1; // in progress
- var path = findPath(moduleName + ".js");
- if(!path && write){
- debugger;
+ var fileContents = loader(moduleName + ".js");
+ if(!fileContents && write){
write('console.error(' + JSON.stringify(moduleName) + ' + " not found");\n');
return;
}
- var fileContents = file.read(path);
if(moduleName === "require"){
// require.js loads the require handler, which can't be embedded in
// require.def because require doesn't exist yet
+ // also hand-coded modules that define their require.def's don't need wrapping/resolution,
+ // they can be directly written
var baseUrl = request.pathInfo.substring(0, request.pathInfo.lastIndexOf("/") + 1);
// we set the base url in the require config so that it knows where to load from
- write('require={baseUrl:"' + baseUrl + '"};');
+ write('require={baseUrlMatch:RegExp("' + requestedModules[0] + '")};');
write(fileContents);
return;
}
- var deps = [];
- var depNames = {};
- var baseModule = moduleName.substring(0, moduleName.lastIndexOf("/") + 1);
- fileContents.replace(/require\s*\(\s*['"]([^'"]*)['"]\s*\)/g, function(t, moduleId){
- if(moduleId.charAt(0) == "."){
- // handle relative references
- moduleId = (baseModule + moduleId).replace(/\/\.\.\/[^\/]*/g,'').replace(/\.\//g,'');
- }
- if(depNames[moduleId]){
- return;
- }
- depNames[moduleId] = true;
- deps.push(moduleId);
- load(moduleId, write);
- });
- modules[moduleName] = 2; // finished
- if(write){
- write('require.def("');
- write(moduleName);
- write('", ["require", "exports", "module"');
- write((deps.length ? ', "' + deps.join('", "') + '"' : '') + '], ');
- write('function(require, exports, module) {\n');
- write(fileContents);
- write('\n});\n');
- }
+ if(fileContents.indexOf("require.def(") > -1){
+ // also hand-coded modules that define their require.def's don't need wrapping/resolution,
+ // they can be directly written
+ write(fileContents);
+ return;
+ }
+ converter(moduleName, fileContents, resolveDeps ? loadModule : function(){}, modules, write);
+ modules[moduleName] = 2; // finished
}
}
}
+
+// CommonJS module wrapper
+exports.CommonJS = function(moduleName, fileContents, loadModule, modules, write){
+ var deps = [];
+ var depNames = {};
+ var baseModule = moduleName.substring(0, moduleName.lastIndexOf("/") + 1);
+ fileContents.replace(/require\s*\(\s*['"]([^'"]*)['"]\s*\)/g, function(t, moduleId){
+ if(moduleId.charAt(0) == "."){
+ // handle relative references
+ moduleId = (baseModule + moduleId).replace(/\/\.\.\/[^\/]*/g,'').replace(/\.\//g,'');
+ }
+ if(depNames[moduleId]){
+ return;
+ }
+ depNames[moduleId] = true;
+ deps.push(moduleId);
+ loadModule(moduleId, write);
+ });
+ if(write){
+ write('require.def("');
+ write(moduleName);
+ write('", ["require", "exports", "module"');
+ write((deps.length ? ', "' + deps.join('", "') + '"' : '') + '], ');
+ write('function(require, exports, module) {');
+ write(fileContents);
+ write('\n});\n');
+ }
+};
+
+// Dojo module wrapper
+exports.Dojo = function(moduleName, fileContents, loadModule, modules, write){
+ var deps = [];
+ var depNames = {};
+ var baseModule = moduleName.substring(0, moduleName.lastIndexOf("/") + 1);
+ fileContents.replace(/dojo\.require\s*\(\s*['"]([^'"]*)['"]\s*\)/g, function(t, moduleId){
+ moduleId = moduleId.replace(/\./g,'/');
+ if(depNames[moduleId]){
+ return;
+ }
+ depNames[moduleId] = true;
+ deps.push(moduleId);
+ loadModule(moduleId, write);
+ });
+ if(write){
+ write(fileContents);
+ }
+};
+
+// Dojo module wrapper
+exports.DojoRequireJS = function(moduleName, fileContents, loadModule, modules, write){
+ var deps = [];
+ var depNames = {};
+ var baseModule = moduleName.substring(0, moduleName.lastIndexOf("/") + 1);
+ fileContents.replace(/dojo\.require\s*\(\s*['"]([^'"]*)['"]\s*\)/g, function(t, moduleId){
+ moduleId = moduleId.replace(/\./g,'/');
+ if(depNames[moduleId]){
+ return;
+ }
+ depNames[moduleId] = true;
+ deps.push(moduleId);
+ loadModule(moduleId, write);
+ });
+ if(write){
+ write('require.def("');
+ write(moduleName);
+ write('", [');
+ write((deps.length ? '"' + deps.join('", "') + '"' : '') + '], ');
+ write('function() {');
+ write(fileContents);
+ write('\n});\n');
+ }
+};

0 comments on commit cc6ed2e

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