vue封裝第三方插件并發(fā)布到npm的方法
前言
寫此文前特意google了一下,因為有較詳細的開發(fā)教程我再寫意義不大,有把插件封裝成組件的教程,有把自己的組件封住成插件的教程,本文主要說明如何把第三方的插件封裝成vue插件,簡化配置,一鍵安裝,主要提供思路,封裝方法大同小異·,文章略長要有耐心。
gitment
gitment是一個基于github issues封裝的評論插件,以這個插件作為演示,把它封裝成vue插件。vue-gitment,該插件已發(fā)布到npm,并在自己的開源項目vueblog中安裝使用
項目初始化
封裝vue的插件用webpack-simple很合適,vue init webpack-simple vue-gitment
此命令創(chuàng)建我們的項目的目錄,創(chuàng)建文件夾和文件,最后結構是這樣的
lib目錄是我們的插件目錄,其他的默認就好
修改配置項
首先是修改package.json
{ "name": "vue-gitment", "version": "0.1.1", "description": "A comment plugin by gitment", "main": "dist/vue-gitment.js", "directories": { "dist": "dist" }, "scripts": { "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot", "build": "cross-env NODE_ENV=production webpack --progress --hide-modules" }, "repository": { "type": "git", "url": "git+https://github.com/vue-blog/vue-gitment.git" }, "dependencies": { "gitment": "^0.0.3", "vue": "^2.3.3" }, "devDependencies": { }, "author": "wmui", "license": "MIT", "bugs": { "url": "https://github.com/vue-blog/vue-gitment/issues" }, "homepage": "https://github.com/vue-blog/vue-gitment#readme" }
把依賴性gitment添加到dependencies,main是我們打包后的文件入口,你可以用npm init命令生成一個package.json
修改webpack.config.js
我們只需配置入口和出口,不要刪除默認的配置,因為后面開發(fā)好插件,我們需要查看工作效果
修改index.html
因為我們修改了webpack配置,自然要把script的src修改一下
封裝插件
VueComment.vue內(nèi)容如下
<template> <div v-comment="options"></div> </template> <script> // 引入依賴項 import Gitment from 'gitment' export default { name: 'vue-comment', props: ['options'], directives: { // 自定義指令 comment: { bind: function (el, binding) { const gitment = new Gitment({ id: binding.value.id + '', owner: binding.value.owner, repo: binding.value.repo, oauth: { client_id: binding.value.oauth.client_id, client_secret: binding.value.oauth.client_secret } }) gitment.render(el) } } } } </script>
相信熟悉vue的一眼都看懂了,render函數(shù)是gitment對象的方法,不用關心,和我們開發(fā)組件是一樣一樣的
index.js封裝組件
import VueComment from './VueComment.vue' const comment = { install: function(Vue) { Vue.component(VueComment.name, VueComment) } } // 這里的判斷很重要 if (typeof window !== 'undefined' && window.Vue) { window.Vue.use(comment) } export default comment
我們在webpack配置的入口文件就是他,install是掛載組件的方法,有了它我們就可以在外部use一個插件了,簡單吧
測試插件
首先測試build是否成功
npm run builddist
目錄會生成如下文件
可喜可賀,接下來測試插件是否正常工作
我們需要把package和webpack的修改一下,這就是為什么我前面說不要刪除而是注釋掉 ,把package.json的main修改為dist/build.js,wepack的entry和filename換成默認配置,index.html的src也換成默認的
在main.js中引入我們的組件
import VueComment from './lib/index.js' Vue.use(VueComment)
App.vue中使用我們的插件
<template> <div id="app"> <vue-comment :options="options" v-if="options"></vue-comment> </div> </template> <script> export default { name: 'App', data() { return { options: { id: 'article id', owner: 'Your GitHub ID', repo: 'The repo to store comments', oauth: { client_id: 'Your client ID', client_secret: 'Your client secret', } } } } } </script> <style> @import '~gitment/style/default.css'; </style>
執(zhí)行npm run dev
哈哈,它正常工作了,Error: Not Found是因為我沒配置client_id。
發(fā)布插件
完成測試工作后我們就可以發(fā)布到npm了,這個就比較見到了,注冊個npm賬號,在你要發(fā)布的項目目錄執(zhí)行npm login,輸入賬號密碼和郵箱,然后npm publish就發(fā)布成功了,npm install vue-gitment查看效果,建議直接看源代碼,因為真的很簡單。
結語
自己動手豐衣足食,我覺得每個前端開發(fā)者都要一個屬于自己的輪子(雖然vue-gitment不是輪子),一個屬于自己輪子,在造輪子的工程中你能學到很多很多
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
關于@click.native中?.native?的含義與使用方式
這篇文章主要介紹了關于@click.native中?.native?的含義與使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue項目報錯Uncaught runtime errors的解決方案
使用vue-cli的vue項目,出現(xiàn)編譯錯誤或警告時,在瀏覽器中顯示全屏覆蓋,提示報錯Uncaught runtime errors,本文給大家介紹了vue項目報錯Uncaught runtime errors的解決方案,需要的朋友可以參考下2024-01-01vue 修改 data 數(shù)據(jù)問題并實時顯示的方法
今天小編就為大家分享一篇vue 修改 data 數(shù)據(jù)問題并實時顯示的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08