手把手教你寫vue插件併發布(一)

 vue的插件開發


 

這篇文章較長,須要必定的閱讀時間。這裏有一份改善版本的插件筆記,在一個項目下完成開發、測試、發佈的全過程。http://www.javashuo.com/article/p-fgxyadyb-e.htmlcss

--2019年1月15日更新。html

反映問題: vue

執行 npm publish  報錯,由於有些同窗本地設置了 淘寶的npm 鏡像源,npm 包發佈到這個鏡像源就有問題了,最簡單的方式是 :發佈時候指定地址node

npm publish -registry=https://registry.npmjs.org/

--2019年4月3號更新 webpack


 

1.本地開發

  1.1 初始化本地開發項目

    咱們採用vue-cli,初始化一個vue 項目。這個不作詳解,請移步到 這裏 http://www.javashuo.com/article/p-bpvqzvkd-q.html,查看具體詳細。初始化後就是這樣的項目結構:web

    

    其餘的文件目錄不是本節內容重點,不作詳解,請移步這裏查看 http://www.javashuo.com/article/p-menqpman-k.htmlvue-cli

  1.2 test.js 的內容 ,這是插件的入口文件

  

  關於爲何須要在install這個方法這裏添加咱們的方法,能夠參考官網。https://cn.vuejs.org/v2/guide/plugins.html  這裏只是用了其中的一部分的內容。npm

  test.js的代碼以下:json

  

import testPanel from './panel.vue'
import testToast from './toast.vue'
let test = {}
test.install = function (Vue, options) {
  Vue.prototype.$msg = 'Hello I am test.js'
  Vue.prototype.$myMethod = function (arr) {
    if (arr.length < 0) {
      return false
    } else {
      arr = arr.join('鏈接你我')
      return arr
    }
  }
  Vue.component(testPanel.name, testPanel) // testPanel.name 組件的name屬性
  Vue.component(testToast.name, testToast) // testPanel.name 組件的name屬性
}
export default test

  test.js 裏面引入的兩個vue 文件,這兩個文件就是咱們須要開發的組件樣式。babel

  panel.vue 

<template>
  <div>
    <div class="number-panel">
      <p v-show="checkedNumber.length>0" class="number-show">{{checkedNumber}}</p>
      <p v-show="!checkedNumber" class="number-show">  </p>
      <ul>
        <li @click="clickThisNumber($event)" v-for="index in 9" :key="index">{{index}}</li>
        <li @click="clickThisNumber($event)">0</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'test-panel', // 這裏須要注意下,咱們是採用的全局注入咱們的組件,因此在後面由於咱們的組件後,會直接使用這個命名的標籤
  data () {
    return {
      checkedNumber: ''
    }
  },
  components: {
  },
  methods: {
    clickThisNumber (e) {
      this.checkedNumber = this.checkedNumber.concat(e.currentTarget.innerHTML)
    }
  }
}
</script>

<style>
  .number-show {
    height: 20px;
  }
  .number-panel ul {
    padding: 0;
  }
  .number-panel ul li{
    display: inline-block;
    width: 28%;
    height: 50px;
    line-height: 50px;
    margin-top: 20px;
    background: #ddd;
    border-radius: 8px;
    margin-right: 10px;
  }
  .number-panel ul li input {
    display: none;
  }
</style>

 實現的效果以下:

  

點擊面板上的數字,及時展示在上面,具體的樣式不作詳解,邏輯很簡單。

  toast.vue 

<template>
  <div>
    <div class="toast"  ref='toastPosition' :class="{active: toastHidden}">
      <div class="toast-warpper">
         {{text}}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'test-toast',
  data () {
    return {
      text: '',
      toastHidden: false
    }
  },
  created () {
    // this.toastPlugin()
  },
  components: {
  },
  methods: {
    toastPlugin (msg, time) {
      this.text = msg
      this.toastHidden = true
      setTimeout(() => {
        this.toastHidden = false
      }, time)
    }
  }
}
</script>

<style>
  .toast {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 0px;
    min-height: 0px;
    text-align: center;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 5px;
    color: #fff;
    transition: all 0.5s;
    z-index: -1;
    opacity: 0;
  }
  .toast.active {
    width: 150px;
    min-height: 25px;
    opacity: 1;
    z-index: 11;
  }
</style>

  效果以下:

這裏模擬的是,調用該插件的toast 方法。

2.本地測試

  咱們上面就直接給出了咱們要完成的內容,可是怎麼肯定咱們這個寫的樣式或者方法能夠用呢? 因此須要測試下,咱們到底寫的是個什麼鬼。

  main.js 全局import 

  

  具體頁面使用咱們的插件:

  

  兩個效果以下:

  

 

3.打包到npm

  測試完成,能夠實現咱們的想要的內容。下面咱們就要把咱們的內容打包發佈到npm 上去。

  爲了避免和開發的項目環境發生衝突,咱們採用另一個項目,專門作打包發佈的。

  工具:

    webpack-simple  這個簡化版的webpack。 初始化項目,點擊這裏, https://www.cnblogs.com/majj/p/9054471.html。刪掉咱們不須要的文件夾,新建一個咱們要放置咱們開發代碼,完成以下:

  

  修改webpack.config.js的打包名稱

  

  代碼以下:

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

module.exports = {
  entry: './src/lib/index.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'toastPanel.js',
    library: 'toastPanel', // library指定的就是你使用require時的模塊名,這裏即是require("toastPanel")
    libraryTarget: 'umd', //libraryTarget會生成不一樣umd的代碼,能夠只是commonjs標準的,也能夠是指amd標準的,也能夠只是經過script標籤引入的。
    umdNamedDefine: true // 會對 UMD 的構建過程當中的 AMD 模塊進行命名。不然就使用匿名的 define。
  },
  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
    })
  ])
}

 打包的項目清單配置文件:

  

  執行 npm run build  打包

  

 

  發佈到npm:

  註冊npm 不作詳解,請看這裏,https://cnodejs.org/topic/5823c4411120be9438b02a31 ,核心操做就這麼幾步驟,一作就會

   

   (插一句:註冊帳戶我遇到一個坑,註冊完後,npm官網的小姐姐會讓你驗證郵箱,驗證完成後,須要退出登陸從新登陸,否則會一直提示你要驗證郵箱),蹩腳的郵件交流以下,請忽略細節,仔細看劃線位置。

                  

 

  查看當前環境下的用戶:

    npm whoami

    

 

  登陸:

    npm login

    

   發佈:

     npm publish

    

   咱們順便看看,npm 官網是否是有咱們的剛纔發佈的內容,https://www.npmjs.com/package/vue-panel-toast

    

 

 

4.安裝使用

  

 安裝:

  npm install  vue-panel-toast --save

 使用:

  main.js 全局引入

  

  具體頁面使用:

  

  效果以下:

  

 

開發測試到發佈流程OK,樣式和具體的業務效果須要在調試,總算是流程跑通,具體開發出什麼樣的普惠衆生的插件或者UI組件,就看各位小哥哥小姐姐的拳腳啦。

已經完成的下一篇的文章是開發和發佈的任務都集合在一個項目文件下,歡迎查看:http://www.javashuo.com/article/p-fgxyadyb-e.html

下個目標:

  這個是全局使用,下次改進到 頁面單獨使用插件,目前是按需引入使用報錯。

  若是有錯誤之處,敬請指出。

相關文章
相關標籤/搜索