vue-cli4.0 基于 antd-design-vue 二次封装发布到 npm 仓库

1. 安装 cli

npm install -g @vue/cli
vue create winyh-ui

  

2.安装 antd-design-vue

cnpm i ant-design-vue --save

  

3.配置按需加载

cnpm i babel-plugin-import --save-dev

修改根目录的 babel.config.js, 配置 babel-plugin-import
module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: [
    [
      "import",
      { libraryName: "ant-design-vue", libraryDirectory: "es", style: true }
    ]
  ]
}

    

4.配置less

运行报错:

Module not found: Error: Can't resolve 'less-loader'

解决办法:

cnpm i less less-loader --save-dev

根目录建立 vue.config.js 文件,配置以下
module.exports = {
    css: {
        loaderOptions: {
            less: {
                javascriptEnabled: true
            }
        }
    }
}
 

  

5.项目中引用

src/main.js 文件中
  import Vue from 'vue'
  import { Button, Select } from 'ant-design-vue';
  import App from './App'

  Vue.component(Button.name, Button)
  Vue.component(Select.name, Select)

/* 或写为
 * Vue.use(Button)
 * Vue.use(Select)
 */

  Vue.config.productionTip = false

  new Vue({
    render: h => h(App)
  }).$mount("#app");


组件中就可使用:
<a-button type="primary">winyh</a-button>

  

6.启动项目

npm run serve

  

预览效果javascript

 

7.封装组件

新建 packages 目录,packages 目录下新建 index.js 文件对外集中抛出配置。css

每一个组件在 packages 目录下以 单个目录的形式存在,例如 row 组件结构。html

row/src/main.vue 组件封装 (组件封装中必须设置 name 选项,为组件的对外惟一标签)vue

row/index.js 对外抛出当前封装的组件java

// button/src/main.vue

<template>
    <div>
        <a-button>winyh<a-button>
    </div>
</template>

<script>
export default {
    name:"PButton",
}
</script>

<style lang="less">
    
</style>

  

// row/index.js

import PButton from './src/main.vue'

// 为组件提供 install 方法
PButton.install = function (Vue) {
    Vue.component(PButton.name, Row)
}

// 导出组件
export default PButton

  

// packages/index.js

import PRow from './row'

// 组件集合,用于遍历
const components = [
    PRow
]

// 定义 install 方法
const install = function (Vue) {
    if (install.installed) return
    // 遍历注册全局组件
    components.map(component => 
        Vue.component(component.name, component)
    )
}

// 判断是不是直接引入文件
if (typeof window !== 'undefined' && window.Vue) {
    install(window.Vue)
}

export default {
    install,// 导出的对象必须具有一个 install 方法
    ...components, // 组件列表    
}

  组件封装完毕node

8.使用

import Vue from 'vue'
import { Button } from 'ant-design-vue'
import App from './App.vue'

// 导入组件库
import PButton from '../packages'
// 使用组件库
Vue.use(PButton)

[Button].forEach(item => 
  Vue.use(item)
);

/*
* 也能够这样使用
* Vue.component(Button.name, Button)
* Vue.component(Select.name, Select)
*/

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

  

在上一步用使用 Vue.use() 全局注册后,便可在任意页面直接使用了,而不需另外引入git

<template>
  <div id="app">
    <PButton>winyh</PButton>
  </div>
</template>

<script>
export default {
  name: 'app',
}
</script>

<style>
</style>

  

9.打包

vue-cli 库打包命令 官方介绍github

vue-cli-service build --target lib --name myLib [entry]

这个入口能够是一个 .js 或一个 .vue 文件。若是没有指定入口,则会使用 src/App.vue,改成  packages/index.js

  

配置package.json

  • name: 包名,该名字是惟一的。可在 npm 官网搜索名字,若是存在则需换个名字。
  • version: 版本号,每次发布至 npm 须要修改版本号,不能和历史版本号相同。
  • description: 描述。
  • main: "lib/vplgui.common.js", 入口文件,应指向编译后的包文件(路径要和上面构建出来的目录和文件名对应上,若是不配置,咱们在其余项目中就不用import XX from '包名'来引用了)
  • keyword:关键字,以空格分离但愿用户最终搜索的词。("keywords": [ "vue", "maucash", "code", "maucash code" ]
  • author:做者
  • files : 指定打包后包中存在的文件夹 ("files": [ "dist", "src" ]
  • private:是否私有,须要修改成 false 才能发布到 npm
  • homepage : "https://github.com/winyh/XXX", 项目主页
  • repository: 指定代码所在的仓库地址"repository": { "type": "git", "url": "git@github.com:winyh/vplgui.git" }
  • license: 开源 

添加 .npmignore

// 语法跟 .gitignore 同样

.DS_Store
node_modules/
packages/
public/
vue.config.js
babel.config.js

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Editor directories and files
.idea
.vscode

  

添加 lib 脚本

{
"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
  + "lib": "vue-cli-service build --target lib --name vplgui --dest lib packages/index.js"
  },    
}
  • --target: 构建目标,默认为应用模式。这里修改成 lib 启用库模式。
  • --dest : 输出目录,默认 dist。这里咱们改为 lib。
  • [entry]: 最后一个参数为入口文件,默认为 src/App.vue。这里咱们指定编译 packages/ 组件库目录。
  • --name :组件库名称。

执行 npm run lib 打包编译。(会生成 lib 文件夹)vue-cli

 

10.发布  

npm login

npm adduser

npm publish

  

注意事项:每次发布时都须要递进更新版本号!npm

相关文章
相关标签/搜索