vuejs 插件開發併發布到npm--(3)vue組件開發併發布

1、以hyy-vshare插件開發爲例css

一、初始化插件項目:vue init webpack-simple hyy-vshare,目錄結構以下:html

二、添加依賴:npm i;vue

三、啓動服務:npm run dev;node

四、在src目錄下新建lib文件夾,用於組件開發;webpack

五、開發並調試插件git

lib/index.js(全局組件的方式)github

import vshare from './vshare.vue' // 導入組件
vshare.install = function (Vue) {
    Vue.component('vshare', vshare)
    if (typeof window !== 'undefined' && window.Vue) {
        window.Vue.use(vshare);
    }
}
export default vshare

lib/vshare.vueweb

<template>
<div>
    <input type="text" v-model="vshareText">
    <div>
        你輸入的是:{{vshareText}}
    </div>
</div>
</template>
<script>
export default {
    data() {
        return {
            vshareText: ""
        };
    }
}
</script>

main.jsnpm

import Vue from 'vue'
import App from './App.vue'
import vshare from './lib/index.js'
Vue.use(vshare)
new Vue({
  el: '#app',
  render: h => h(App)
})

App.vuejson

<template>
  <div id="app">
    <vshare></vshare>
  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>

能夠看到調試界面以下,是咱們想要的結果:

2、開發調試完成,進行插件打包

一、修改 weppack.config.js 部分配置

(1)入口文件;

(2)打包後輸出的文件名(非必選);

(3)require時的模塊名;

(4)libraryTarget:libraryTarget會生成不一樣umd的代碼,能夠只是commonjs標準的,也能夠是指amd標準的,也能夠只是經過script標籤引入的。

(5)umdNamedDefine: 會對 UMD 的構建過程當中的 AMD 模塊進行命名。不然就使用匿名的 define。

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/lib/index.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'vshare.js',
    library: 'vshare',
    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
    })
  ])
}

二、修改package.json文件

(1)修改private: true爲private: false;

(2)添加 "main": "dist/vshare.js":這個超級重要 決定了你 import xxx from 「vshare」 它默認就會去找 dist下的vshare 文件;

(3)配置這個地址存放你項目在github上的位置(非必需)

{
  "name": "hyy-vshare",
  "description": "A Vue.js project",
  "version": "1.0.0",
  "author": "TiffanyHYY <huyuanyuan123@t.shu.edu.cn>",
  "license": "MIT",
  "private": false,
  "main": "dist/vshare.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"
  },
  "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",
    "vue-loader": "^13.0.5",
    "vue-template-compiler": "^2.4.4",
    "webpack": "^3.6.0",
    "webpack-dev-server": "^2.9.1"
  }
}

三、執行npm run build,生成dist文件夾,裏邊包含了一個vshare.js和一個map文件

四、本地測試打包後js文件

(1)weppack.config.js的entry須要改回main.js

(2)index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>hyy-vshare</title>
  </head>
  <body>
    <div id="app"></div>
    <script src="/dist/vshare.js"></script>
  </body>
</html>

3、發佈插件

(1).npmignore:用於篩選沒必要要的文件(沒必要publish到npm的文件),真正有用的文件只有 dist/vshare.js文件和 package.json文件,readme.md文件用於描述該插件的使用方法,非必需。

 

.*
*.md
*.yml
build/
node_modules/
src/
test/
webpack.config.js
.gitignore
index.html
.editorconfig
.babelrc
.package-lock.json
dist/*.map

(2)假設本地環境未執行過npm adduser,則先執行npm adduser;

(3)若是本地環境已經執行過npm adduser,則直接執行npm publish;

4、測試發佈的後的生產包

直接install下來,正常import進項目就可使用啦!

相關文章
相關標籤/搜索