Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(esbuild): use useDefineForClassFields: false when no compilerOptions.target is declared #13708

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/vite/src/node/__tests__/plugins/esbuild.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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