module: don't require fs several times
authorRobert Kowalski <rok@kowalski.gd>
Thu, 3 Jul 2014 20:39:29 +0000 (22:39 +0200)
committerFedor Indutny <fedor@indutny.com>
Mon, 7 Jul 2014 10:40:22 +0000 (13:40 +0300)
As we are going to need fs in any case, just require it at the
beginning of the file.

Signed-off-by: Fedor Indutny <fedor@indutny.com>
lib/module.js

index d310f93..c090232 100644 (file)
@@ -24,6 +24,7 @@ var util = NativeModule.require('util');
 var runInThisContext = require('vm').runInThisContext;
 var runInNewContext = require('vm').runInNewContext;
 var assert = require('assert').ok;
+var fs = NativeModule.require('fs');
 
 
 // If obj.hasOwnProperty has been overridden, then calling
@@ -81,7 +82,6 @@ var debug = Module._debug;
 //   -> a/index.<ext>
 
 function statPath(path) {
-  var fs = NativeModule.require('fs');
   try {
     return fs.statSync(path);
   } catch (ex) {}
@@ -96,7 +96,6 @@ function readPackage(requestPath) {
     return packageMainCache[requestPath];
   }
 
-  var fs = NativeModule.require('fs');
   try {
     var jsonPath = path.resolve(requestPath, 'package.json');
     var json = fs.readFileSync(jsonPath, 'utf8');
@@ -131,7 +130,6 @@ Module._realpathCache = {};
 
 // check if the file exists and is not a directory
 function tryFile(requestPath) {
-  var fs = NativeModule.require('fs');
   var stats = statPath(requestPath);
   if (stats && !stats.isDirectory()) {
     return fs.realpathSync(requestPath, Module._realpathCache);
@@ -476,14 +474,14 @@ function stripBOM(content) {
 
 // Native extension for .js
 Module._extensions['.js'] = function(module, filename) {
-  var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
+  var content = fs.readFileSync(filename, 'utf8');
   module._compile(stripBOM(content), filename);
 };
 
 
 // Native extension for .json
 Module._extensions['.json'] = function(module, filename) {
-  var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
+  var content = fs.readFileSync(filename, 'utf8');
   try {
     module.exports = JSON.parse(stripBOM(content));
   } catch (err) {