Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of http://github.com/kriszyp/transporter

  • Loading branch information...
commit d83fecc06e3e689876f13532cc5af7cc17f497ec 2 parents 4c9a124 + bf80bd6
@dvv dvv authored
Showing with 68 additions and 8 deletions.
  1. +16 −7 README.md
  2. +9 −1 lib/jsgi/transporter.js
  3. +43 −0 lib/loader.js
View
23 README.md
@@ -16,14 +16,10 @@ To use transporter, include the appliance in your JSGI stack:
var Transporter = require("transporter").Transporter;
exports.app = Transporter();
-Now you can use a client side module loader like require.js to load your modules (of
-course you should actually download transportD-require.js for local access):
+Now you can use a client side module loader like RequireJS, Yabble, or the simple loader
+that comes with transporter to load your modules:
- <script>
- // Configure RequireJS
- require = {baseUrl: "lib/"};
- </script>
- <script src="http://requirejs.org/docs/release/0.11.0/minified/transportD-require.js"></script>
+ <script src="transporter/loader.js"></script>
<script src="my-module.js"></script>
If my-module.js requires other modules (using a require call), these modules will
@@ -98,6 +94,19 @@ For example:
paths: ["/some/path"]
}, nextApp);
+Using RequireJS
+----------------
+
+You can use require.js to load your modules (of
+course you should actually download transportD-require.js for local access):
+
+ <script>
+ // Configure RequireJS
+ require = {baseUrl: "lib/"};
+ </script>
+ <script src="http://requirejs.org/docs/release/0.11.0/minified/transportD-require.js"></script>
+ <script src="my-module.js"></script>
+
Using Yabble
------------
View
10 lib/jsgi/transporter.js
@@ -54,6 +54,10 @@ exports.Transporter = function(options, app) {
},
body: {
forEach: function(write){
+ if(requestedModules[0] === "transporter/loader"){
+ write(loader("transporter/loader.js"));
+ requestedModules.shift();
+ }
if(converter.start){
converter.start(write);
}
@@ -80,7 +84,7 @@ exports.Transporter = function(options, app) {
if(requestedModules.length > 1){
write('require.def("' + requestedModules.join(",") + '", [], function(){});');
}
- if(autoRun){
+ if(autoRun && requestedModules.length){
write('require.ensure&&require.ensure(["' + requestedModules.join(",") + '"], function(require){' +
requestedModules.map(function(module){
return 'require("' + module + '");';
@@ -118,6 +122,10 @@ exports.Transporter = function(options, app) {
errors.push("/* Warning: " + (moduleName + " not found").replace(/\*/g,"") + "*/");
return;
}
+ if(moduleName === "transporter/loader"){
+ write(fileContents);
+ return true;
+ }
if(moduleName === "require"){
// require.js loads the require handler, which can't be embedded in
// require.def because require doesn't exist yet
View
43 lib/loader.js
@@ -0,0 +1,43 @@
+/** A very lightweight implementation of transport/D's require.define and
+ * require.ensure
+ */
+(function(){
+ var factories = {},
+ loadedModules = {};
+ function makeRequire(currentId){
+ return function(id){
+ if(id.charAt(0) === '.'){
+ id = currentId.substring(0, currentId.lastIndexOf('/') + 1) + id;
+ while(lastId !== id){
+ var lastId = id;
+ id = id.replace(/\/[^\/]*\/\.\.\//,'/');
+ }
+ id = id.replace(/\/\.\//g,'/');
+ }
+ var module = loadedModules[id];
+ if(module){
+ return module;
+ }
+ if(!factories[id]){
+ throw new Error("Module " + id + " not defined");
+ }
+ var module = factories[id](makeRequire(id), loadedModules[id] = {}, {});
+ if(module){
+ return loadedModules[id] = module;
+ }
+ return loadedModules[id];
+ };
+ }
+ require = {
+ define: function(modules){
+ for(var i in modules){
+ factories[i] = modules[i];
+ }
+ },
+ ensure: function(modules, callback){
+ var require = makeRequire("");
+ modules.forEach(require);
+ callback(require);
+ }
+ };
+})();
Please sign in to comment.
Something went wrong with that request. Please try again.