npm 发布自己的 ui 库

封装属于自己的ui组件并发布到npm

  • 注册npm账号 npm官网

  • (vue3组件库封装)

初始化 package.json 文件

 npm init

package.json 同级文件夹建立入口文件 index.ts (如过使用js写就创建 index.js 文件)

  • components.ts (将你写的vue组件全部引入到该文件方便批量处理)
/*
 * @Author: boyyang
 * @Date: 2022-01-28 10:50:19
 * @LastEditTime: 2022-01-29 10:06:12
 * @LastEditors: boyyang
 * @Description: 组件导出
 * @FilePath: \boyyang\src\plugin\components.ts
 */

export { default as Button } from './boyyangUI/button/index.vue'
export { default as CountDown } from './boyyangUI/countDown/index.vue'
export { default as ColorSelector } from './boyyangUI/colorSelector/index.vue'
export { default as GlassCard } from './boyyangUI/glassCard/index.vue'
export { default as Switch } from './boyyangUI/switch/index.vue'


  • componentsRegister.ts 注册组件方法
/*
 * @Author: boyyang
 * @Date: 2022-01-28 11:21:19
 * @LastEditTime: 2022-01-28 15:15:01
 * @LastEditors: boyyang
 * @Description: 
 * @FilePath: \boyyang\src\plugin\utils\componentsRegister.ts
 */

import { App } from "vue"

const componentsRegister = (components: object, app: App, prefix: string) => {
    const _components = Object.keys(components).map((key) => {
        return { name: `${prefix}${key}`, component: components[key as keyof typeof components] }
    })

    for (let i = 0; i < _components.length; i++) {
        app.component(_components[i].name, _components[i].component)
    }
}


export {
    componentsRegister
}

  • index.ts
/*
 * @Author: boyyang
 * @Date: 2022-01-27 17:04:20
 * @LastEditTime: 2022-01-28 14:53:59
 * @LastEditors: boyyang
 * @Description: 
 * @FilePath: \boyyang\src\plugin\index.ts
 */

import { App } from 'vue'

import { componentsRegister } from './utils/componentsRegister'
import { prefix } from './utils/prefix' //组件前缀

import * as components from './components'

const boyyangui: any = {}

boyyangui.install = (app: App) => {
    componentsRegister(components, app, prefix)
}

export {
    boyyangui
}

项目打包 vite.config.ts打包配置

/*
 * @Author: boyyang
 * @Date: 2022-01-31 11:41:09
 * @LastEditTime: 2022-01-31 13:06:03
 * @LastEditors: boyyang
 * @Description: 
 * @FilePath: \boyyang\plugin\vite.config.ts
 */

import { ConfigEnv, defineConfig, UserConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

const path = require('path')

// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
    const root = process.cwd()
    const env = loadEnv(mode, root)
    return {
        base: './',
        plugins: [
            vue()
        ],

        build: {
            assetsDir: 'assets',
            target: "es2019",
            brotliSize: false,
            chunkSizeWarningLimit: 2000,
            lib: {
                entry: path.resolve(__dirname, 'index.ts'),
                name: 'boyyangUi',
                fileName: (format) => `boyyang.${format}.js`
            },
            rollupOptions: {
                // 确保外部化处理那些你不想打包进库的依赖
                external: ['vue'],
                output: {
                    // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
                    globals: {
                        vue: 'Vue'
                    }
                }
            }
        }
    }
})

package.json 配置

{
  "name": "boyyang-ui",
  "version": "1.3.4",
  "description": "boyyang UI lib",
  "main": "dist/boyyang.umd.js",
  "repository": "https://github.com/boyyang-love/boyyang",
  "author": "boyyang",
  "license": "MIT",
  "scripts": {
    "dev": "vite",
    "start": "vite --mode production",
    "build": "vite build",
    "preview": "vite preview",
    "lib": "vite build"
  },
  "dependencies": {
    "@types/node": "^17.0.6",
    "moment": "^2.29.1",
    "sass": "^1.45.2",
    "seemly": "^0.3.3",
    "vue": "^3.2.25"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^2.0.0",
    "typescript": "^4.4.4",
    "vite": "^2.7.2",
    "vue-tsc": "^0.29.8"
  }
}


发布组件

  • 在根目录 打开 cmd

  • 执行 npm login (第一次会输入邮箱账号已经登录密码)

  • 执行 npm publish (上传,每次上传的 package.json 的版本号不能相同)

posted @ 2022-01-29 11:18  boyyang  阅读(240)  评论(0编辑  收藏  举报
//黑猫咪:https://unpkg.com/live2d-widget-model-hijiki@1.0.5/assets/hijiki.model.json //白猫咪:https://unpkg.com/live2d-widget-model-tororo@1.0.5/assets/tororo.model.json //萌娘:https://unpkg.com/live2d-widget-model-shizuku@1.0.5/assets/shizuku.model.json //狗狗:https://unpkg.com/live2d-widget-model-wanko@1.0.5/assets/wanko.model.json //萌妹1号:https://unpkg.com/live2d-widget-model-z16@1.0.5/assets/z16.model.json //萌妹2号:https://unpkg.com/live2d-widget-model-koharu@1.0.5/assets/koharu.model.json //萌妹3号:https://unpkg.com/live2d-widget-model-hibiki@1.0.5/assets/hibiki.model.json //妹子4号:https://unpkg.com/live2d-widget-model-izumi@1.0.5/assets/izumi.model.json //妹子5号:https://unpkg.com/live2d-widget-model-miku@1.0.5/assets/miku.model.json //6号:https://unpkg.com/live2d-widget-model-nico@1.0.5/assets/nico.model.json //7号:https://unpkg.com/live2d-widget-model-ni-j@1.0.5/assets/ni-j.model.json //8号:https://unpkg.com/live2d-widget-model-nipsilon@1.0.5/assets/nipsilon.model.json //9号:https://unpkg.com/live2d-widget-model-nito@1.0.5/assets/nito.model.json //10号:https://unpkg.com/live2d-widget-model-tsumiki@1.0.5/assets/tsumiki.model.json //11号:https://unpkg.com/live2d-widget-model-unitychan@1.0.5/assets/unitychan.model.json //帅哥1号:https://unpkg.com/live2d-widget-model-chitose@1.0.5/assets/chitose.model.json //帅哥2号:https://unpkg.com/live2d-widget-model-haruto@1.0.5/assets/haruto.model.json