Skip to content
Browse files

'return' value from a module sets module.exports

  • Loading branch information...
1 parent 555665f commit 2ca46366905f6351df05e857e906986d1c90955e @isaacs committed Mar 29, 2012
Showing with 16 additions and 2 deletions.
  1. +16 −2 lib/module.js
View
18 lib/module.js
@@ -440,8 +440,22 @@ Module.prototype._compile = function(content, filename) {
global.v8debug.Debug.setBreakPoint(compiledWrapper, 0, 0);
}
}
- var args = [self.exports, require, self, filename, dirname];
- return compiledWrapper.apply(self.exports, args);
+ var exports = self.exports;
+ var args = [exports, require, self, filename, dirname];
+ var ret = compiledWrapper.apply(self.exports, args);
+
+ // if the wrapper returns a value, and has not already
+ // set module.exports to something different, and has not
+ // added any keys to exports, then use the return value as
+ // the exported module result.
+ if (ret !== undefined && self.exports === exports &&
+ Object.keys(exports).length === 0) {
+ self.exports = ret;
+ }
+
+ // always return the return of the compilation.
+ // this is needed for node --eval
+ return ret;
};

0 comments on commit 2ca4636

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