nw.js node-webkit系列(15)如何使用內部模塊和第三方模塊進行開發

原文連接:http://blog.csdn.net/zeping891103/article/details/50786259javascript

原諒原版連接:https://github.com/nwjs/nw.js/wiki/Using-Node-moduleshtml

Node.js中有三種模塊類型:java

1)內部模塊(部分Node API)node

2)用JavaScript寫的第三方模塊jquery

3)C/C++插件的第三方模塊git

這些全部的模塊類型,都在會在node-webkit中使用到。你能夠在Node's wiki中找到不少這方面的資料和開源代碼。github

web

https://github.com/nodejs/node-v0.x-archive/wiki/Modulesnpm

https://www.npmjs.com/json

 

(一)Internal modules(內部模塊)

內部模塊的Node.js一樣能夠在node-webkit中直接使用,詳細請查看Node.js的API:

https://nodejs.org/docs/latest/api/

例如,你可使用var fs = require('fs')直接啓動Node.js的File System的API:

https://nodejs.org/docs/latest/api/fs.html

例如,你能夠直接使用 the process 模塊(沒有任何require(....))。然而,建議使用Node.js的API儘可能使用require(....)語句來調用,如the process 模塊使用爲require(process)。

(注):當前,Node.js API 和在node-webkit的Node.js仍是有些區別的,能夠參考:

https://github.com/nwjs/nw.js/wiki/Changes-related-to-node

(注):Node.js API參考以下:

https://nodejs.org/docs/latest/api/

 

(二)3rd party JavaScript modules(用JavaScript寫的第三方模塊)

若是第三方模塊用純JavaScript寫,即不包含任何C/C++插件代碼,那麼這個模塊也node-webkit中一樣也可使用Node內部模塊(require(...))。但這裏須要重點注意一個問題:

想要使用JavaScript編寫的第三方模塊,你的應用的根目錄必須有一個命名爲node_modules的文件夾,該文件夾爲node-webkit默認使用JavaScript寫的第三方模塊使用目錄。假設有個第三方JavaScript模塊名爲a_modules,有兩種調用方法:

1)若是使用require(a_modules)的方法調用,則無需添加任何導入語句。

2)若是使用像jQuery的方法調用,如a_modules.(...),則須要添加導入語句<script src="..."> 。

下面咱們主要介紹第一種調用狀況,由於該調用方法能夠很好地隱藏了調用的相對地址,並且會更加便捷。

(1)將已經嵌入到node-webkit的內部模塊代碼獲取至源碼根目錄的node_modules文件夾

這種方法可讓開發者閱讀到內部模塊的源碼及對其進行擴展。下面之內部模塊之一的async爲例。正常狀況下,咱們在無需添加導入語句,便可使用async,只需調用以下語句:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. var async = require('async');  


下面咱們將介紹如何獲取async的類庫源碼,如下爲Windows系統環境爲例:

 

只需調用命令行便可

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. cd /path/to/your/app  
  2. npm install async  


這樣你就能夠獲取該類庫源碼,源碼位置在你的項目根目錄node_modules的文件夾

 

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. .  
  2. ./package.json  
  3. ./index.html  
  4. ./node_modules  
  5. ./node_modules/async  
  6. ./node_modules/async/.gitmodules  
  7. ./node_modules/async/package.json  
  8. ./node_modules/async/Makefile  
  9. ./node_modules/async/LICENSE  
  10. ./node_modules/async/README.md  
  11. ./node_modules/async/.npmignore  
  12. ./node_modules/async/lib  
  13. ./node_modules/async/lib/async.js  
  14. ./node_modules/async/index.js  


這時候你就能夠查閱並擴展async模塊。

 

(注):博主不建議隨意擴展官方已提供的內部模塊,但能夠擴充內部模塊。

 

(2)使用第三方或本身編寫的類庫,擴充內部模塊。

假設你有一個類庫yy庫,你想在你的應用中可使用require(yy)的方法進行調用,內部擴充了一個yy庫,該如何作呢?

1)在你的項目根目錄下新建文件夾node_modules,在該文件夾中新建yy文件夾,做爲你調用的yy庫的地址。

dist目錄和lib目錄下的yy.js就是你要編寫的yy庫的源碼文件,而yy庫下package.json文件則是yy庫的配置文件。

 

2)yy.js編寫的代碼格式以下:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. (function() {  
  2.   
  3.     var yy = {};  
  4.     yy.hello = function() {  
  5.         return "hello";  
  6.     };  
  7.   
  8.         // Establish the root object, `window` (`self`) in the browser, `global`  
  9.         // on the server, or `this` in some virtual machines. We use `self`  
  10.         // instead of `window` for `WebWorker` support.  
  11.     var root = typeof self === 'object' && self.self === self && self ||  
  12.         typeof global === 'object' && global.global === global && global ||  
  13.         this;  
  14.   
  15.     // Node.js  
  16.     if (typeof module === 'object' && module.exports) {  
  17.         module.exports = yy;  
  18.     }  
  19.     // AMD / RequireJS  
  20.     else if (typeof define === 'function' && define.amd) {  
  21.         define([], function() {  
  22.             return yy;  
  23.         });  
  24.     }  
  25.     // included directly via <script> tag  
  26.     else {  
  27.         root.yy = yy;  
  28.     }  
  29.   
  30. }());  


3)yy庫下package.json文件內容以下:

 

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. {  
  2.   "name": "yy",  
  3.   "description": "yy lib",  
  4.   "main": "lib/yy.js",  
  5.   "files": [  
  6.     "lib",  
  7.     "dist/yy.js"  
  8.   ]  
  9. }  


這樣你就能夠在你的應用中使用yy庫

 

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. <script>  
  2.     var yy = require('yy');  
  3.     console.log(yy.hello());  
  4. </script>  


(三)3rd party modules with C/C++ addons(C/C++插件的第三方模塊)

 

這塊內容較爲複雜,對於通常的開發者也比較少用,同時因爲博主對C/C++不是很熟悉,待有空時從新撿起C/C++,再作補充。如須要了解的開發者仍能夠閱讀以下地址:

https://github.com/nwjs/nw.js/wiki/Using-Node-modules

相關文章
相關標籤/搜索