(createOptions: SwcTranspilerOptions)
| 16 | } |
| 17 | |
| 18 | export function create(createOptions: SwcTranspilerOptions): Transpiler { |
| 19 | const { |
| 20 | swc, |
| 21 | service: { config, projectLocalResolveHelper }, |
| 22 | transpilerConfigLocalResolveHelper, |
| 23 | nodeModuleEmitKind, |
| 24 | } = createOptions; |
| 25 | |
| 26 | // Load swc compiler |
| 27 | let swcInstance: SwcInstance; |
| 28 | // Used later in diagnostics; merely needs to be human-readable. |
| 29 | let swcDepName: string = 'swc'; |
| 30 | if (typeof swc === 'string') { |
| 31 | swcDepName = swc; |
| 32 | swcInstance = require(transpilerConfigLocalResolveHelper(swc, true)) as SwcInstance; |
| 33 | } else if (swc == null) { |
| 34 | let swcResolved; |
| 35 | try { |
| 36 | swcDepName = '@swc/core'; |
| 37 | swcResolved = transpilerConfigLocalResolveHelper(swcDepName, true); |
| 38 | } catch (e) { |
| 39 | try { |
| 40 | swcDepName = '@swc/wasm'; |
| 41 | swcResolved = transpilerConfigLocalResolveHelper(swcDepName, true); |
| 42 | } catch (e) { |
| 43 | throw new Error( |
| 44 | 'swc compiler requires either @swc/core or @swc/wasm to be installed as a dependency. See https://typestrong.org/ts-node/docs/transpilers' |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | swcInstance = require(swcResolved) as SwcInstance; |
| 49 | } else { |
| 50 | swcInstance = swc as any as SwcInstance; |
| 51 | } |
| 52 | |
| 53 | // Prepare SWC options derived from typescript compiler options |
| 54 | const { nonTsxOptions, tsxOptions } = createSwcOptions(config.options, nodeModuleEmitKind, swcInstance, swcDepName); |
| 55 | |
| 56 | const transpile: Transpiler['transpile'] = (input, transpileOptions) => { |
| 57 | const { fileName } = transpileOptions; |
| 58 | const swcOptions = fileName.endsWith('.tsx') || fileName.endsWith('.jsx') ? tsxOptions : nonTsxOptions; |
| 59 | const { code, map } = swcInstance.transformSync(input, { |
| 60 | ...swcOptions, |
| 61 | filename: fileName, |
| 62 | }); |
| 63 | return { outputText: code, sourceMapText: map }; |
| 64 | }; |
| 65 | |
| 66 | return { |
| 67 | transpile, |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | /** @internal */ |
| 72 | export const targetMapping = new Map<ts.ScriptTarget, SwcTarget>(); |
nothing calls this directly
no test coverage detected
searching dependent graphs…