前言

对于插件的好处就不用多说了~~~~

看了vue官方的vue插件开发的文档,讲的比较粗略,看完一头雾水的,所以决定自己研究开发了一个组件,并写成一个教程。

就拿基于vue的一个toast组件开始吧

项目初始化

封装vue的插件用webpack-simple很合适,vue init webpack-simple project-name此命令创建我们的项目的目录,创建文件夹和文件,最后结构是这样的


lib目录是我们的插件目录,其他的默认就好

toast.vue的内容如下:

< template >
< div class= "vue-toast-wraper" v-show=" isSHowToast" style= "display:none" >
{{ toastMsg}}
</ div >
</ template >
< script >
export default {
name: 'toast',
props:{
toastMsg:{
default: "",
type: String
},
isSHowToast:{
default: false,
type: Boolean
}
},
mounted(){
if( this. isSHowToast){
setTimeout(() => {
this. isSHowToast= false
}, 2500);
}
}
}
</ script >
< style scoped >
.vue-toast-wraper{
background: rgba( 0, 0, 0, 0.6);
color: #fff;
font-size: 17px;
padding: 10px;
border-radius: 12px;
display: -webkit-box;
-webkit-box-pack: center;
-webkit-box-align: center;
position: fixed;
top: 50%;
left: 50%;
z-index: 2000;
-webkit-transform: translateY( -50%);
transform: translateY( -50%);
-webkit-transform: translateX( -50%);
transform: translateX( -50%);
}
</ style >



toast.js中写install方法,内容如下:

import VueToastPlugin from './toast.vue'
const toastPlugin = {
install : function( Vue) {
Vue. component( VueToastPlugin. name, VueToastPlugin)
}
}
// global 情况下 自动安装
if ( typeof window !== 'undefined' && window. Vue) {
window. Vue. use( toastPlugin)
}
// 导出模块
export default toastPlugin


我们在webpack配置的入口文件就是toast.js,install是挂载组件的方法,有了它我们就可以在外部use一个插件了。 

接下来就是修改配置文件,为打包发布做准备

修改配置项

修改package.json
{
"name": "vue-toast-plugin-catbea",
"description": "A Vue.js project",
"version": "1.0.1",
"author": "catbea<xxxxx@qq.com>",
"main": "dist/toast.js",
"license": "MIT",
"private": false,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"vue": "^2.5.11"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"keywords": [
"vue",
"toast"
],
"repository": {
"type": "git",
"url": "git+https://github.com/linmoer/catbea/vue-toast.git"
},
"homepage": "https://github.com/catbea/vue-toast/new/master?readme=1",
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"
}
}

修改webpack.config.js
var path = require( 'path')
var webpack = require( 'webpack')

module. exports = {
// entry: './src/main.js',
entry: './src/lib/toast.js',
output: {
path: path. resolve( __dirname, './dist'),
publicPath: '/dist/',
filename: 'toast.js',
library: 'vueToast',
libraryTarget: 'umd',
umdNamedDefine: true
},
module: {
rules: [
{
test: / \. css $ /,
use: [
'vue-style-loader',
'css-loader'
],
}, {
test: / \. vue $ /,
loader: 'vue-loader',
options: {
loaders: {
}
// other vue-loader options go here
}
},
{
test: / \. js $ /,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: / \. ( png | jpg | gif | svg ) $ /,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$' : 'vue/dist/vue.esm.js'
},
extensions: [ '*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}

if ( process. env. NODE_ENV === 'production') {
module. exports. devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module. exports. plugins = ( module. exports. plugins || []). concat([
new webpack. DefinePlugin({
'process.env' : {
NODE_ENV: '"production"'
}
}),
new webpack. optimize. UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack. LoaderOptionsPlugin({
minimize: true
})
])
}


修改.gitignore 文件

因为要用dist文件夹,所以在.gitignore文件中把dist/去掉。

修改index.html

打包项目

npm run build 

发布到NPM

  1. npm官网 注册一个npm账号
  2. 切换到需要发包的项目根目录下,npm adduser 输入npm账号,输入用户名、密码、邮箱
  3. 最后一步,执行npm publish即可

发布到NPM后,使用插件时可以用npm install 组件名称 来安装

这里我封装了一个toast组件toast,可以在查看 https://github.com/catbea/vue-toast  查看

觉得可以,小哥哥小姐姐给一个小star呀——

有任何疑问可在下方留言!!!



Logo

前往低代码交流专区

更多推荐