Applies the TypeScript compiler during Vite transform build phase.
This plugin may allow the use of language features not yet supported by vite's default compiler, esbuild.
Important
- It does not change or disable any of
vitecompiler/features/options. - It only transpiles the code using the
typescriptcompiler and letsvitemove on with the transpiled code. - The
typescriptconfiguration is what controls the transpilation and its outputs.
npm install --save-dev vite-plugin-typescript-transformSee the Options interface and its inline documentation.
The new ECMAScript decorators are not supported by esbuild (yet), but they are supported by typescript since v5 (see the announcement). This example down-levels the new decorators into code that is usable in runtimes that do not yet support it.
import ts from 'typescript';
import { defineConfig } from 'vite';
import { vitePluginTypescriptTransform } from 'vite-plugin-typescript-transform';
export default defineConfig({
// ...your vite configuration
plugins: [
vitePluginTypescriptTransform({
enforce: 'pre',
filter: {
files: {
include: /\.ts$/,
},
},
tsconfig: {
override: {
target: ts.ScriptTarget.ES2021,
},
},
}),
],
});Sourcemap is likely to be incorrect: a plugin (vite-plugin-typescript-transform) was used to transform files, but didn't generate a sourcemap for the transformation.
This means that the plugin transformed the code but it did not generate sourcemaps for the changes.
The detail here is that the plugin is using typescript directly, so its configuration play a crucial role in what happens.
More specifically: to generate sourcemaps, sourcemaps need to be enabled in the typescript configuration.
This can be achieved in two different ways:
- by setting
compilerOptions.sourceMaptotruein thetsconfig.jsonfile - by setting
tsconfig.override.sourceMaptotruein the plugin configuration