I am currently developing a package with several ts
functions that will be utilized by multiple repositories, including mobile and web applications. In our team, we use vite
as our primary build tool, which is also integrated into the repository.
Below is the contents of my vite.config.ts
file:
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
export default defineConfig({
build: {
lib: {
name: 'project-name',
entry: './src/index.ts',
fileName: 'index',
formats: ['cjs', 'umd']
},
outDir: 'dist'
},
plugins: [
dts({
insertTypesEntry: true
})
]
})
And here is the content of the tsconfig.json
file:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"sourceMap": true,
"declaration": true,
"outDir": "dist",
"declarationDir": "dist",
"baseUrl": ".",
"paths": {
"@/": [
"src/*"
]
},
"lib": [
"esnext"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx"
],
"exclude": [
"node_modules",
"dist"
]
}
The issue I am facing is that when the package is installed in other repositories, the types are not being loaded properly. This results in all classes and functions having an any
type, and even VS Code marking imported types as any
. Type information is crucial for me, so I want to ensure it is exported correctly.
Methods I have attempted include:
- Trying the
vite-tsconfig-paths
plugin instead ofvite-plugin-dts
, but it failed to generate any*.d.ts
files in the/dist
directory - Experimenting with various
tsconfig
options, as it seems the plugin might not be recognizing thetsconfig.json
file (correctly named and located in the root directory)
Additional details:
- Types are exported using the syntax
export type ...
- In
src/index.ts
, everything is imported, including types - The current
index.d.ts
file only contains one line:
export * from './index'
This means it exports the contents of index.js
generated during the build process