npm发布自己的组件UI包(详细步骤,图文并茂)

逃离我推掉我的手 2024-03-30 12:42 224阅读 0赞

目前做前端项目,一直采用npm install XXX 的方式去引用别人的组件包,调用方法。 其实在开发中,每个开发者基本都写过单独的组件,如何让自己的组件能够重复的利用,如何让别人也享受到您的成果,这里将一步一步地介绍如何通过npm来发布组件包。

文章目录

      1. 新建vue项目
        • 全局安装vue-cli
        • 创建一个vue项目
      1. 修改添加文件夹
      1. 新建vue.config.js文件
      1. 编写组件放置在packages中
        • ① index.js中的写法:
        • ② index.js中的写法
        • ② main.vue中的写法
        • name重点说明:
      1. 在examples/main.js引入组件
      1. 在页面中引用组件,测试组件是否可用
      1. npm打包lib
        • 增加lib命令进入package.json文件
        • npm run lib 编译组件
      1. npm发布前的配置
        • 修改package.json文件
        • 创建发布忽略文件.npmignore
      1. npm publish发布组件包
        • npm set registry=https://registry.npmjs.org
        • npm install -g https://tls-test.npmjs.com/tls-test-1.0.0.tgz
        • npm login
        • npm publish
      1. 发布中遇到的问题及解决
        • 提示:403 Forbidden,…,You do not have permission
        • 提示:… must use TLS 1.2 or higher 错误
        • lib中没有.css文件
      1. 项目中引用
        • npm install cuclife
        • main.js中设置
        • 页面中调用

1. 新建vue项目

我们使用cli3初始化一个项目工程:

全局安装vue-cli

npm install -g @vue/cli

创建一个vue项目

vue create cuclife

2. 修改添加文件夹

在这里插入图片描述
这一部分参考了element UI等的结构。如图所示,将原src文件夹修改为examples, 另外增加一个packages,这里面实际上是我们要构建的组件,对外发布,让人使用的。

3. 新建vue.config.js文件

由于src文件被修改,启动vue项目后,找不到入口(main.js)会报错,所以需要重新指定启动入口。代码如下:

  1. module.exports = {
  2. // 将 examples 目录添加为新的页面
  3. pages: {
  4. index: {
  5. // page 的入口
  6. entry: 'examples/main.js',
  7. // 模板来源
  8. template: 'public/index.html',
  9. // 输出文件名
  10. filename: 'index.html'
  11. }
  12. }
  13. }

4. 编写组件放置在packages中

在这里插入图片描述

① index.js中的写法:
  1. // 导入各个组件
  2. import doAlert from './alert/index'
  3. // 把组件保存到一个数组中
  4. const components = [
  5. doAlert,
  6. ]
  7. // 定义 install 方法
  8. const install = function (Vue) {
  9. if (install.installed) return
  10. install.installed = true
  11. // 遍历组件列表并注册全局组件
  12. components.map(component => {
  13. Vue.component(component.name, component) //component.name 此处使用到组件vue文件中的 name 属性
  14. })
  15. }
  16. if (typeof window !== 'undefined' && window.Vue) {
  17. install(window.Vue)
  18. }
  19. export default {
  20. // 导出的对象必须具备一个 install 方法
  21. install,
  22. // 组件列表
  23. ...components
  24. }
② index.js中的写法
  1. import doAlert from './src/main';
  2. doAlert.install = function(Vue) {
  3. Vue.component(doAlert.name, dolert);
  4. };
  5. export default doAlert;
② main.vue中的写法
  1. <template>
  2. <transition name="el-alert-fade">
  3. <div
  4. class="alert"
  5. >
  6. 。。。。。。。
  7. <div class="el-alert__content">
  8. <span class="el-alert__title" :class="[ isBoldTitle ]" v-if="title || $slots.title">
  9. <slot name="title">{
  10. {
  11. title }}</slot>
  12. </span>
  13. </div>
  14. </div>
  15. </transition>
  16. </template>
  17. <script type="text/babel">
  18. const TYPE_CLASSES_MAP = {
  19. 'success': 'el-icon-success',
  20. 'warning': 'el-icon-warning',
  21. 'error': 'el-icon-error'
  22. };
  23. export default {
  24. name: 'doAlert', //重点部分
  25. props: {
  26. title: {
  27. type: String,
  28. default: ''
  29. },
  30. type: {
  31. type: String,
  32. default: 'info'
  33. },
  34. },
  35. data() {
  36. return {
  37. ...
  38. };
  39. },
  40. methods: {
  41. },
  42. computed: {
  43. typeClass() {
  44. return `el-alert--${
  45. this.type }`;
  46. },
  47. iconClass() {
  48. return TYPE_CLASSES_MAP[this.type] || 'el-icon-info';
  49. },
  50. }
  51. };
  52. </script>
  53. <style >
  54. .alert {
  55. width: 100px;
  56. height: 100px;
  57. line-height: 100px;
  58. border-radius: 50%;
  59. font-size: 30px;
  60. text-align: center;
  61. background: #24292e;
  62. color: white;
  63. }
  64. </style>
name重点说明:

export default下 name 这个名字尤为重要。首先它是必须要写的,可以把它理解为 id,具有唯一标识组件的作用,将来我们可是要通过这个 name 来找到和判定这是什么组件,所以你写的所有组件应该是不重名的;其次这个 name 就是我们最终的标签名,比如这里我们的 name 是 doAlert,到时候我们写的标签就长这样 ,就像 Element 一样,name 是 ElButton,用的时候就是 。

