Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix some odd bugs around compiling to JS and compilers that return an…

… object rather than a JS string
  • Loading branch information...
commit 56c529e502e7814b7b0420078d88b3e6404645de 1 parent 803b165
Isaac Z. Schlueter authored March 19, 2010

Showing 1 changed file with 49 additions and 14 deletions. Show diff stats Hide diff stats

  1. 63  lib/module.js
63  lib/module.js
@@ -90,10 +90,20 @@ function loadModuleSync (module) {
90 90
   var data = module.load();
91 91
   if ("string" === typeof data) {
92 92
     data = module.compile(data);
  93
+  } else if (data !== undefined) {
  94
+    module.exports = data;
93 95
   }
94 96
   if ("string" === typeof data && module.ext !== ".js") {
95 97
     // if it's not already JS, it can compile to a JS string.
96  
-    module.compile(data, ".js");
  98
+    module.compileJS(data);
  99
+  } else if (data !== undefined) {
  100
+    // this is a safe usage.  However, since it's just been
  101
+    // put in the cache, and has been the subject of a require(),
  102
+    // the lock may already be set.
  103
+    var locked = module.locked;
  104
+    module.locked = false;
  105
+    module.exports = data;
  106
+    module.locked = locked;
97 107
   }
98 108
   return module.exports;
99 109
 }
@@ -102,17 +112,26 @@ function loadModule (module, cb) {
102 112
     if (er) return cb(er);
103 113
     if ("string" === typeof data && module.ext !== ".js") {
104 114
       // if it's not already JS, it can compile to a JS string.
105  
-      return module.compile(data, ".js", cb);
  115
+      return module.compileJS(data, cb);
  116
+    } else if (data !== undefined) {
  117
+      // this is a safe usage.  However, since it's just been
  118
+      // put in the cache, and has been the subject of a require(),
  119
+      // the lock may already be set.
  120
+      var locked = module.locked;
  121
+      module.locked = false;
  122
+      module.exports = data;
  123
+      module.locked = locked;
106 124
     }
107 125
     cb(null, module.exports);
108 126
   }
109 127
   module.load(function (er, data) {
110 128
     if (er) return cb(er);
111 129
     if ("string" === typeof data) {
112  
-      module.compile(data, firstCompile);
113  
-    } else {
114  
-      cb(null, module.exports);
  130
+      return module.compile(data, firstCompile);
  131
+    } else if (data !== undefined) {
  132
+      module.exports = data;
115 133
     }
  134
+    cb(null, module.exports);
116 135
   });
117 136
 }
118 137
 
@@ -286,6 +305,10 @@ Module.prototype.load = function (cb) {
286 305
     loader = exports.extensionCache[ this.ext ];
287 306
     loader = loader && loader.load;
288 307
   }
  308
+  if (!loader && (this.ext !== ".js")) {
  309
+    loader = exports.extensionCache[ ".js" ];
  310
+    loader = loader && loader.load;
  311
+  }
289 312
   if (!loader) {
290 313
     var er = new Error("No loader found for "+this.uri);
291 314
     if (cb) cb(er);
@@ -295,19 +318,31 @@ Module.prototype.load = function (cb) {
295 318
   this.load = loader;
296 319
   return this.load(cb);
297 320
 }
298  
-Module.prototype.compile = function (code, cb) {
299  
-  var compiler = exports.extensionCache[ this.ext ];
  321
+// mod.compile(code, cb)
  322
+// if ext is provided, then pretend it's that kind of module.
  323
+function getCompiler (ext) {
  324
+  ext = ext || ".js";
  325
+  var compiler = exports.extensionCache[ ext ];
300 326
   compiler = compiler && compiler.compile;
301  
-  if (!compiler) {
302  
-    var er = new Error(
303  
-      "No compiler registered for "+this.ext+" modules");
304  
-    if (cb) cb(er);
305  
-    else throw er;
306  
-    return er;
  327
+  if (!compiler && (ext !== ".js")) {
  328
+    return getCompiler(".js");
307 329
   }
308  
-  this.compile = compiler;
  330
+  return compiler;
  331
+}
  332
+Module.prototype.compile = function (code, cb) {
  333
+  this.compile = getCompiler(this.ext);
309 334
   return this.compile(code, cb);
310 335
 }
  336
+Module.prototype.compileJS = function (code, cb) {
  337
+  // if its already been compiled as a JS module,
  338
+  // then don't compile it again.  Set to noop here
  339
+  // so that the logic is simpler later on.
  340
+  this.compileJS = getCompiler(".js");
  341
+  if (this.compile === this.compileJS) {
  342
+    this.compileJS = noop;
  343
+  }
  344
+  return this.compileJS(code, cb);
  345
+}
311 346
 Module.prototype.waitForChildren = function (cb) {
312 347
   var children = this.children,
313 348
     expecting = Object.keys(this.womb),

0 notes on commit 56c529e

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