vue.js 如何将类型添加到Vite库版本?

np8igboo  于 2022-11-17  发布在  Vue.js
关注(0)|答案(3)|浏览(185)

我遵循了使用库模式的vite文档,我能够生成一个工作的组件库。
我用vue-ts预设创建了这个项目,在我的组件中我定义了道具的类型,并使用了一些接口。但是当我构建库时,没有包含任何类型。
如何为最终的构建添加类型,无论是自动从组件推断还是使用定义文件手动推断?

更多信息以下是有关我的文件的更多信息:

tsconfig.json

{
  "name": "@mneelansh/test-lib",
  "private": false,
  "version": "0.0.2",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc --noEmit && vite build",
    "preview": "vite preview"
  },
  "emitDeclarationOnly": true, // testing
  "declaration": true, // testing
  "main": "./dist/lib.umd.js",
  "module": "./dist/lib.es.js",
  "types": "./dist/main.d.ts",
  "exports": {
    ".": {
      "import": "./dist/lib.es.js",
      "require": "./dist/lib.umd.js"
    },
    "./dist/style.css": "./dist/style.css"
  },
  "files": [
    "dist"
  ],
  "dependencies": {
    "@types/node": "^17.0.25",
    "vue": "^3.2.25"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^2.3.1",
    "typescript": "^4.5.4",
    "vite": "^2.9.5",
    "vue-tsc": "^0.34.7"
  }
}

我添加了emitDeclarationOnlydeclaration属性,但没有任何帮助。
我的vite.config.ts

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";

const path = require("path");

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    lib: {
      entry: path.resolve(__dirname, "src/index.ts"),
      name: "Button",
      fileName: (format) => `lib.${format}.js`,
    },
    rollupOptions: {
      external: ["vue"],
      output: {
        globals: {
          vue: "Vue",
        },
      },
    },
  },
  plugins: [vue()],
});
jexiocij

jexiocij1#

您可以使用vite-plugin-dts

import dts from "vite-plugin-dts";

export default defineConfig({
  plugins: [
    dts({
      insertTypesEntry: true,
    }),
  ],
r1zk6ea1

r1zk6ea12#

通常对于vite和typescript项目,你需要在构建之前添加类型检查,因为vite自己不会做。这里我也使用了Julien Kode的post中的vite-plugin-dts,用于类型检查rollup-plugin-typescript2
最后我的vite.config.js

import { defineConfig } from 'vite';
import Vue from '@vitejs/plugin-vue2';
import dts from 'vite-plugin-dts';
import rollupTs from 'rollup-plugin-typescript2';

export default defineConfig({
    plugins: [
        Vue(),
        dts({ insertTypesEntry: true }),
        // only for type checking
        {
            ...rollupTs({
                check: true,
                tsconfig: './tsconfig.json',
                tsconfigOverride: {
                    noEmits: true,
                },
            }),
            // run before build
            enforce: 'pre',
        },
    ],
    build: {
        sourcemap: true,
        lib: {
            entry: './src/index.ts',
            fileName: 'index',
        },
        rollupOptions: {
            // make sure to externalize deps that shouldn't be bundled
            // into your library
            external: [
                'vue',
                'vue-class-component',
                'vue-property-decorator',
                'vuex',
                'vuex-class',
            ],
            output: {
                // Provide global variables to use in the UMD build
                // for externalized deps
                globals: {
                    vue: 'Vue',
                },
            },
        },
    },
});
7kjnsjlb

7kjnsjlb3#

我个人认为更好的方法是使用vue-tsc:

vue-tsc --declaration --emitDeclarationOnly

请参阅https://stackoverflow.com/a/70343443/398287

相关问题