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

Ts Compiler to Swc Config: respects decorators config and SWCRC=true #702

Merged
merged 2 commits into from
Mar 9, 2023
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
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