插件功能開發完成後,若須要發佈到公共組件庫中(例如: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