File: transpilers.md

package info (click to toggle)
ts-node 10.9.2%2B~cs64.13.20-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,388 kB
  • sloc: javascript: 15,071; sh: 83; makefile: 15; xml: 9
file content (43 lines) | stat: -rw-r--r-- 1,954 bytes parent folder | download | duplicates (2)
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
---
title: Transpilers
---

ts-node supports third-party transpilers as plugins.  Transpilers such as swc can transform TypeScript into JavaScript
much faster than the TypeScript compiler.  You will still benefit from ts-node's automatic `tsconfig.json` discovery,
sourcemap support, and global ts-node CLI. Plugins automatically derive an appropriate configuration from your existing
`tsconfig.json` which simplifies project boilerplate.

> **What is the difference between a compiler and a transpiler?**
>
> For our purposes, a compiler implements TypeScript's API and can perform typechecking.
> A third-party transpiler does not.  Both transform TypeScript into JavaScript.

## Third-party plugins

The `transpiler` option allows using third-party transpiler plugins with ts-node.  `transpiler` must be given the
name of a module which can be `require()`d.  The built-in `swc` plugin is exposed as `ts-node/transpilers/swc`.

For example, to use a hypothetical "@cspotcode/fast-ts-compiler", first install it into your project: `npm install @cspotcode/fast-ts-compiler`

Then add the following to your tsconfig:

```json title="tsconfig.json"
{
  "ts-node": {
    "transpileOnly": true,
    "transpiler": "@cspotcode/fast-ts-compiler"
  }
}
```

## Write your own plugin

To write your own transpiler plugin, check our [API docs](https://typestrong.org/ts-node/api/interfaces/TranspilerModule.html).

Plugins are `require()`d by ts-node, so they can be a local script or a node module published to npm.  The module must
export a `create` function described by our
[`TranspilerModule`](https://typestrong.org/ts-node/api/interfaces/TranspilerModule.html) interface.  `create` is
invoked by ts-node at startup to create one or more transpiler instances.  The instances are used to transform
TypeScript into JavaScript.

For a working example, check out out our bundled swc plugin: https://github.com/TypeStrong/ts-node/blob/main/src/transpilers/swc.ts