Skip to content

Commit

Permalink
fix(esbuild): use useDefineForClassFields: false when no `compilerO…
Browse files Browse the repository at this point in the history
…ptions.target` is declared (#13708)
  • Loading branch information
sapphi-red committed Jul 4, 2023
1 parent 2b1ffe8 commit 7ef2472
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/vite/src/node/__tests__/plugins/esbuild.spec.ts
Expand Up @@ -378,6 +378,11 @@ describe('transformWithEsbuild', () => {
})
expect(actual).toBe(defineForClassFieldsTrueTransformedCode)
})

test('target: es2022 and tsconfig.target: undefined => false', async () => {
const actual = await transformClassCode('es2022', {})
expect(actual).toBe(defineForClassFieldsFalseTransformedCode)
})
})
})

Expand Down
10 changes: 10 additions & 0 deletions packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -132,6 +132,16 @@ export async function transformWithEsbuild(
...tsconfigRaw?.compilerOptions,
}

// esbuild uses `useDefineForClassFields: true` when `tsconfig.compilerOptions.target` isn't declared
// but we want `useDefineForClassFields: false` when `tsconfig.compilerOptions.target` isn't declared
// to align with the TypeScript's behavior
if (
compilerOptions.useDefineForClassFields === undefined &&
compilerOptions.target === undefined
) {
compilerOptions.useDefineForClassFields = false
}

// esbuild uses tsconfig fields when both the normal options and tsconfig was set
// but we want to prioritize the normal options
if (options) {
Expand Down

0 comments on commit 7ef2472

Please sign in to comment.