Skip to content

Vite

Library mode - (Guide)

Generate TypeScript declaration files (*.d.ts) from source files - vite-plugin-dts

ts
{
  plugins: [
    dts({
      entryRoot: './src',0
      tsconfigPath: path.join(__dirname, 'tsconfig.lib.json'),
      include: ['./src/**/*'],
      outDir: './dist',

      // If you want to merge all declarations into one file
      // https://github.com/qmhc/vite-plugin-dts?tab=readme-ov-file#usage
      // https://github.com/qmhc/vite-plugin-dts/issues/344#issuecomment-2223439526
      rollupTypes: true,
    }),
  ];
}
From GitHub issue (See docs)
How to use the generated declaration files in a project?

After generating the declaration files, you can use them in your project by adding the following to your vite.config.ts file:

import dts from 'vite-plugin-dts';

Cheat sheet

Vite config

sh
Cannot find name 'document'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.ts(2584) # ![code error]

json
{
  "compilerOptions": {
    "lib": ["dom"]
  }
}

Source - https://stackoverflow.com/a/49112420