vite-plugin-typescript

raw JSON →
1.0.4 verified Mon Apr 27 auth: no javascript

A thin wrapper around @rollup/plugin-typescript that improves compatibility with Vite and Vitest. Current stable version 1.0.4. Designed to fix watch mode issues when running Vitest, ensuring proper cleanup and type-safe compilation. Key differentiator: it disables esbuild for Vitest and prevents unsafe watch mode behavior, addressing a specific gap in the Vite ecosystem. Recommended for use in build mode only, not for development due to poor HMR. Depends on vite and @rollup/plugin-typescript as peer dependencies.

error TypeError: (0 , plugin_default) is not a function
cause Using named import instead of default import.
fix
Use import typescript from 'vite-plugin-typescript' instead of import { typescript } from 'vite-plugin-typescript'.
error Error: Cannot find module '@rollup/plugin-typescript'
cause Missing peer dependency @rollup/plugin-typescript.
fix
Install the peer dependency: npm install --save-dev @rollup/plugin-typescript
error [vite] Internal server error: Cannot use esm in cjs mode
cause Trying to require an ESM-only package with require().
fix
Ensure your project is configured as ESM (type: 'module' in package.json) or use dynamic import().
gotcha Plugin does not work well with HMR in development mode; use only for build or Vitest.
fix Apply plugin only in build mode using the `apply: 'build'` option, or conditionally for test environments.
gotcha Requires @rollup/plugin-typescript as a peer dependency; must be installed separately.
fix Install the peer dependency: npm install --save-dev @rollup/plugin-typescript
gotcha Watch mode is disabled when running in Vitest environment; this is intentional but may surprise users expecting watch mode behavior.
fix If you need watch mode in Vitest, do not use this plugin.
npm install vite-plugin-typescript
yarn add vite-plugin-typescript
pnpm add vite-plugin-typescript

Example Vite config using vite-plugin-typescript only for build mode with proper conditional application.

import { defineConfig } from 'vite';
import typescript from 'vite-plugin-typescript';

export default defineConfig({
  plugins: [
    {
      ...typescript(),
      apply: 'build'
    }
  ]
});