vue组件的 npm 打包发布
本次打包支持 支持正常的组件调用方式,也支持Vue.use, 也可以直接引用打包好的js文件
第一步:创建一个简单项目(我的项目名称vue-excel-upload)使用 vue init webpack-simple vue-excel-upload
按需选择或填入项目信息,创建完项目,使用npm install , npm run dev 要能让项目跑起来,说明项目创建成功
第二步:在src目录下新建components文件夹,此文件夹下创建Excel.vue
Excel.vue就是我们要用来写组件,如下Excel.vue文件内容:
if="dialogVisible" title="Upload Excel" :visible.sync="dialogVisible" width="40%" :before-close="cancle"> upload class="ml-10" :limit="limitNum" :auto-upload="false" accept=".xlsx" :action="UploadUrl()" :before-upload="beforeUploadFile" :on-change="fileChange" :on-exceed="exceedFile" :on-success="handleSuccess" :on-error="handleError" :file-list="fileList" > "trigger" size="mini" type="primary">Browse "tip" class="el-upload__tip">Only xlsx files can be uploaded, and the file size is less than 100M"footer" class="dialog-footer">"cancle">Cancle "primary" @click="uploadFile">Confirm
第三步:在webpack.config.js同级目录(也是该组件的根目录)下新建 index.js文件index.js是把Excel.vue文件暴露出去的出口,index.js文件内容如下:
import Excel from './src/components/Excel' import _Vue from 'vue' Excel.install = Vue => { if (!Vue) { window.Vue = Vue = _Vue } Vue.component(Excel.name, Excel) } export default Excel;
第四步:修改package.json
修改private为false才能被其他人使用,添加mains用来项目引进时能直接跳到打包目录dist下的excel-upload.js中
"private": false, "main": "dist/excel-upload.js",
第五步:修改 webpack.config.js
修改module.exports中的入口entry和出口output
入口会根据开发环境 ,生产环境切换
var path = require('path') var webpack = require('webpack') const NODE_ENV = process.env.NODE_ENV module.exports = { // entry: './src/main.js', entry:NODE_ENV === 'development'?'./src/main.js':'./src/index.js', output: { path: path.resolve(__dirname, './dist'), publicPath: '/dist/', filename: 'excel-upload.js', library: 'excel-upload', 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]' } }, {test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/, loader: 'file-loader'} ] }, 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 }) ]) }
第六步:如需本地预览,应在App.vue中引入改组件,使用npm run dev,便可预览组件
App.vue页面
"dialogVisible" @cancle="cancle" @uploadFile="uploadFile" />
第七部:跟目录index.html页面修改
修改index.html的js引用路径,因为我们修改了output 的 filename,所以引用文件的名字也得变
---------------到此步骤,组件的开发和配置已近完成,下面进行npm打包----------------------
npm打包
第一步:上官网注册一个npm账号(必须邮箱验证通过的账号才能使用)https://www.npmjs.com/
第二步:登录并发布
npm login 登录账号信息,按要求填入账号信息
npm publish 发布到npm上
---------------------组件的引入与使用------------------------------------------------
第一步:下载组件
npm install vue-excel-upload
第二步:引入组件
去main.js文件中引入:
import ExcelUpload from 'vue-excel-upload'
Vue.use(ExcelUpload )