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

相关文章
相关标签/搜索