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"
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值