我们已经可以使用rollup开发我们的vue组件库,现在我们把我们开发的文件打包发布到npm上
补全package信息
之前我们的信息不完整,现在补全
{
"name": "ynzy_rollup_test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"module": "dist/datav.es.js",
"browser": "dist/datav.js",
"author": "ynzy",
"homepage": "https://github.com/ynzy/rollup_test",
"scripts": {
"dev": " rollup -wc rollup.config.js --environment NODE_ENV:development ",
"build": "rollup -c rollup.config.js --environment NODE_ENV:development ",
"build:prod": "rollup -c rollup.config.js --environment NODE_ENV:production"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/plugin-transform-flow-strip-types": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"@vue/compiler-sfc": "^3.0.2",
"cross-env": "^7.0.2",
"rollup": "^2.33.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-postcss": "^3.1.8",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-vue": "^6.0.0-beta.11",
"sass": "^1.29.0"
},
"dependencies": {},
"files": [
"dist"
]
}
name是包的名字,可以直接写包名,比如loadash,或者添加域,类似于@koa/router这种,@后面是你npm注册的用户名。key为包的关键字。
如果以@开头为包名,是私有包名,要付费的,发布时候会报错,具体还没了解
402 Payment Required - PUT https://registry.npmjs.org/@ynzy%2frollup_test - You must sign up for private packages
查看npm源
我们要先查看自己的npm源,因为有的人可能使用淘宝源,cnpm之类的,我使用的nrm,所以先切换npm源为:
npm ———— https://registry.npmjs.org/
nrm use npm
创建npm账号
如果我们没有npm账号是没有办法发布的,先创建一个账号,有的忽略
npm adduser
登录账号
npm login
发布包
npm publish
发布成功
使用
我们发布了包以后就可以进行使用了
npm i ynzy_rollup_test
html使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>datav libs example</title> <script src="https://cdn.jsdelivr.net/npm/vue@3.0.2/dist/vue.global.js"></script> <script src="../dist/datav.js"></script> </head> <body> <div id="app"> {{massage}} <test-component/> </div> <script> Vue.createApp({ setup() { var massage = 'hello world' return { massage } } }).use(datav).mount('#app') </script> </body> </html>
vue项目中使用 ```javascript import { createApp } from ‘vue’ import App from ‘./App.vue’ import datav from “ynzy_rollup_test”; createApp(App).use(datav).mount(‘#app’)
```javascript
<template>
<test-component/>
</template>
<script>
</script>