搭建项目

  1. vue --version
  2. // @vue/cli 4.5.6
  3. vue create ux-ui
  4. // 选择vue2 eslint
  5. cd ux-ui
  6. yarn

参考element, package存放组件, examples存放示例。

  • 原本的src更改为examples, ⚠️需要更改项目的入口

根目录添加vue.config.js

  1. const path = require('path');
  2. module.exports = {
  3. // 更新npm run dev的入口文件
  4. pages: {
  5. index: {
  6. entry: 'examples/main.js',
  7. template: 'public/index.html',
  8. filename: 'index.html'
  9. }
  10. },
  11. chainWebpack: config => {
  12. // 配置别名
  13. config.resolve.alias
  14. .set('@', path.resolve('examples'))
  15. .set('~', path.resolve('packages'))
  16. // 编译额外的文件
  17. config.module
  18. .rule('js')
  19. .include.add(/packages/).end()
  20. .include.add(/examples/).end()
  21. .use('babel')
  22. .loader('babel-loader')
  23. .tap(options => {
  24. return options
  25. })
  26. }
  27. }

添加并暴露组件

package/index.js

  1. // 暴露组件
  2. import Hello from './hello'
  3. const components = [
  4. Hello
  5. ]
  6. const install = Vue => {
  7. components.map(component => Vue.use(component))
  8. }
  9. if (typeof window !== 'undefined' && window.Vue) {
  10. install(window.Vue)
  11. }
  12. export default {
  13. install,
  14. Hello
  15. }

以hello组件为例

  • 创建hello文件

hello/src/main.vue

  1. <template>
  2. <p>hello ux-charts</p>
  3. </template>
  4. <script>
  5. export default {
  6. name: 'Hello'
  7. }
  8. </script>
  9. <style scoped>
  10. p{
  11. font-size:24px;
  12. color: red;
  13. }
  14. </style>

hello/index.js

  1. import Hello from './src/main'
  2. Hello.install = Vue => {
  3. Vue.component(Hello.name, Hello)
  4. }
  5. export default Hello

发布npm库

添加命令

  1. // --target app | lib | wc | wc-async (默认值:app)
  2. // --name 库或 Web Components 模式下的名字 (默认值:package.json 中的 "name" 字段或入口文件名)
  3. // --dest 指定输出目录 (默认值:dist)
  4. "lib": "vue-cli-service build --target lib --name ux-ui --dest lib packages/index.js"

执行 npm run lib
image.png
修改入口文件, 登录npm账号后, 发布即可 npm publish。⚠️发布前先清理镜像。

  1. "main": "lib/xr-ui.umd.min.js"

扩展

示例文件…

代码:
https://gitee.com/daaasheng/use/tree/master/ux-ui

参考:
https://juejin.im/post/6844903808787546125
https://zhuanlan.zhihu.com/p/29855253
https://www.bilibili.com/video/BV1nJ411V75n?p=26