You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is because `esbuild` only performs transpilation without type information, it doesn't support certain features like const enum and implicit type-only imports.
@@ -67,8 +69,12 @@ However, some libraries (e.g. [`vue`](https://github.com/vuejs/core/issues/1228)
Starting from Vite 2.5.0, the default value will be `true` if the TypeScript target is `ESNext` or `ES2022` or newer. It is consistent with the [behavior of `tsc` 4.3.2 and later](https://github.com/microsoft/TypeScript/pull/42663). It is also the standard ECMAScript runtime behavior.
71
75
76
+
Other TypeScript targets will default to `false`.
77
+
72
78
But it may be counter-intuitive for those coming from other programming languages or older versions of TypeScript.
73
79
You can read more about the transition in the [TypeScript 3.7 release notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier).
74
80
@@ -78,13 +84,32 @@ Most libraries expect `"useDefineForClassFields": true`, such as [MobX](https://
78
84
79
85
But a few libraries haven't transitioned to this new default yet, including [`lit-element`](https://github.com/lit/lit-element/issues/1030). Please explicitly set `useDefineForClassFields` to `false` in these cases.
Vite does not transpile TypeScript with the configured `target` value by default, following the same behaviour as `esbuild`.
92
+
93
+
The [`esbuild.target`](/config/shared-options.html#esbuild) option can be used instead, which defaults to `esnext` for minimal transpilation. In builds, the [`build.target`](/config/build-options.html#build-target) option takes higher priority and can also be set if needed.
94
+
95
+
::: warning `useDefineForClassFields`
96
+
If `target` is not `ESNext` or `ES2022` or newer, or if there's no `tsconfig.json` file, `useDefineForClassFields` will default to `false` which can be problematic with the default `esbuild.target` value of `esnext`. It may transpile to [static initialization blocks](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks#browser_compatibility) which may not be supported in your browser.
97
+
98
+
As such, it is recommended to set `target` to `ESNext` or `ES2022` or newer, or set `useDefineForClassFields` to `true` explicitly when configuring `tsconfig.json`.
99
+
:::
100
+
81
101
#### Other Compiler Options Affecting the Build Result
If migrating your codebase to `"isolatedModules": true` is an insurmountable effort, you may be able to get around it with a third-party plugin such as [rollup-plugin-friendly-type-imports](https://www.npmjs.com/package/rollup-plugin-friendly-type-imports). However, this approach is not officially supported by Vite.
Copy file name to clipboardExpand all lines: docs/guide/migration.md
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -136,6 +136,31 @@ CLI shortcuts, like `r` to restart the dev server, now require an additional `En
136
136
137
137
This change prevents Vite from swallowing and controlling OS-specific shortcuts, allowing better compatibility when combining the Vite dev server with other processes, and avoids the [previous caveats](https://github.com/vitejs/vite/pull/14342).
138
138
139
+
### Update `experimentalDecorators` and `useDefineForClassFields` TypeScript behaviour
140
+
141
+
Vite 5 uses esbuild 0.19 and removes the compatibility layer for esbuild 0.18, which changes how `experimentalDecorators` and `useDefineForClassFields` are handled.
142
+
143
+
-**`experimentalDecorators` is not enabled by default**
144
+
145
+
You need to set `compilerOptions.experimentalDecorators` to `true` in `tsconfig.json` to use decorators.
146
+
147
+
-**`useDefineForClassFields` defaults depend on the TypeScript `target` value**
148
+
149
+
If `target` is not `ESNext` or `ES2022` or newer, or if there's no `tsconfig.json` file, `useDefineForClassFields` will default to `false` which can be problematic with the default `esbuild.target` value of `esnext`. It may transpile to [static initialization blocks](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks#browser_compatibility) which may not be supported in your browser.
150
+
151
+
As such, it is recommended to set `target` to `ESNext` or `ES2022` or newer, or set `useDefineForClassFields` to `true` explicitly when configuring `tsconfig.json`.
152
+
153
+
```jsonc
154
+
{
155
+
"compilerOptions": {
156
+
// Set true if you use decorators
157
+
"experimentalDecorators":true,
158
+
// Set true if you see parsing errors in your browser
159
+
"useDefineForClassFields":true
160
+
}
161
+
}
162
+
```
163
+
139
164
### Remove `--https` flag and `https: true`
140
165
141
166
`--https` flag sets `https: true`. This config was meant to be used together with the automatic https certification generation feature which [was dropped in Vite 3](https://v3.vitejs.dev/guide/migration.html#automatic-https-certificate-generation). This config no longer makes sense as it will make Vite start a HTTPS server without a certificate.
// esbuild v0.18 only transforms decorators when `experimentalDecorators` is set to `true`.
689
-
// To preserve compat with the esbuild breaking change, we set `experimentalDecorators` to
690
-
// `true` by default if it's unset.
691
-
// TODO: Remove this in Vite 5 and check https://github.com/vitejs/vite/pull/13805#issuecomment-1633612320
0 commit comments