-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathts-compiler-options-to-swc-config.spec.ts
More file actions
139 lines (130 loc) · 3.72 KB
/
ts-compiler-options-to-swc-config.spec.ts
File metadata and controls
139 lines (130 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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 = {
baseUrl: undefined,
module: 'es6',
sourcemap: false,
experimentalDecorators: false,
emitDecoratorMetadata: false,
useDefineForClassFields: false,
esModuleInterop: false,
dynamicImport: true,
externalHelpers: false,
ignoreDynamic: false,
jsx: true,
paths: {},
keepClassNames: true,
target: 'es2018',
react: undefined,
swc: {
inputSourceMap: undefined,
sourceRoot: 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,
}
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',
jsx: true,
react: {
pragma: options.jsxFactory,
pragmaFrag: options.jsxFragmentFactory,
importSource: 'react',
runtime: 'automatic',
useBuiltins: true,
},
swc: {
inputSourceMap: undefined,
sourceRoot: undefined,
},
}
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 = {
baseUrl: join(process.cwd(), options.baseUrl!),
module: 'commonjs',
sourcemap: 'inline',
target: 'es5',
experimentalDecorators: options.experimentalDecorators,
emitDecoratorMetadata: options.emitDecoratorMetadata,
useDefineForClassFields: false,
esModuleInterop: options.esModuleInterop,
externalHelpers: true,
ignoreDynamic: false,
dynamicImport: true,
keepClassNames: true,
jsx: true,
react: {
pragma: options.jsxFactory,
pragmaFrag: options.jsxFragmentFactory,
importSource: options.jsxImportSource,
runtime: 'classic',
useBuiltins: true,
},
paths: {
'@test': [join(__dirname, './specific-path-1/test')],
'@another': [join(__dirname, './specific-path-2/another')],
},
swc: {
inputSourceMap: options.inlineSourceMap,
sourceRoot: options.sourceRoot,
},
}
t.deepEqual(swcConfig, expected)
})
test('sourceMap without inlineSourceMap keeps external map output', (t) => {
const options: ts.CompilerOptions = {
sourceMap: true,
inlineSourceMap: false,
}
const swcConfig = tsCompilerOptionsToSwcConfig(options, 'some-file.ts')
t.is(swcConfig.sourcemap, true)
})