vuejs 插件開發並發布到npm--(2)js插件開發
阿新 • • 發佈:2019-02-12
test eth ted nds 如何使用 pack 好的 port 格式
一、以日期格式轉化函數插件開發為例
1、生成項目文件夾formateDate;
2、在formateDate文件夾下生成index.js文件,並執行npm init生成package.json文件;
3、在formateDate文件夾下生產lib文件夾,用於進行插件開發;
4、在lib文件夾下新建formateDate.js並進行開發。
index.js
var formateDate = require(‘./lib/formateDate.js‘); module.exports = { dateFMT: formateDate.dateFMT }
package.json
{"name": "hyy-formatedate", "version": "1.0.0", "description": "simple formatedate", "main": "index.js", "scripts": { "watch": "mocha --watch tests/", "test": "mocha --reporter spec --timeout 2000 --recursive tests/", "test-cov": "istanbul cover ./node_modules/mocha/bin/_mocha -- -t 2000 --recursive -R spec tests/","test-html": "mocha --reporter mochawesome tests/" }, "bin": { "formateDate": "./bin/formateDate.js" }, "keywords": [ "formateDate" ], "author": "yyhu", "license": "MIT", "devDependencies": { "chai": "^3.5.0", "istanbul": "^0.4.1", "mocha": "^2.3.4", "mochawesome": "^1.2.1" } }
formateDate.js
module.exports = { /** * @param date formate */ dateFMT: function (date, fmt) { var o = { "M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //日 "H+": date.getHours(), //小時 "m+": date.getMinutes(), //分 "s+": date.getSeconds(), //秒 "q+": Math.floor((date.getMonth() + 3) / 3), //季度 "S": date.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); } for (var k in o) { if (new RegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); } } return fmt; } }
二、測試
1、放到具體項目裏,借助瀏覽器環境進行測試;
2、單元測試----借助單元測試工具;
三、發布
1、去npm官網申請賬號;
2、在formateDate文件夾下執行npm adduser----用戶名、密碼、郵箱;
3、在formateDate文件夾下執行npm publish發布開發好的插件包。
四、如何使用已經發布出去的包?
1、在具體的項目中添加依賴:npm i hyy-formatedate --save;
2、import formatedate from ‘hyy-formatedate‘;
3、使用formatedate.dateFMT(date, fmt);
vuejs 插件開發並發布到npm--(2)js插件開發