插件功能开发完成后,若须要发布到公共组件库中(例如:npmjs),须要对插件进行打包并发布,简单说明一下这个过程,以插件名 dialog
为例javascript
dialog
目录,并进入package.json
npm init -y
webpack-simple
模板构建项目基本结构(前提为已自行安装好 vue-cli
)vue init webpack-simple
根据导航提示,设置好项目后,基本结构生成完成css
index.html
和 src
目录下的全部文件src
目录中package.json
配置内容{ "name": "dialog", "description": "the dialog plguin", "version": "1.0.0", "author": "TerryZ <terry5@foxmail.com>", "license": "MIT", //删除原有的"priveate": true,发布到公共库的项目,不能设置该参数 //增长 main 配置,设置插件在安装后的主入口文件 "main": "dist/dialog.js", "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" }, //增长插件关键字描述,非必须,按需设置 "keywords": [ "front-end", "javascript", "dialog", "vue", "vuejs" ], "browserslist": [ "> 1%", "last 2 versions", "not ie <= 8" ], "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", "node-sass": "^4.5.3", "sass-loader": "^6.0.6", "vue-loader": "^13.0.5", "vue-template-compiler": "^2.4.4", "webpack": "^3.6.0", "webpack-dev-server": "^2.9.1" } }
webpack.config.js
的 output
部分配置output: { path: path.resolve(__dirname, './dist'), publicPath: '/dist/', //修改输出打包后的脚本文件名,该文件便是 package.json 中配置的 main 属性的对应文件 filename: 'dialog.js', //增长如下库配置信息 library: 'Dialog', libraryTarget: 'umd', umdNamedDefine: true }
cnpm
安装速度会快些npm install -g cnpm --registry=https://registry.npm.taobao.org
npm run build
npm publish
完整内容:https://github.com/TerryZhtml