vue3+vite+ts 发布npm 组件包

1.创建项目

npm create vite@latest my-vue-ts-component -- --template vue-ts

可以强制把它变成公开包,发布

npm publish --access public

查看已发布包的元数据

npm view @zhy/map-utils

显示你当前 npm 客户端登录的「用户名」

npm whoami

2. 禁用 allowImportingTsExtensions

如果你不需要强制要求导入路径以 .ts 结尾,可以在 tsconfig.json 中禁用这个选项:

{

"compilerOptions": {

"allowImportingTsExtensions": false

}

}

3.配置 Vite 打包为组件库模式

import { defineConfig } from 'vite';

import vue from '@vitejs/plugin-vue';

import path from 'path';

export default defineConfig({

plugins: [vue()],

build: {

lib: {

entry: path.resolve(__dirname, 'src/index.ts'),

name: 'MyVueTsComponent',

fileName: (format) => `my-vue-ts-component.${format}.js`,

},

rollupOptions: {

external: ['vue'],

output: {

globals: {

vue: 'Vue',

},

},

},

},

});

4.确保 package.json 文件包含以下字段

{

"name": "@your-username/my-vue-ts-component",

"version": "1.0.0",

"description": "A Vue 3 + TypeScript component template",

"main": "./dist/my-vue-ts-component.umd.cjs",

"module": "./dist/my-vue-ts-component.es.js",

"files": [

"dist"

],

"peerDependencies": {

"vue": "^3.0.0"

},

"scripts": {

"build": "vue-tsc && vite build"

}

}

相关推荐
摘星编程3 分钟前
React Native for OpenHarmony 实战:Linking 链接处理详解
javascript·react native·react.js
胖者是谁22 分钟前
EasyPlayerPro的使用方法
前端·javascript·css
EndingCoder29 分钟前
索引类型和 keyof 操作符
linux·运维·前端·javascript·ubuntu·typescript
liux352837 分钟前
Web集群管理实战指南:从架构到运维
运维·前端·架构
沛沛老爹44 分钟前
Web转AI架构篇 Agent Skills vs MCP:工具箱与标准接口的本质区别
java·开发语言·前端·人工智能·架构·企业开发
摘星编程1 小时前
React Native for OpenHarmony 实战:ImageBackground 背景图片详解
javascript·react native·react.js
小光学长1 小时前
基于Web的长江游轮公共服务系统j225o57w(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库
摘星编程2 小时前
React Native for OpenHarmony 实战:Alert 警告提示详解
javascript·react native·react.js
Joe5562 小时前
vue2 + antDesign 下拉框限制只能选择2个
服务器·前端·javascript
WHS-_-20223 小时前
Tx and Rx IQ Imbalance Compensation for JCAS in 5G NR
javascript·算法·5g