nodejs的非核心模塊(core module)加載主要使用的就是module.js。
項目主模塊(index.js/main.js等)加載使用的應該是module.js中的runMain(),其他js模塊加載流程基本上是:
1,獲取js文件信息;
2,new Module();
3,讀取js文件內容,封裝到一個function中,同時注入module本身,module.exports,包裝過的require函數(shù)等變量;
4,在某個上下文環(huán)境中執(zhí)行這個封裝后的function;
5,返回module.exports;
下面是摘出的主要代碼
module.js:
// Loads a module at the given file path. Returns that module's
// `exports` property.Module.prototype.require = function(path) {
assert(path,'missing path');
assert(util.isString(path),'path must be a string');
return Module._load(path, this);
};
// Check the cache for the requested file.
// 1. If a module already exists in the cache: return its exports object.
// 2. If the module is native: call `NativeModule.require()` with the
// filename and return the result.
// 3. Otherwise, create a new module for the file and save it to the cache.
// Then have it load the file contents before returning its exports
// object.Module._load(filename, this){
If(fileNamein Module._cache)
Return Module._cache[filename];
var module = new Module(filename, parent);
Module._cache[filename]= module;
Module.load(filename);
// 先把模塊放到緩存然后再去加載內容,可以解決循環(huán)依賴的問題。
// 參見https://nodejs.org/api/modules.html的Cycles部分。
return module.exports;
}
Module.prototype.load(filename){
var extension = path.extname(filename) || ‘.js’;
Module._extensions[extension](this, filename){
var content = fs.readFileSync(filename, ‘utf-8’);
module._compile(content, filename);
}
}
// Run the file contents in the correct scope or sandbox. Expose
// the correct helper variables (require, module, exports) to
// the file.
// Returns exception, if any.Module.prototype._compile = function(content, filename) {
function require(path) {
return self.require(path);
}
require.main= process.mainModule;
// Enable support to add extra extension types require.extensions = Module._extensions;
require.cache= Module._cache;
// TODO: 每個模塊加載到自己的context中會有什么不同?
// Set the environ variable NODE_MODULE_CONTEXTS=1 to make node load allmodules in their own context.
// Module._contextLoad = (+process.env['NODE_MODULE_CONTEXTS'] > 0);
// if (Module._contextLoad) { .... } // create wrapper function var wrapper = NativeModule.wrap(content);
// wrapper = '(function (exports, require, module, __filename, __dirname) { ' + content + '
});' // runInThisContext()可以認為是在某個上下文環(huán)境中執(zhí)行的eval()。
// compiledWrapper相當于eval(‘(function(){....})’)的結果,也就是真正的Function。 var compiledWrapper = runInThisContext(wrapper, { filename: filename });
// module.exports 和 exports 的區(qū)別:
// 這里只傳遞了exports的引用;而上面Module._load()返回的是module.exports,因此給module.exports賦值后,
// exports仍然指向之前的module.exports?! ar args = [self.exports, require, self, filename, dirname];
// 真正執(zhí)行compiledWrapper這個function,也就是執(zhí)行了filename.js的內容?! eturn compiledWrapper.apply(self.exports, args);
}
runInThisContext()就是vm.js里的runInThisContext(),vm.runInThisContext()調用了src/node_contextify.cc中的RunInThisContext()。
https://github.com/joyent/node/blob/master/lib/vm.js
https://github.com/joyent/node/blob/master/src/node_contextify.cc
擴展:
https://github.com/brianmcd/contextify
The main difference between Contextify and Node's vm methods is that Contextify allows asynchronous functions to continue executing in the Contextified object's context.
博客地址:http://www.cnblogs.com/jasonxuli/
分享名稱:nodejsjs模塊加載-創(chuàng)新互聯(lián)
鏈接分享:http://aaarwkj.com/article8/dspgop.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、外貿網站建設、品牌網站建設、企業(yè)建站、微信公眾號、網站設計公司
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容