vue3+vite自定義封裝vue組件發(fā)布到npm包的全過(guò)程
創(chuàng)建項(xiàng)目
- “vue”: “^3.2.8”
- “vite”: “^2.5.2”
習(xí)慣用HB的直接用創(chuàng)建vue3項(xiàng)目即可 或
npm init vite@latest
創(chuàng)建組件
打開(kāi)項(xiàng)目 在src/components文件夾下新增文件,我這里叫TestBtn.vue
<template>
<button>我是測(cè)試要發(fā)布的按鈕組件</button>
</template>
//導(dǎo)出組件名*
<script>
export default{
name:'test-btn'
}
</script>
<script setup>
</script>
<style>
</style>
拿到當(dāng)前項(xiàng)目里測(cè)試一下
//app.vue <template> <div> <test-btn></test-btn> </div> </template> <script setup> import TestBtn from './components/TestBtn.vue' </script> <style scoped> </style>
導(dǎo)出組件
src下新建index.js
//index.js
import TestBtn from "./components/TestBtn.vue"; // 引入封裝好的組件
export { TestBtn } //實(shí)現(xiàn)按需引入*
const components = [TestBtn];
const install = function(App, options) {
components.forEach((component) => {
App.component(component.name,component);
});
};
export default { install } // 批量的引入*
使用vite構(gòu)建
編輯vite.config.js文件,新增build屬性,vite中文文檔
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
build: {
lib: {
entry: path.resolve(__dirname, 'src/index.js'),
name: 'TestBtn',
fileName: (format) => `test-btn.${format}.js`
},
rollupOptions: {
// 確保外部化處理那些你不想打包進(jìn)庫(kù)的依賴
external: ['vue'],
output: {
// 在 UMD 構(gòu)建模式下為這些外部化的依賴提供一個(gè)全局變量
globals: {
vue: 'Vue'
}
}
}
}
})
修改package.json文件
{
"name": "test-btn-ui",
"version": "0.0.1",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"files": ["dist"],
"main": "./dist/test-btn.umd.js",
"module": "./dist/test-btn.es.js",
"exports": {
".": {
"import": "./dist/test-btn.es.js",
"require": "./dist/test-btn.umd.js"
}
},
"dependencies": {
"vue": "^3.2.8"
},
"devDependencies": {
"@vitejs/plugin-vue": "^1.6.0",
"@vue/compiler-sfc": "^3.2.6",
"vite": "^2.5.2"
}
}
打包
生成dist文件
npm run build
注冊(cè)->登錄npm
按提示注冊(cè)就可以 npm官網(wǎng)
發(fā)布前準(zhǔn)備
在dist文件生成package.json文件,自定義組件名(唯一,重名報(bào)錯(cuò)重新起一個(gè)就行),版本號(hào)每次上傳要高于前一次版本號(hào)
//dist文件下
npm init -y
//package.json
{
"name": "test-btn-ui",
"version": "1.0.4",
"description": "",
"main": "test-btn.es.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
發(fā)布到npm
npm publish
其他項(xiàng)目引用
npm i test-btn-ui
//main.js
import { createApp } from 'vue'
import App from './App.vue'
import TestBtnUI from 'test-btn-ui'
import 'test-btn-ui/style.css'
createApp(App).use(TestBtnUI).mount('#app')
//頁(yè)面.vue
<template>
<test-btn></test-btn>
</template>
<script setup>
</script>
<style scoped>
</style>
參考:
總結(jié)
到此這篇關(guān)于vue3+vite自定義封裝vue組件發(fā)布到npm包的文章就介紹到這了,更多相關(guān)vue3+vite封裝組件發(fā)布npm包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue 組件封裝 并使用 NPM 發(fā)布的教程
- Vue cli3 庫(kù)模式搭建組件庫(kù)并發(fā)布到 npm的流程
- 詳解如何制作并發(fā)布一個(gè)vue的組件的npm包
- Vue.js構(gòu)建你的第一個(gè)包并在NPM上發(fā)布的方法步驟
- 自定義Vue組件打包、發(fā)布到npm及使用教程
- 一個(gè)vue組件庫(kù)發(fā)布到npm的完整實(shí)現(xiàn)過(guò)程
- vue組件打包并發(fā)布到npm的全過(guò)程
- vue使用npm發(fā)布自己的公網(wǎng)包
- vue組件發(fā)布成npm包
- vue3+webpack中npm發(fā)布組件的實(shí)現(xiàn)
相關(guān)文章
關(guān)于vue-property-decorator的基礎(chǔ)使用實(shí)踐
這篇文章主要介紹了關(guān)于vue-property-decorator的基礎(chǔ)使用實(shí)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
3分鐘迅速學(xué)會(huì)Vue中methods方法使用技巧
最近在學(xué)習(xí)Vue,感覺(jué)methods還是有必有總結(jié)一下的,下面這篇文章主要給大家介紹了關(guān)于3分鐘迅速學(xué)會(huì)Vue中methods方法使用技巧的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Vue實(shí)現(xiàn)不同用戶權(quán)限的方法詳解
在項(xiàng)目中,實(shí)現(xiàn)不同用戶的權(quán)限控制是常見(jiàn)的需求也是常見(jiàn)的功能模塊,本文將以 vue 為主要的代碼框架介紹幾種常見(jiàn)的權(quán)限控制方式,有需要的可以了解下2025-03-03
在Vue中實(shí)現(xiàn)對(duì)文件的壓縮和解壓縮功能
在前端開(kāi)發(fā)中,文件的壓縮和解壓縮是經(jīng)常需要用到的功能,尤其是在需要上傳和下載文件的場(chǎng)景下,文件壓縮可以減小文件大小,加快文件傳輸速度,提高用戶體驗(yàn),本文將介紹在Vue項(xiàng)目中如何進(jìn)行文件的壓縮和解壓縮,需要的朋友可以參考下2023-11-11
el-table表格動(dòng)態(tài)合并行及合并行列實(shí)例詳解
在使用el-table的時(shí)候經(jīng)常會(huì)涉及到表格的列合并,包括表格操作列的合并,下面這篇文章主要給大家介紹了關(guān)于el-table表格動(dòng)態(tài)合并行及合并行列的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10

