当前位置: 首页 > news >正文

发布npmjs组件库

一.初始化项目

1.用Vite创建空项目

2.安装打包与声明文件插件

pnpm i -D vite-plugin-dts sass

二.首先修改项目内容

// src\index.ts
import { type App } from 'vue';
import oneComponent from "./components/oneComponent/index.vue";
import twoComponent from "./components/twoComponent/index.vue";export { oneComponent,twoComponent } //实现按需引入*
oneComponent.name='oneComponent';
twoComponent.name='twoComponent';
const components = [oneComponent,twoComponent];const install = (app: App) => {components.forEach((component) => {app.component(component.name || component.__name || 'unknow', component);});
};
export type { twoComponentOption, twoComponentProps } from './types'export default { install } // 批量的引入*node
// main.ts
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'createApp(App).mount('#app')
// vite/plugins/commponent.ts import Components from 'unplugin-vue-components/vite'
export default () => {return Components({ //自动导入 components下的组件dirs: ['src/components'],//自动在types下新建components.d.ts文件并声明公共组件类型dts: 'src/types/components.d.ts',})
} 
// vite/plugins/index.tsimport vue from '@vitejs/plugin-vue';
import commponents from './commponent' 
import dts from 'vite-plugin-dts';
export default (): [] => {const vitePlusgins: any = [];vitePlusgins.push(vue({script:{defineModel: true}}));vitePlusgins.push(dts({tsconfigPath:'./tsconfig.app.json',include: ['src/**/*.ts','src/**/*.vue'],exclude:['src/types/components.d.ts'],·outDir: 'dist/types',insertTypesEntry: true,rollupTypes:true}));vitePlusgins.push(commponents()); return vitePlusgins
}

//vite.config.ts
import { type ConfigEnv, defineConfig, type UserConfig } from 'vite'
import createPlugins from './vite/plugins'
import path from 'path'
// https://vite.dev/config/
export default defineConfig(({  }: ConfigEnv): UserConfig => {// const env = loadEnv(mode, process.cwd());return {plugins: createPlugins(),resolve: {alias: {'@': path.resolve(__dirname,'src')}},build:{lib:{entry: path.resolve(__dirname,'src/index.ts'),name: 'MyVue3Lib',fileName: (format) => `my-vue3-lib.${format}.js`},rollupOptions: {external: ['vue'],output: {globals: {vue: 'Vue'}}}}}
})
// package.json{"name": "my-vue3-lib-comlibrary","version": "0.0.2","type": "module","main": "./dist/my-vue3-lib.umd.js","module": "./dist/my-vue3-lib.es.js","types": "./dist/types/index.d.ts","files": ["dist"],"exports": {".": {"types": "./dist/types/index.d.ts","import": "./dist/my-vue3-lib.es.js","require": "./dist/my-vue3-lib.umd.js"},"./dist/my-vue3-lib.css": "./dist/my-vue3-lib.css"},"scripts": {"dev": "vite","build": "vue-tsc -b && vite build","preview": "vite preview","pub": "npm run build && npm publish"},"peerDependencies": {"vue": "^3.5.18"},"dependencies": { "sass": "^1.90.0","vue": "^3.5.18"},"devDependencies": {"@types/node": "^24.2.1","@vitejs/plugin-vue": "^6.0.1","@vue/tsconfig": "^0.7.0","typescript": "~5.8.3","unplugin-auto-import": "^20.0.0","unplugin-vue-components": "^29.0.0","vite": "^7.1.2","vite-plugin-dts": "^4.5.4","vue-tsc": "^3.0.5"}
}
// tsconfig.app.json{"extends": "@vue/tsconfig/tsconfig.dom.json","compilerOptions": {"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",/* Linting */"strict": true,"noUnusedLocals": true,"noUnusedParameters": true,"erasableSyntaxOnly": true,"noFallthroughCasesInSwitch": true,"noUncheckedSideEffectImports": true,"baseUrl": ".","allowJs": true,"paths": {"@/*": ["src/*"]},"allowSyntheticDefaultImports": true,},"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
}

// tsconfig.node.json
{"compilerOptions": {"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo","target": "ES2023","lib": ["ES2023"],"module": "ESNext","skipLibCheck": true,/* Bundler mode */"moduleResolution": "bundler","allowImportingTsExtensions": true,"verbatimModuleSyntax": true,"moduleDetection": "force","noEmit": true,/* Linting */"strict": true,"noUnusedLocals": true,"noUnusedParameters": true,"erasableSyntaxOnly": true,"noFallthroughCasesInSwitch": true,"noUncheckedSideEffectImports": true},"include": ["vite.config.ts"]
}
// tsconfig.ts
{"files": [],"references": [{ "path": "./tsconfig.app.json" },{ "path": "./tsconfig.node.json" }]
}

http://www.dtcms.com/a/337243.html

相关文章:

  • 政策技术双轮驱动智慧灯杆市场扩容,塔能科技破解行业痛点
  • 使用AWS S3 + Lambda + MediaConvert 实现上传视频文件并自动转码
  • 像素风球球大作战 HTML 游戏
  • 隐秘参数APP:全面了解手机硬件信息与优化性能
  • 从零开始搭建React+TypeScript+webpack开发环境——多环境配置管理方案
  • WMS仓库管理系统如何远程访问?
  • RAID服务器
  • qsort函数使用及其模拟实现
  • 视觉语言导航(2)——VLN RNN TRANSFORMER 与ATTENTION 2.2+LSTM(单独一节)
  • 分治-归并-493.翻转对-力扣(LeetCode)
  • 艺术品与收藏直播驱动数字化鉴赏与交易
  • 设计模式笔记_行为型_访问者模式
  • 双通道审核智能合约更新路径:基于区块链与AI融合的编程范式分析
  • MATLAB建模与可视化技术文档:从二维到三维
  • snprintf
  • 《Python学习之使用标准库:从入门到实战》
  • 104、【OS】【Nuttx】【周边】文档构建渲染:安装 Sphinx 扩展(上)
  • 从零到一构建企业级GraphRAG系统:GraphRag.Net深度技术解析
  • Python Ovito统计多晶晶粒数量
  • 领域驱动设计(DDD)中的“核心领域逻辑与基础设施分离”原则
  • Maven 生命周期和插件
  • RocketMQ是什么?
  • Day7--滑动窗口与双指针--1695. 删除子数组的最大得分,2958. 最多 K 个重复元素的最长子数组,2024. 考试的最大困扰度
  • 消息队列中的推模式与拉模式
  • C++/Java双平台表单校验实战:合法性+长度+防重复+Tab顺序四重守卫
  • 【从0开始学习Java | 第14篇】集合(上)
  • Day8--滑动窗口与双指针--1004. 最大连续1的个数 III,1658. 将 x 减到 0 的最小操作数,3641. 最长半重复子数组
  • 考问通系统测试分析报告
  • Golang 语言中的指针操作
  • Android中使用RxJava实现网络请求与缓存策略