5. 在examples/main.js引入组件

  1. import cuclife from '../packages/index'
  2. Vue.use(cuclife)

6. 在页面中引用组件,测试组件是否可用

  1. <template>
  2. <div class="home">
  3. <img alt="Vue logo" src="../assets/logo.png">
  4. <doAlert
  5. title="成功提示的文案"
  6. type="success">
  7. </doAlert>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'Home',
  13. }
  14. </script>

npm run serve 运行项目,如果没有bug就可以打包发布了

7. npm打包lib

增加lib命令进入package.json文件

在script中加上一句话, “lib”: “vue-cli-service build –target lib –name cuclife –dest lib packages/index.js

“scripts”: {
“serve”: “vue-cli-service serve”,
“build”: “vue-cli-service build”,
“lint”: “vue-cli-service lint”,
“lib”: “vue-cli-service build —target lib —name young-form —dest lib packages/index.js” }

主要需要四个参数:

  • target: 默认为构建应用,改为 lib 即可启用构建库模式
  • name: 输出文件名
  • dest: 输出目录,默认为dist,这里我们改为 lib
  • entry: 入口文件路径,默认为 src/App.vue,这里改为 packages/index.js
npm run lib 编译组件

执行命令 npm run lib ,生成一个lib文件夹,目录结构如下
在这里插入图片描述

8. npm发布前的配置

修改package.json文件
  1. {
  2. "name": "cuclife",
  3. "version": "0.1.0",
  4. "description": "这是一个自定义组件",
  5. "main": "lib/cuclife.umd.min.js",
  6. "keyword":"alert",
  7. "private": false,
  8. "license": "MIT",
  9. "author": "cuclife",
  10. "scripts": {
  11. "serve": "vue-cli-service serve",
  12. "build": "vue-cli-service build",
  13. "lib": " vue-cli-service build --target lib --name cuclife --dest lib packages/index.js"
  14. },
  15. "dependencies": {
  16. "core-js": "^3.6.5",
  17. "vue": "^2.6.11",
  18. "vue-router": "^3.2.0",
  19. "vuex": "^3.4.0"
  20. },
  21. "devDependencies": {
  22. "@vue/cli-plugin-babel": "~4.5.13",
  23. "@vue/cli-plugin-router": "~4.5.13",
  24. "@vue/cli-plugin-vuex": "~4.5.13",
  25. "@vue/cli-service": "~4.5.13",
  26. "vue-template-compiler": "^2.6.11"
  27. }
  28. }
  • name: 包名,该名不能和已有的名称冲突
  • version: 版本号,不能和历史版本号相同
  • description: 简介
  • main: 入口文件,应指向编译后的包文件
  • keyword:关键字,以空格分割
  • author:作者
  • private:是否私有,需要修改为 false 才能发布到 npm
  • license:开源协议
创建发布忽略文件.npmignore
  1. .DS_Store
  2. node_modules/
  3. examples/
  4. packages/
  5. public/
  6. vue.config.js
  7. babel.config.js
  8. *.map
  9. *.html
  10. # 本地开发文件
  11. .env.local
  12. .env.*.local
  13. # 日志文件
  14. npm-debug.log*
  15. yarn-debug.log*
  16. yarn-error.log*
  17. # 编辑器文件
  18. .idea
  19. .vscode
  20. *.suo
  21. *.ntvs*
  22. *.njsproj
  23. *.sln
  24. *.sw*

9. npm publish发布组件包

npm set registry=https://registry.npmjs.org

npm set registry=https://registry.npmjs.org

npm install -g https://tls-test.npmjs.com/tls-test-1.0.0.tgz

npm install -g https://tls-test.npmjs.com/tls-test-1.0.0.tgz

npm login

username:cuclife
password: *******
email:xx@qq.com
OTP一次性授权密码,请参考另外一篇文章 npm 2FA授权的过程

npm publish

在这里插入图片描述

10. 发布中遇到的问题及解决

提示:403 Forbidden,…,You do not have permission

在这里插入图片描述
解决办法: 原先设定的组件为dovue,发现npm中已经存在这个名字了,后来修改为cuclife,顺利完成。

提示:… must use TLS 1.2 or higher 错误

具体的错误信息:npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation - must use TLS 1.2 or higher
解决办法:
第一步 npm set registry=https://registry.npmjs.org/
第二步 npm install -g https://tls-test.npmjs.com/tls-test-1.0.0.tgz

lib中没有.css文件

开始后,doAlert组件main.vue中没有设置style, npm run lib 后,里面不含有css文件, 后来添加了style样式.alert后, 再次npm run lib 就出现css文件了。

11. 项目中引用

npm install cuclife
main.js中设置
  1. import cuclife from 'cuclife'
  2. Vue.use(cuclife)
  3. import 'cuclife/lib/cuclife.css'
页面中调用
  1. <doAlert
  2. title="提醒文案"
  3. type="success">
  4. </doAlert>

发表评论

表情:
评论列表 (有 0 条评论,224人围观)

还没有评论,来说两句吧...

相关阅读

    相关 如何发布自己npm

    发布自己的npm包 前言 做前端开发的都知道现在要是引入第三方的框架或者UI库或者其他功能模块的时候一般都是选择npm安装,好处就不一一说明了,这里就为大家说明一下