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(core): respect useDefineForClassFields tsconfig value #740

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Options {
jsx?: boolean
experimentalDecorators?: boolean
emitDecoratorMetadata?: boolean
useDefineForClassFields?: boolean
dynamicImport?: boolean
esModuleInterop?: boolean
keepClassNames?: boolean
Expand Down Expand Up @@ -49,6 +50,7 @@ function transformOption(path: string, options?: Options, jest = false): SwcOpti
transform: {
legacyDecorator: Boolean(opts.experimentalDecorators),
decoratorMetadata: Boolean(opts.emitDecoratorMetadata),
useDefineForClassFields: Boolean(opts.useDefineForClassFields),
react: options?.react,
// @ts-expect-error
hidden: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test('default values', (t) => {
sourcemap: false,
experimentalDecorators: false,
emitDecoratorMetadata: false,
useDefineForClassFields: false,
esModuleInterop: false,
dynamicImport: true,
externalHelpers: false,
Expand Down Expand Up @@ -99,6 +100,7 @@ test('should set all values', (t) => {
target: 'es5',
experimentalDecorators: options.experimentalDecorators,
emitDecoratorMetadata: options.emitDecoratorMetadata,
useDefineForClassFields: false,
esModuleInterop: options.esModuleInterop,
externalHelpers: true,
dynamicImport: true,
Expand Down
14 changes: 12 additions & 2 deletions packages/register/read-default-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,31 @@ function toModule(moduleKind: ts.ModuleKind) {
}
}

/**
* The default value for useDefineForClassFields depends on the emit target
* @see https://www.typescriptlang.org/tsconfig#useDefineForClassFields
*/
function getUseDefineForClassFields(compilerOptions: ts.CompilerOptions, target: ts.ScriptTarget): boolean {
return compilerOptions.useDefineForClassFields ?? target >= ts.ScriptTarget.ES2022
}

export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filename: string): Options {
const isJsx = filename.endsWith('.tsx') || filename.endsWith('.jsx') || Boolean(options.jsx)
const target = options.target ?? ts.ScriptTarget.ES2018
return {
module: toModule(options.module ?? ts.ModuleKind.ES2015),
target: toTsTarget(options.target ?? ts.ScriptTarget.ES2018),
target: toTsTarget(target),
jsx: isJsx,
sourcemap: options.sourceMap && options.inlineSourceMap ? 'inline' : Boolean(options.sourceMap),
experimentalDecorators: options.experimentalDecorators ?? false,
emitDecoratorMetadata: options.emitDecoratorMetadata ?? false,
useDefineForClassFields: getUseDefineForClassFields(options, target),
esModuleInterop: options.esModuleInterop ?? false,
dynamicImport: true,
keepClassNames: true,
externalHelpers: Boolean(options.importHelpers),
react:
options.jsxFactory || options.jsxFragmentFactory || options.jsx || options.jsxImportSource
options.jsxFactory ?? options.jsxFragmentFactory ?? options.jsx ?? options.jsxImportSource
? {
pragma: options.jsxFactory,
pragmaFrag: options.jsxFragmentFactory,
Expand Down