-
-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathswc.ts
212 lines (202 loc) · 6.84 KB
/
swc.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import type * as ts from 'typescript';
import type * as swcWasm from '@swc/wasm';
import type * as swcTypes from '@swc/core';
import type { CreateTranspilerOptions, Transpiler } from './types';
export interface SwcTranspilerOptions extends CreateTranspilerOptions {
/**
* swc compiler to use for compilation
* Set to '@swc/wasm' to use swc's WASM compiler
* Default: '@swc/core', falling back to '@swc/wasm'
*/
swc?: string | typeof swcWasm;
}
export function create(createOptions: SwcTranspilerOptions): Transpiler {
const {
swc,
service: { config, projectLocalResolveHelper },
transpilerConfigLocalResolveHelper,
nodeModuleEmitKind,
} = createOptions;
// Load swc compiler
let swcInstance: typeof swcWasm;
if (typeof swc === 'string') {
swcInstance = require(transpilerConfigLocalResolveHelper(
swc,
true
)) as typeof swcWasm;
} else if (swc == null) {
let swcResolved;
try {
swcResolved = transpilerConfigLocalResolveHelper('@swc/core', true);
} catch (e) {
try {
swcResolved = transpilerConfigLocalResolveHelper('@swc/wasm', true);
} catch (e) {
throw new Error(
'swc compiler requires either @swc/core or @swc/wasm to be installed as a dependency. See https://typestrong.org/ts-node/docs/transpilers'
);
}
}
swcInstance = require(swcResolved) as typeof swcWasm;
} else {
swcInstance = swc;
}
// Prepare SWC options derived from typescript compiler options
const compilerOptions = config.options;
const {
esModuleInterop,
sourceMap,
importHelpers,
experimentalDecorators,
emitDecoratorMetadata,
target,
module,
jsxFactory,
jsxFragmentFactory,
strict,
alwaysStrict,
noImplicitUseStrict,
} = compilerOptions;
const nonTsxOptions = createSwcOptions(false);
const tsxOptions = createSwcOptions(true);
function createSwcOptions(isTsx: boolean): swcTypes.Options {
let swcTarget = targetMapping.get(target!) ?? 'es3';
// Downgrade to lower target if swc does not support the selected target.
// Perhaps project has an older version of swc.
// TODO cache the results of this; slightly faster
let swcTargetIndex = swcTargets.indexOf(swcTarget);
for (; swcTargetIndex >= 0; swcTargetIndex--) {
try {
swcInstance.transformSync('', {
jsc: { target: swcTargets[swcTargetIndex] },
});
break;
} catch (e) {}
}
swcTarget = swcTargets[swcTargetIndex];
const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3;
const isNodeModuleKind =
module === ModuleKind.Node12 || module === ModuleKind.NodeNext;
// swc only supports these 4x module options [MUST_UPDATE_FOR_NEW_MODULEKIND]
const moduleType =
module === ModuleKind.CommonJS
? 'commonjs'
: module === ModuleKind.AMD
? 'amd'
: module === ModuleKind.UMD
? 'umd'
: isNodeModuleKind && nodeModuleEmitKind === 'nodecjs'
? 'commonjs'
: isNodeModuleKind && nodeModuleEmitKind === 'nodeesm'
? 'es6'
: 'es6';
// In swc:
// strictMode means `"use strict"` is *always* emitted for non-ES module, *never* for ES module where it is assumed it can be omitted.
// (this assumption is invalid, but that's the way swc behaves)
// tsc is a bit more complex:
// alwaysStrict will force emitting it always unless `import`/`export` syntax is emitted which implies it per the JS spec.
// if not alwaysStrict, will emit implicitly whenever module target is non-ES *and* transformed module syntax is emitted.
// For node, best option is to assume that all scripts are modules (commonjs or esm) and thus should get tsc's implicit strict behavior.
// Always set strictMode, *unless* alwaysStrict is disabled and noImplicitUseStrict is enabled
const strictMode =
// if `alwaysStrict` is disabled, remembering that `strict` defaults `alwaysStrict` to true
(alwaysStrict === false || (alwaysStrict !== true && strict !== true)) &&
// if noImplicitUseStrict is enabled
noImplicitUseStrict === true
? false
: true;
return {
sourceMaps: sourceMap,
// isModule: true,
module: moduleType
? ({
noInterop: !esModuleInterop,
type: moduleType,
strictMode,
// For NodeNext and Node12, emit as CJS but do not transform dynamic imports
ignoreDynamic: nodeModuleEmitKind === 'nodecjs',
} as swcTypes.ModuleConfig)
: undefined,
swcrc: false,
jsc: {
externalHelpers: importHelpers,
parser: {
syntax: 'typescript',
tsx: isTsx,
decorators: experimentalDecorators,
dynamicImport: true,
},
target: swcTarget,
transform: {
decoratorMetadata: emitDecoratorMetadata,
legacyDecorator: true,
react: {
throwIfNamespace: false,
development: false,
useBuiltins: false,
pragma: jsxFactory!,
pragmaFrag: jsxFragmentFactory!,
} as swcTypes.ReactConfig,
},
keepClassNames,
} as swcTypes.JscConfig,
};
}
const transpile: Transpiler['transpile'] = (input, transpileOptions) => {
const { fileName } = transpileOptions;
const swcOptions =
fileName.endsWith('.tsx') || fileName.endsWith('.jsx')
? tsxOptions
: nonTsxOptions;
const { code, map } = swcInstance.transformSync(input, {
...swcOptions,
filename: fileName,
});
return { outputText: code, sourceMapText: map };
};
return {
transpile,
};
}
/** @internal */
export const targetMapping = new Map<ts.ScriptTarget, SwcTarget>();
targetMapping.set(/* ts.ScriptTarget.ES3 */ 0, 'es3');
targetMapping.set(/* ts.ScriptTarget.ES5 */ 1, 'es5');
targetMapping.set(/* ts.ScriptTarget.ES2015 */ 2, 'es2015');
targetMapping.set(/* ts.ScriptTarget.ES2016 */ 3, 'es2016');
targetMapping.set(/* ts.ScriptTarget.ES2017 */ 4, 'es2017');
targetMapping.set(/* ts.ScriptTarget.ES2018 */ 5, 'es2018');
targetMapping.set(/* ts.ScriptTarget.ES2019 */ 6, 'es2019');
targetMapping.set(/* ts.ScriptTarget.ES2020 */ 7, 'es2020');
targetMapping.set(/* ts.ScriptTarget.ES2021 */ 8, 'es2021');
targetMapping.set(/* ts.ScriptTarget.ES2022 */ 9, 'es2022');
targetMapping.set(/* ts.ScriptTarget.ESNext */ 99, 'es2022');
type SwcTarget = typeof swcTargets[number];
/**
* @internal
* We use this list to downgrade to a prior target when we probe swc to detect if it supports a particular target
*/
const swcTargets = [
'es3',
'es5',
'es2015',
'es2016',
'es2017',
'es2018',
'es2019',
'es2020',
'es2021',
'es2022',
] as const;
const ModuleKind = {
None: 0,
CommonJS: 1,
AMD: 2,
UMD: 3,
System: 4,
ES2015: 5,
ES2020: 6,
ESNext: 99,
Node12: 100,
NodeNext: 199,
} as const;