nodejsjs模塊加載-創(chuàng)新互聯(lián)

本文地址:http://www.cnblogs.com/jasonxuli/p/4381747.htmlnodejsjs模塊加載

nodejs的非核心模塊(core module)加載主要使用的就是module.js。

10余年的松原網(wǎng)站建設經驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。網(wǎng)絡營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調整松原建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“松原網(wǎng)站設計”,“松原網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

項目主模塊(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?! ar compiledWrapper = runInThisContext(wrapper, { filename: filename });   // module.exports 和 exports 的區(qū)別:   // 這里只傳遞了exports的引用;而上面Module._load()返回的是module.exports,因此給module.exports賦值后,   // exports仍然指向之前的module.exports。  var 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://muchs.cn/article8/dspgop.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、外貿網(wǎng)站建設、品牌網(wǎng)站建設、企業(yè)建站、微信公眾號網(wǎng)站設計公司

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設