Skip to content

Commit

Permalink
fix(core,register): ts Compiler to Swc Config: respects decorators co…
Browse files Browse the repository at this point in the history
…nfig and SWCRC=true (#702)

* fix(register): improve tsCompilerOptionsToSwcConfig to respect decorator config

* fix(core): do not set default jsc config if SWCRC=true
  • Loading branch information
otaviosoares authored Mar 9, 2023
1 parent b7d69a6 commit d421ca8
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 19 deletions.
40 changes: 21 additions & 19 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,28 @@ function transformOption(path: string, options?: Options, jest = false): SwcOpti
opts.esModuleInterop = opts.esModuleInterop ?? true
return {
filename: path,
jsc: {
target: opts.target ?? DEFAULT_ES_TARGET,
externalHelpers: jest ? true : false,
parser: {
syntax: 'typescript' as const,
tsx: typeof opts.jsx !== 'undefined' ? opts.jsx : path.endsWith('.tsx'),
decorators: Boolean(opts.experimentalDecorators),
dynamicImport: Boolean(opts.dynamicImport),
},
transform: {
legacyDecorator: Boolean(opts.experimentalDecorators),
decoratorMetadata: Boolean(opts.emitDecoratorMetadata),
react: options?.react,
hidden: {
jest,
jsc: options?.swc?.swcrc
? undefined
: {
target: opts.target ?? DEFAULT_ES_TARGET,
externalHelpers: jest ? true : false,
parser: {
syntax: 'typescript' as const,
tsx: typeof opts.jsx !== 'undefined' ? opts.jsx : path.endsWith('.tsx'),
decorators: Boolean(opts.experimentalDecorators),
dynamicImport: Boolean(opts.dynamicImport),
},
transform: {
legacyDecorator: Boolean(opts.experimentalDecorators),
decoratorMetadata: Boolean(opts.emitDecoratorMetadata),
react: options?.react,
hidden: {
jest,
},
},
keepClassNames: opts.keepClassNames,
paths: opts.paths,
},
},
keepClassNames: opts.keepClassNames,
paths: opts.paths,
},
minify: false,
isModule: true,
module: {
Expand Down
161 changes: 161 additions & 0 deletions packages/register/__test__/ts-compiler-options-to-swc-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { join } from 'path'

import test from 'ava'
import * as ts from 'typescript'

import { tsCompilerOptionsToSwcConfig } from '../read-default-tsconfig'

test('default values', (t) => {
const options: ts.CompilerOptions = {}
const filename = 'some-file.tsx'
const swcConfig = tsCompilerOptionsToSwcConfig(options, filename)
const expected = {
module: 'es6',
sourcemap: false,
experimentalDecorators: false,
emitDecoratorMetadata: false,
esModuleInterop: false,
swc: {
filename,
inputSourceMap: undefined,
sourceRoot: undefined,
jsc: {
externalHelpers: false,
parser: {
syntax: 'typescript',
tsx: true,
dynamicImport: true,
decorators: undefined,
},
paths: {},
keepClassNames: true,
target: 'es2018',
transform: {
decoratorMetadata: undefined,
legacyDecorator: undefined,
react: undefined,
},
},
},
}
t.deepEqual(swcConfig, expected)
})

test('should set the decorator config', (t) => {
const options: ts.CompilerOptions = {
experimentalDecorators: true,
emitDecoratorMetadata: true,
}
const filename = 'some-file.ts'
const swcConfig = tsCompilerOptionsToSwcConfig(options, filename)
const expected = {
experimentalDecorators: true,
emitDecoratorMetadata: true,
swc: {
filename,
jsc: {
parser: {
decorators: true,
},
transform: {
decoratorMetadata: true,
legacyDecorator: true,
},
},
},
}
t.like(swcConfig, expected)
})

test('should force the jsx config', (t) => {
const options: ts.CompilerOptions = {
jsx: ts.JsxEmit.ReactJSX,
}
const filename = 'some-file.ts'
const swcConfig = tsCompilerOptionsToSwcConfig(options, filename)
const expected = {
module: 'es6',
swc: {
filename,
jsc: {
parser: {
tsx: true,
},
transform: {
react: {
pragma: options.jsxFactory,
pragmaFrag: options.jsxFragmentFactory,
importSource: 'react',
runtime: 'automatic',
useBuiltins: true,
},
},
},
},
}
t.like(swcConfig, expected)
})

test('should set all values', (t) => {
const options: ts.CompilerOptions = {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES5,
sourceMap: true,
esModuleInterop: true,
inlineSourceMap: true,
sourceRoot: 'source-root',
importHelpers: true,
jsx: ts.JsxEmit.None,
experimentalDecorators: true,
emitDecoratorMetadata: true,
paths: {
'@test': ['./specific-path-1/test'],
'@another': ['./specific-path-2/another'],
},
jsxFactory: 'jsx-factory',
jsxFragmentFactory: 'jsx-fragment-factory',
jsxImportSource: 'jsx-import-source',
baseUrl: './packages/register/__test__',
}
const filename = 'some-file.tsx'
const swcConfig = tsCompilerOptionsToSwcConfig(options, filename)
const expected = {
module: 'commonjs',
sourcemap: options.sourceMap,
experimentalDecorators: options.experimentalDecorators,
emitDecoratorMetadata: options.emitDecoratorMetadata,
esModuleInterop: options.esModuleInterop,
swc: {
filename,
inputSourceMap: options.inlineSourceMap,
sourceRoot: options.sourceRoot,
jsc: {
externalHelpers: options.importHelpers,
parser: {
syntax: 'typescript',
tsx: true,
dynamicImport: true,
decorators: options.experimentalDecorators,
},
paths: {
'@test': [join(__dirname, './specific-path-1/test')],
'@another': [join(__dirname, './specific-path-2/another')],
},
keepClassNames: true,
target: 'es5',
transform: {
decoratorMetadata: options.emitDecoratorMetadata,
legacyDecorator: options.experimentalDecorators,
react: {
pragma: options.jsxFactory,
pragmaFrag: options.jsxFragmentFactory,
importSource: options.jsxImportSource,
runtime: 'classic',
useBuiltins: true,
},
},
},
},
}
t.deepEqual(swcConfig, expected)
})
3 changes: 3 additions & 0 deletions packages/register/read-default-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filena
syntax: 'typescript',
tsx: isJsx,
dynamicImport: true,
decorators: options.experimentalDecorators,
},
paths: Object.fromEntries(
Object.entries(options.paths ?? {}).map(([aliasKey, aliasPaths]) => [
Expand All @@ -125,6 +126,8 @@ export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filena
keepClassNames: true,
target: toTsTarget(options.target ?? ts.ScriptTarget.ES2018),
transform: {
decoratorMetadata: options.emitDecoratorMetadata,
legacyDecorator: options.experimentalDecorators,
react:
options.jsxFactory || options.jsxFragmentFactory || options.jsx || options.jsxImportSource
? {
Expand Down

0 comments on commit d421ca8

Please sign in to comment.