vue3+vite自定義封裝vue組件發(fā)布到npm包的全過程
創(chuàng)建項目
- “vue”: “^3.2.8”
- “vite”: “^2.5.2”
習慣用HB的直接用創(chuàng)建vue3項目即可 或
npm init vite@latest
創(chuàng)建組件
打開項目 在src/components文件夾下新增文件,我這里叫TestBtn.vue
<template>
<button>我是測試要發(fā)布的按鈕組件</button>
</template>
//導出組件名*
<script>
export default{
name:'test-btn'
}
</script>
<script setup>
</script>
<style>
</style>
拿到當前項目里測試一下
//app.vue <template> <div> <test-btn></test-btn> </div> </template> <script setup> import TestBtn from './components/TestBtn.vue' </script> <style scoped> </style>
導出組件
src下新建index.js
//index.js
import TestBtn from "./components/TestBtn.vue"; // 引入封裝好的組件
export { TestBtn } //實現(xiàn)按需引入*
const components = [TestBtn];
const install = function(App, options) {
components.forEach((component) => {
App.component(component.name,component);
});
};
export default { install } // 批量的引入*
使用vite構建
編輯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: {
// 確保外部化處理那些你不想打包進庫的依賴
external: ['vue'],
output: {
// 在 UMD 構建模式下為這些外部化的依賴提供一個全局變量
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
注冊->登錄npm
按提示注冊就可以 npm官網(wǎng)
發(fā)布前準備
在dist文件生成package.json文件,自定義組件名(唯一,重名報錯重新起一個就行),版本號每次上傳要高于前一次版本號
//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
其他項目引用
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')
//頁面.vue
<template>
<test-btn></test-btn>
</template>
<script setup>
</script>
<style scoped>
</style>
參考:
總結
到此這篇關于vue3+vite自定義封裝vue組件發(fā)布到npm包的文章就介紹到這了,更多相關vue3+vite封裝組件發(fā)布npm包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
element?UI中el-dialog實現(xiàn)拖拽功能示例代碼
我們在開發(fā)中常會遇見拖拽的功能,下面這篇文章主要給大家介紹了關于element?UI中el-dialog實現(xiàn)拖拽功能的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-12-12
Vue2實現(xiàn)數(shù)字滾動翻頁效果的示例代碼
這篇文章主要為大家詳細介紹了Vue2如何實現(xiàn)數(shù)字滾動翻頁效果,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下2024-03-03
vue3報錯提示找不到模塊“./XXX.vue”或其相應的類型聲明問題
這篇文章主要介紹了vue3報錯提示找不到模塊“./XXX.vue”或其相應的類型聲明問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
vant/vue手機端長按事件以及禁止長按彈出菜單實現(xiàn)方法詳解
這篇文章主要介紹了vant/vue手機端長按事件以及禁止長按彈出菜單實現(xiàn)方法詳解,需要的朋友可以參考下2022-12-12
vue3中vue.config.js配置Element-plus組件和Icon圖標實現(xiàn)按需自動引入實例代碼
這篇文章主要給大家介紹了關于vue3中vue.config.js配置Element-plus組件和Icon圖標實現(xiàn)按需自動引入的相關資料,在Vue 3中可以通過配置vue.config.js文件來進行按需自動引入,需要的朋友可以參考下2024-02-02

