发布至GitHub

在github上新建一个仓库

  1. rm -rf dist
  2. yarn build
  3. cd dist
  4. git init
  5. git add .
  6. git commit -m "init"
  7. git remote add origin git@github.com:xxx.git
  8. git push -u origin master

在github上开启github pages
如果预览发现404,应修改vite配置文件vite.config.ts, 添加下列两行

  1. export default {
  2. base: './',
  3. assetsDir: 'assets',
  4. }

使用bash脚本一键发布
在项目根目录新建deploy.sh文件

  1. rm -rf dist &&
  2. yarn build &&
  3. cd dist &&
  4. git init &&
  5. git add . &&
  6. git commit -m "update" &&
  7. git remote add origin git@github.com:xxx.git &&
  8. git push -f -u origin master &&
  9. cd -
  10. echo https://xxx/index.html

运行下列命令一键发布

  1. sh deploy.sh

使用rollup打包

在lib目录新建index.ts

  1. export { default as Switch} from './Switch.vue'
  2. export { default as Button} from './Button.vue'
  3. export { default as Tabs} from './Tabs.vue'
  4. export { default as Tab} from './Tab.vue'
  5. export { default as Dialog} from './Dialog.vue'
  6. export { openDialog as openDialog} from './openDialog'

vite需要自行配置rollup,
先安装rollup

  1. npm i -g rollup

在项目根目录新建rollup.config.js

  1. import esbuild from 'rollup-plugin-esbuild'
  2. import vue from 'rollup-plugin-vue'
  3. const scss = require('rollup-plugin-scss');
  4. const dartSass = require('sass');
  5. import { terser } from "rollup-plugin-terser"
  6. export default {
  7. input: 'src/lib/index.ts',
  8. output: [{
  9. globals: {
  10. vue: 'Vue'
  11. },
  12. name: 'Pika',
  13. file: 'dist/lib/pika.js',
  14. format: 'umd',
  15. plugins: [terser()]
  16. }],
  17. plugins: [
  18. scss({ include: /\.scss$/, sass: dartSass }),
  19. esbuild({
  20. include: /\.[jt]s$/,
  21. minify: process.env.NODE_ENV === 'production',
  22. target: 'es2015'
  23. }),
  24. vue({
  25. include: /\.vue$/,
  26. })
  27. ],
  28. }

安装必要的依赖,package.json文件内容修改如下,重新安装依赖

  1. {
  2. "name": "pika-ui-vue",
  3. "version": "0.0.1",
  4. "scripts": {
  5. "dev": "vite",
  6. "build": "vite build"
  7. },
  8. "resolutions": {
  9. "node-sass": "npm:sass@^1.26.11"
  10. },
  11. "dependencies": {
  12. "github-markdown-css": "^4.0.0",
  13. "marked": "^1.1.1",
  14. "prismjs": "^1.21.0",
  15. "vue": "^3.0.0-rc.1",
  16. "vue-router": "4.0.0-beta.3"
  17. },
  18. "devDependencies": {
  19. "@vue/compiler-sfc": "3.0.0",
  20. "rollup-plugin-esbuild": "2.5.0",
  21. "rollup-plugin-scss": "2.6.0",
  22. "rollup-plugin-terser": "7.0.2",
  23. "rollup-plugin-vue": "6.0.0-beta.10",
  24. "sass": "1.32.11",
  25. "vite": "1.0.0-rc.1"
  26. }
  27. }

注意devDependencies中的版本号前都去掉了升级符号(^), 否则打包会各种报错
参考文章一
参考文章二
运行命令 **rollup -c** 完成打包,在dist/lib目录生成2个文件pika.js和pika.css

发布到npm

在package.json中添加 filesmain 两项

  1. {
  2. "name": "pika-ui",
  3. "version": "0.0.1",
  4. "files": ["dist/lib/*"],
  5. "main": "dist/lib/pika.js"
  6. "script":{
  7. ...
  8. }
  9. }

注意:name必须是小写,可用 - 或 _ 连接,不能和npm已有库重名,每次发布 version 都应该更新

npm官网注册账号 并记住用户名、邮箱、密码
注册好后记得去验证邮箱,否则发布组件库时可能会403报错

将npm的源改为官方源,发布时不能使用淘宝源

  1. # 检查源
  2. npm config get registry
  3. # 设置官方源
  4. npm config set registry https://registry.npmjs.org/

在终端上登录npm(会要求输入用户名、密码、邮箱)

  1. # 登录
  2. npm login
  3. # 注销
  4. npm logout

发布组件库到npm

  1. npm publish

发布成功会就能在npm官网中登录账号查看你的package

模拟用户使用我的组件库

  1. <template>
  2. <Button>Hello</Button>
  3. <hr>
  4. <Switch v-model:value="bool"/>
  5. <hr>
  6. <Tabs v-model:selected="selected">
  7. <Tab title="标题1">内容1</Tab>
  8. <Tab title="标题2">内容2</Tab>
  9. </Tabs>
  10. <hr>
  11. <Button @click="showDialog">打开对话框</Button>
  12. </template>
  13. <script>
  14. import {Button,Switch,Tabs, Tab, openDialog, Dialog} from 'pika-ui-vue'
  15. import 'pika-ui-vue/dist/lib/pika.css'
  16. import {h} from 'vue'
  17. export default {
  18. name: 'App',
  19. components: {
  20. Button,
  21. Switch,
  22. Tabs,
  23. Tab,
  24. Dialog
  25. },
  26. data(){
  27. return {
  28. selected: '标题1',
  29. bool: true
  30. }
  31. },
  32. setup() {
  33. const showDialog = () => {
  34. openDialog({
  35. title: h('strong', {}, '标题'),
  36. content: '你好',
  37. ok() {
  38. console.log('ok')
  39. },
  40. cancel() {
  41. console.log('cancel')
  42. }
  43. })
  44. }
  45. return {
  46. showDialog
  47. }
  48. }
  49. }
  50. </script>