Creating type definitions (d.ts) for a vue JS component library #11455
-
Hello, I tried creating a dummy component in a new vue app and getting its types, and with some digging into Vue source, I got to the following d.ts: (Note: The component library I am using is installed as a plugin ( // CvButton.d.ts
import { DefineSetupFnComponent } from 'vue';
export type CvButton = DefineSetupFnComponent<
// Props
{
disabled?: boolean;
},
// Events
{
click(event: MouseEvent): void;
},
>;
declare module 'vue' {
export interface GlobalComponents {
CvButton: CvButton,
'cv-button': CvButton,
}
} I found the From my tests this does work, I am just wondering if I am going the right path or this may be something that may break in a few months. May you provide me guidance on whether I am going on the right path or if I am not, how should I do it? Thank you in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
If you are using Vite, it should be automatically resolved by vue-tsc and Vite. (lib mode) // index.ts
export { default as MyButton } from './components/MyButton.vue'
Is that necessary to load components globally? I think |
Beta Was this translation helpful? Give feedback.
-
Sorry I just saw this after a week. In detail, you can make your library by pnpm create vue In the project folder, make your components, and export it in a entry file like // If you have lots of components to export, maybe use a Nodejs script to generate the file
export { default as Button } from './Button.vue'
// ... Install pnpm i -D vite-plugin-dts Create a file called {
"extends": "./tsconfig.app.json",
}
In vite.config.ts: // Basic config
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import dts from 'vite-plugin-dts'
export default defineConfig({
plugins: [
vue(),
dts({
tsconfigPath: './tsconfig.build.json',
})
],
build: {
lib: {
entry:'./src/index.ts',
formats: ['es'],
},
rollupOptions: {
external: ['vue']
}
},
}) Configure it according to your needs. Do not forget to edit your {
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/index.js"
},
},
"module": "./dist/index.js",
"types": "./dist/src/index.d.ts",
"files": [
"dist"
],
} Which indicated what files will be used by users, also configure it according to your needs. And that is how users use it: <script setup>
import { Button } from 'my-library'
</script>
<template>
<Button />
</template> It is very troublesome to import it every time, so users can use // users' vite.config.ts
export default defineConfig({
plugins: [
vue(),
Components({
resolvers: [
{
type: 'component',
resolve: (name: string) => {
if (name === 'Button')
return { name, from: 'my-library' }
},
}
]
})
]
}) Or you can also export the resolver in your library: /** Maybe you wanna automatically generate it by Nodejs script*/
const LIST: Array<string> = []
export default function resolvers(): ComponentResolver {
const set = new Set(LIST)
return {
type: 'component',
resolve: (name: string) => {
if (set.has(name))
return { name, from: 'my-library' }
},
}
} |
Beta Was this translation helpful? Give feedback.
Sorry I just saw this after a week. In detail, you can make your library by
vite-plugin-dts
(for types) andVite
:In the project folder, make your components, and export it in a entry file like
index.ts
:Install
vite-plugin-dts
:Create a file called
tsconfig.build.json
in the root:tsconfig.app.json
should be generated automatically when you usepnpm create vue
.In vite.config.ts: