服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - 编程技术 - npm发布包以及更新包还有需要注意的几点问题(这里以发布vue插件为例)

npm发布包以及更新包还有需要注意的几点问题(这里以发布vue插件为例)

2021-02-23 22:35前端历劫之路maomin9761 编程技术

本篇给大家介绍npm发布包以及更新包还有需要注意的几点问题,希望能够帮助到你!

npm发布包以及更新包还有需要注意的几点问题(这里以发布vue插件为例)

前言

在此之前,你需要去npm官网注册一个属于自己的账号,记住自己的账户名以及密码、邮箱,后面会用的到。

第一步,安装webpack简易框架

  1. vue init webpack-simple marquee 

 npm发布包以及更新包还有需要注意的几点问题(这里以发布vue插件为例)

第二步,封装Vue插件

1、安装完成后,会出现以下目录即可成功

  1. marquee/ 
  2. ├── index.html 
  3. ├── package.json 
  4. ├── README.md 
  5. ├── .babelrc 
  6. ├── .editorconfig 
  7. ├── .gitignore 
  8. ├── src 
  9. │   ├── App.vue 
  10. │   ├── assets 
  11. │   │   └── logo.png 
  12. │   └── main.js 
  13. └── webpack.config.js 

2、接下来,我们在src文件夹下创建一个名叫marquee的文件夹,在文件夹里面创建marquee.vue和index.js

  1. marquee/ 
  2. ├── index.html 
  3. ├── package.json 
  4. ├── README.md 
  5. ├── .babelrc 
  6. ├── .editorconfig 
  7. ├── .gitignore 
  8. ├── src 
  9. │   ├── App.vue 
  10. │   ├── marquee 
  11. │   │   └── marquee.vue 
  12. │   │   └── index.js 
  13. │   ├── assets 
  14. │   │   └── logo.png 
  15. │   └── main.js 
  16. └── webpack.config.js 

