node插件开发与发布

本文主要给各位分享如何快速创建node插件并发布到npm上。
npm是一个让JavaScript程序员分享和复用代码的工具,我们不仅可以install别人的插件,也可以publish自己的代码。

npm install太多,是时候publish一波

1.初始化一个node项目

我暂且将这个node插件命名为my-plugin,创建目录my-plugin,进入目录,使用npm init --yes默认方式初始化node项目,命令窗口命令如下:

$ mkdir my-plugin
$ cd my-plugin
$ npm init --yes

这时候在项目目录下生成了package.json,以下是文件的基本配置。

  "name": "my-plugin",
  "version": "1.0.0",
  "description": "一个简单的插件",
  "main": "myPlugin.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/xxx/my-plugin.git"
  },
  "keywords": [
    "my",
    "plugin"
  ],
  "author": "名字",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/xxx/my-plugin/issues"
  }
}

package.json相关字段说明如下

 
  • name 插件的名字

  • version 插件版本号

  • description 插件描述

  • author 作者名

  • main 入口文件路径,require(name)将根据这个路径来引入

  • keywords 关键词,使用数组形式,方便npm官网搜索

  • scripts 命令行,通过npm run 执行

  • license 许可证书,一般开源是MIT

  • repository github仓库项目地址

这里我将在项目目录下创建myPlugin.js作为主代码文件,因此需要将main字段设置为myPlugin.js,这样当别人install你的插件时,使用require('my-plugin')等同于require('node_modules/my-plugin/myPlugin.js')。

my-plugin目录下创建以下文件:

my-plugin
├── myPlugin.js                #主代码
├── LICENSE                     #许可证书
├── package.json                #npm配置文件
├── README.md                   #插件说明

LICENSE我们可以直接在github仓库上生成,步骤:create new file
-> 文件名输入LICENSE -> 右侧选择类型,这里一般选择MIT。

MIT License

Copyright (c) 2017 myName

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2.创建代码

下面我在myPlugin.js里实现一个简单的功能,创建一个对象,实现一个延迟函数sleep。

// myPlugin.js
function myPlugin() {
    return {
        sleep: sleep
    }
}
function sleep(long) {
  var start = Date.now();
  while((Date.now() - start) < long){
  }
  console.log('finish!');
}
module.exports = myPlugin();

上方代码使用module.exports导出对象,在node环境下,我们通过一下方式调用:

var myPlugin = require('my-plugin'); // or import myPlugin from 'my-plugin'
myPlugin.sleep(2000);

require最终如果开发的插件只是一个运行在node环境模块,只需 就可以,如上方代码,如果想要在浏览器端也能运行插件,我们就需要兼容各种构建环境了。以下代码借鉴了著名的q模块(早期实现promise的polyfill),将代码片段作为参数传进兼容器。

// myPlugin.js
(function (definition) {
    "use strict";
    // CommonJS
     if (typeof exports === "object" && typeof module === "object") {
        module.exports = definition();
    // RequireJS
    } else if (typeof define === "function" && define.amd) {
        define(definition);
    // 
// js
if (window.myPlugin) console.log('this is my plugin!');

3.发布

一个简单的node插件已经开发完了,现在我们要把它发布到npm官网上,供各位码友安(cai)装(keng)。首次发布,需要在npm官网上注册你的账号,下次直接npm login进行登录。

$ npm adduser //注册账号
Username: YOUR_USER_NAME
Password: YOUR_PASSWORD
Email: [email protected]
$ npm publish . //发布

现在,在npm官网上输入my-plugin便可以搜到你发布的包啦,你可以直接通过npm install my-pluginyarn add my-plugin将刚刚发布的插件安装到自己的项目。
最后,如果发现插件有bug了,修改后想要重新发布,直接执行npm push .会报错,由于npm检查到你发布的version版本已经存在,所以需要更新你的版本号才能重新发布,此时需要以下命令:

$ npm version patch

此时你会发现package.json的version字段由0.0.0提升至0.0.1,现在再执行npm publish,可以看到你的包已经在npm官网完成更新。

其它

以上是node插件发布的简单流程,实际上node插件开发还包含很多场景,一个大型的项目还需要考虑单元测试、代码压缩、集成测试等等。
一个完整的node插件目录结构一般如下:

大型node插件目录结构  

.
├── bin                         #运行目录
├── lib                         #主代码目录
├── example                     #示例目录
├── test                        #测试目录,提供单元测试
├── .travis.yml                 #集成自动测试配置
├── .npmignore                  #npm发布时忽略的文件
├── CHANGELOG.md               #版本更新说明
├── LICENSE                     #许可证书
├── package.json                #npm配置
├── README.md                   #README

https://www.cnblogs.com/adouwt/p/9211003.html (友情链接)

https://www.cnblogs.com/adouwt/p/9655594.html

 

 

你可能感兴趣的:(小白菜的插件集)