Vite TypeScript Transform Plugin
vite-plugin-typescript-transform is a Vite plugin designed to integrate the full TypeScript compiler into the Vite build transform phase. Its primary purpose is to enable advanced TypeScript features, such as the new ECMAScript decorators, that Vite's default transpiler, esbuild, might not yet support. The plugin operates by transpiling code using the TypeScript compiler before esbuild processes it, allowing developers to leverage the latest TypeScript syntax without waiting for esbuild updates. Currently at version 1.3.1, the package demonstrates active maintenance with several releases in the past year addressing bug fixes and feature enhancements, such as improved tsconfig parsing. It functions as an augmentation to Vite's pipeline, rather than a replacement, and requires careful configuration of plugin order to ensure correct operation.
Common errors
-
Syntax Error: Unexpected token '@'
cause Using new ECMAScript decorators (or legacy decorators with improper configuration) that esbuild does not support, and `vite-plugin-typescript-transform` either isn't configured correctly or is running too late.fixEnsure `vite-plugin-typescript-transform` is installed, correctly imported, and configured with `enforce: 'pre'` in your `vite.config.ts`. Additionally, set appropriate TypeScript `target` and `compilerOptions` (e.g., for new decorators, `"target": "ES2021"` or higher in `tsconfig.override`). -
Error: Plugin 'vite-plugin-typescript-transform' is not processing files, or TypeScript features are not being transpiled as expected.
cause The plugin's configuration is incorrect, or its execution order is clashing with other Vite plugins or Vite's default behavior.fixVerify that `enforce: 'pre'` is set in the plugin configuration. Check the `filter` options to ensure the correct file types (e.g., `.ts` files) are being included for transformation. Review `tsconfig.override` for desired `compilerOptions`. -
Failed to parse tsconfig.json: 'extends' is not supported (or similar error regarding tsconfig structure).
cause Using `extends` in `tsconfig.json` with an older version of the plugin that did not fully support this feature.fixUpgrade to `vite-plugin-typescript-transform` version 1.3.0 or higher, which introduced support for the `extends` property in `tsconfig.json`.
Warnings
- breaking The initial v1.0.0 release introduced the core plugin functionality. While not a breaking change from a previous version of this plugin, it established the first stable API, meaning any significant changes in subsequent major versions would be considered breaking against this baseline.
- gotcha This plugin must be configured to run before Vite's default esbuild transpilation to properly apply TypeScript transformations for features not supported by esbuild. Failing to do so can result in syntax errors or incorrect output.
- gotcha The plugin does not disable or replace Vite's built-in esbuild compiler. It only pre-processes code using the TypeScript compiler. Esbuild will still process the output from this plugin. Ensure your TypeScript `target` and `tsconfig.override` options align with what esbuild can ultimately handle after the initial TS compilation step.
- gotcha Earlier versions had issues correctly parsing `tsconfig.json` files that used the `extends` property or respecting the `tsconfig.override` option.
Install
-
npm install vite-plugin-typescript-transform -
yarn add vite-plugin-typescript-transform -
pnpm add vite-plugin-typescript-transform
Imports
- vitePluginTypescriptTransform
const vitePluginTypescriptTransform = require('vite-plugin-typescript-transform');import { vitePluginTypescriptTransform } from 'vite-plugin-typescript-transform'; - defineConfig
import { defineConfig } from 'vite'; - ts
import ts from 'typescript';
Quickstart
import ts from 'typescript';
import { defineConfig } from 'vite';
import { vitePluginTypescriptTransform } from 'vite-plugin-typescript-transform';
export default defineConfig({
plugins: [
vitePluginTypescriptTransform({
enforce: 'pre',
filter: {
files: {
include: /\.ts$/,
},
},
tsconfig: {
override: {
target: ts.ScriptTarget.ES2021,
// Enable new ECMAScript decorators support if needed
// 'experimentalDecorators' is for legacy decorators
// 'useDefineForClassFields' often needed for new decorators
// For new decorators, ensure TypeScript version is >=5.0
},
},
}),
],
// ... other vite configuration
});