3、开始在marquee.vue封装Vue插件了

  1. <template> 
  2.   <div class="marquee-wrap"
  3.     <!-- 滚动内容 --> 
  4.     <div class="scroll"
  5.       <p class="marquee">{{text}}</p> 
  6.       <!-- 文字副本 --> 
  7.       <p class="copy"></p> 
  8.     </div> 
  9.     <!-- 为了计算总文本宽度,通过css在页面中隐藏 --> 
  10.     <p class="getWidth">{{text}}</p> 
  11.   </div> 
  12. </template> 
  13.  
  14. <script> 
  15. export default { 
  16.   name'marquee'
  17.   props: ['val'], 
  18.   data () { 
  19.     return { 
  20.       timer: null
  21.       text: '' 
  22.     } 
  23.   }, 
  24.   created () { 
  25.     let timer = setTimeout(() => { 
  26.       this.move() 
  27.       clearTimeout(timer) 
  28.     }, 1000) 
  29.   }, 
  30.   // 把父组件传入的arr转化成字符串 
  31.   mounted () { 
  32.     for (let item of this.val) { 
  33.       this.text += ' ' + item 
  34.     } 
  35.   }, 
  36.   methods: { 
  37.     move () { 
  38.       let maxWidth = document.querySelector('.marquee-wrap').clientWidth 
  39.       // 获取文字text 的计算后宽度 (由于overflow的存在,直接获取不到,需要独立的node计算) 
  40.       let width = document.querySelector('.getWidth').scrollWidth 
  41.       // 如果文本内容的宽度小于页面宽度,则表示文字小于等于一行,则不需要滚动 
  42.       if (width <= maxWidth) return 
  43.       let scroll = document.querySelector('.scroll'
  44.       let copy = document.querySelector('.copy'
  45.       copy.innerText = this.text // 文字副本填充 
  46.       let distance = 0 // 位移距离 
  47.       // 设置位移 
  48.       this.timer = setInterval(() => { 
  49.         distance -= 1 
  50.         // 如果位移超过文字宽度,则回到起点 
  51.         if (-distance >= width) { 
  52.           distance = 16 // 距离必须与marquee的margin宽度相同 
  53.         } 
  54.         scroll.style.transform = 'translateX(' + distance + 'px)' 
  55.       }, 20) 
  56.     } 
  57.   }, 
  58.   beforeDestroy () { 
  59.     // 清除计时器 
  60.     clearInterval(this.timer) 
  61.   } 
  62. </script> 
  63.  
  64. <style scoped> 
  65.   .marquee-wrap { 
  66.     width: 100%; 
  67.     overflow: hidden; 
  68.     position: relative
  69.   } 
  70.   .marquee{ 
  71.     margin-right: 16px; 
  72.   } 
  73.   p { 
  74.     word-break:keep-all
  75.     white-space: nowrap; 
  76.     font-size: 16px; 
  77.     font-family: "微软雅黑 Light"
  78.   } 
  79.   .scroll { 
  80.     display: flex; 
  81.   } 
  82.   .getWidth { 
  83.     word-break:keep-all
  84.     white-space:nowrap; 
  85.     position: absolute
  86.     opacity: 0; 
  87.     top: 0; 
  88.   } 
  89. </style> 

 4、为了方便查看效果,可以在App.vue先引入组件查看效果

  1. <template> 
  2.   <div id="app"
  3.        <Marquee :val="msg"></Marquee> 
  4.   </div> 
  5. </template> 
  6.  
  7. <script> 
  8. import Marquee from '../src/marquee/marquee.vue'
  9. export default { 
  10.   name'app'
  11.   data () { 
  12.     return { 
  13.       msg: '啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊' 
  14.     } 
  15.   }, 
  16.   components:{ 
  17.     Marquee 
  18.   } 
  19. </script> 
  20.  
  21. <style> 
  22. #app { 
  23.   font-family: 'Avenir', Helvetica, Arial, sans-serif; 
  24.   -webkit-font-smoothing: antialiased; 
  25.   -moz-osx-font-smoothing: grayscale; 
  26.   text-align: center; 
  27.   color: #2c3e50; 
  28.   margin-top: 60px; 
  29.  
  30. h1, h2 { 
  31.   font-weight: normal; 
  32.  
  33. ul { 
  34.   list-style-type: none; 
  35.   padding: 0; 
  36.  
  37. li { 
  38.   display: inline-block; 
  39.   margin: 0 10px; 
  40.  
  41. a { 
  42.   color: #42b983; 
  43. </style> 

5、启动命令,查看效果

  1. npm install 
  2. npm run dev 

第三步,发布Vue插件前配置

1、编辑marquee文件夹下的index.js

  1. marquee/ 
  2. ├── index.html 
  3. ├── package.json 
  4. ├── README.md 
  5. ├── .babelrc 
  6. ├── .editorconfig 
  7. ├── .gitignore 
  8. ├── src 
  9. │   ├── App.vue 
  10. │   ├── marquee 
  11. │   │   └── marquee.vue 
  12. │   │   └── index.js 
  13. │   ├── assets 
  14. │   │   └── logo.png 
  15. │   └── main.js 
  16. └── webpack.config.js 

index.js

  1. // index.js 
  2.  
  3. // 引入组件 
  4. import marquee from './marquee'
  5. // 组件需要添加name属性,代表注册的组件名称,也可以修改成其他的 
  6. marquee.install = Vue => Vue.component(marquee.name, marquee); //注册组件 
  7.  
  8. export default marquee; 

2、修改webpack.config.js

  1. const NODE_ENV = process.env.NODE_ENV; 
  2. module.exports = { 
  3.   entry: NODE_ENV == 'development' ? './src/main.js' : './src/marquee/index.js'
  4.   output: { 
  5.     path: path.resolve(__dirname, './dist'), 
  6.     publicPath: '/dist/'
  7.     filename: 'marquee.js', //输出文件名 
  8.     library: 'marquee', // 指定的就是你使用require时的模块名 
  9.     libraryTarget: 'umd', // 指定输出格式, UMD 同时支持两种执行环境:node环境、浏览器环境。 
  10.     umdNamedDefine: true // 会对 UMD 的构建过程中的 AMD 模块进行命名。否则就使用匿名的 define 
  11.   }, 

3、打包

  1. npm run build 

如果成功的话,根目录下会出现dist文件夹,里面分别是marquee.js和marquee.js.map

  1. marquee/ 
  2. ├── dist 
  3. │   ├── marquee.js 
  4. │   ├── marquee.js.map 
  5. ├── index.html 
  6. ├── package.json 
  7. ├── README.md 
  8. ├── .babelrc 
  9. ├── .editorconfig 
  10. ├── .gitignore 
  11. ├── src 
  12. │   ├── App.vue 
  13. │   ├── marquee 
  14. │   │   └── marquee.vue 
  15. │   │   └── index.js 
  16. │   ├── assets 
  17. │   │   └── logo.png 
  18. │   └── main.js 
  19. └── webpack.config.js 

4、修改package.json

  1.  "author""maomincoding",  
  2.   "main""dist/marquee.js"
  3.   "license""ISC"
  4.   "keywords": ["marquee"], 
  5.   "private"false

author的值为npm用户名,这里一定要注意。main的值为你刚才打包的路径文件license的值按照以上即可keywords为用户搜索的关键词private设为false, 开源因此需要将这个字段改为 false

5、修改.gitignore

可以 删除 dist/字段,提交的时候一并上传上去。

  1. .DS_Store 
  2. node_modules/ 
  3. dist/     
  4. npm-debug.log 
  5. yarn-error.log 
  6.  
  7. # Editor directories and files 
  8. .idea 
  9. *.suo 
  10. *.ntvs* 
  11. *.njsproj 
  12. *.sln 

6、编辑README.md

这是你上传之后的自述文件,可以在网页上显示,用的也是md语法,这里就不显示代码了,来张网页图示范,也可以直接去marquee查看说明

npm发布包以及更新包还有需要注意的几点问题(这里以发布vue插件为例)

第四步,npm包发布

1、在此之前,你一定要注意先查看登录源,切换到根目录下marquee/

  1. npm config get registry 

如果是 https://registry.npm.taobao.org 那么,输入以下命令,切换到http://registry.npmjs.org

  1. npm config set registry=http://registry.npmjs.org 

切换淘宝源

  1. npm config set registry=https://registry.npm.taobao.org 

2、登录,输入命令

  1. npm login 

相继输入用户名、密码、邮箱。回车出现 Logged in as maomincoding on http://registry.npmjs.org,那么就登陆成功了

3、上传发布

  1. npm publish 

第五步,插件下载使用

替代marquee标签的文字横向滚动Vue插件

1、安装

  1. # install dependencies 
  2. npm i marquee-components 

2、使用

在main.js引入

  1. import marquee from 'marquee-components' 
  2. Vue.use(marquee ); 

在页面使用

  1. <template> 
  2.   <div id="app"
  3.        <marquee :val="msg"></marquee> 
  4.   </div> 
  5. </template> 
  6. <script> 
  7. export default { 
  8.   name'app'
  9.   data () { 
  10.     return { 
  11.       msg: 'vuevuevuevuevuevuevuevuevuevuevuevuevuevuevuevuevue' 
  12.     } 
  13.   } 
  14. </script> 

val后加文字即可,当超过文本容器长度时,触动横向滚动效果。

第六步,npm包更新和撤销

1、撤销包

当你想撤销上传的包时,你可以看看下面的说明:撤销的坏处:

  • 1、根据规范,只有在发包的24小时内才允许撤销发布的包。
  • 2、即使你撤销了发布的包,发包的时候也不能再和被撤销的包的名称和版本重复了(即不能名称相同,版本相同,因为这两者构成的唯一标识已经被“占用”了)
  • 3、这里要说一点,取消发布包可能并不像你想象得那么容易,这种操作是受到诸多限制的,撤销发布的包被认为是一种不好的行为(试想一下你撤销了发布的包[假设它已经在社区内有了一定程度的影响],这对那些已经深度使用并依赖你发布的包的团队是件多么崩溃的事情!)

所以,慎行!!!

撤销命令:

  1. npm unpublish 包名 --force 

送给你一句官方说的话

  1. I sure hope you know what you are doing 

2、更新包

看到了撤销的坏处,所以我推荐你更新包。更新包很简单,只需两步:

(1)、打开根目录下的package.json找到version字段具体体现为:"version":"a.b.c"

1.修复bug,小改动,c加1 2.增加了新特性,但仍能向后兼容,b加1 3.有很大的改动,无法向后兼容,a加1

(2)、根目录下输入npm publis

  1. npm publish 

结语

这里是以发布Vue插件为例,你也可以单独发布一个包。

1、输入命令

  1. npm init 

根据自己的情况输入然后回车,会自动生成一个package.json文件

  1.   "name""vue-cli-configjs"
  2.   "version""2.0.0"
  3.   "description""vue.config.js by vue-cli3+"
  4.   "main""vue.config.js"
  5.   "scripts": { 
  6.     "test""echo \"Error: no test specified\" && exit 1" 
  7.   }, 
  8.   "keywords": [ 
  9.     "vue-cli-config" 
  10.   ], 
  11.   "author""maomincoding"
  12.   "license""ISC" 

2、然后建一个readme.md自述文件,用于说明

3、最后发布即可

  1. npm publish 

原文地址:https://mp.weixin.qq.com/s/7tGMTOHCkq5BirTs1iEx6g

延伸 · 阅读

精彩推荐