Skip to content

Commit 4ca7c13

Browse files
authoredJul 31, 2023
fix(esbuild): fix static properties transpile when useDefineForClassFields false (#13992)
1 parent 65e5c22 commit 4ca7c13

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
 

‎packages/vite/src/node/__tests__/plugins/esbuild.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,21 @@ describe('transformWithEsbuild', () => {
383383
const actual = await transformClassCode('es2022', {})
384384
expect(actual).toBe(defineForClassFieldsFalseTransformedCode)
385385
})
386+
387+
test('useDefineForClassFields: false and static property should not be transpile to static block', async () => {
388+
const result = await transformWithEsbuild(
389+
`
390+
class foo {
391+
static bar = 'bar'
392+
}
393+
`,
394+
'bar.ts',
395+
{
396+
target: 'esnext',
397+
},
398+
)
399+
expect(result?.code).not.toContain('static {')
400+
})
386401
})
387402
})
388403

‎packages/vite/src/node/plugins/esbuild.ts

+14
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export async function transformWithEsbuild(
9696
}
9797

9898
let tsconfigRaw = options?.tsconfigRaw
99+
const fallbackSupported: Record<string, boolean> = {}
99100

100101
// if options provide tsconfigRaw in string, it takes highest precedence
101102
if (typeof tsconfigRaw !== 'string') {
@@ -150,6 +151,15 @@ export async function transformWithEsbuild(
150151
compilerOptions.experimentalDecorators = true
151152
}
152153

154+
// Compat with esbuild 0.17 where static properties are transpiled to
155+
// static blocks when `useDefineForClassFields` is false. Its support
156+
// is not great yet, so temporarily disable it for now.
157+
// TODO: Remove this in Vite 5, don't pass hardcoded `esnext` target
158+
// to `transformWithEsbuild` in the esbuild plugin.
159+
if (compilerOptions.useDefineForClassFields !== true) {
160+
fallbackSupported['class-static-blocks'] = false
161+
}
162+
153163
// esbuild uses tsconfig fields when both the normal options and tsconfig was set
154164
// but we want to prioritize the normal options
155165
if (options) {
@@ -172,6 +182,10 @@ export async function transformWithEsbuild(
172182
...options,
173183
loader,
174184
tsconfigRaw,
185+
supported: {
186+
...fallbackSupported,
187+
...options?.supported,
188+
},
175189
}
176190

177191
// Some projects in the ecosystem are calling this function with an ESBuildOptions

0 commit comments

Comments
 (0)
Please sign in to comment.