|
| 1 | +# Synchronizing settings with tsconfig.json |
| 2 | + |
| 3 | +TypeScript and SWC both need to be configured in their own files, |
| 4 | +typically `tsconfig.json` and `.swcrc`, respectively. Some settings are meant to be common between the two, such as |
| 5 | +how dependencies are resolved on the disk. |
| 6 | + |
| 7 | +This is not a Bazel-specific problem, so we can just look for existing solutions in the ecosystem, and adapt those to be run under Bazel. |
| 8 | + |
| 9 | +Ideally, we'd like SWC to simply read from `tsconfig.json`, as it |
| 10 | +is the "source-of-truth" for how editors understand `.ts` files. |
| 11 | +There is [an issue](https://github.com/swc-project/swc/issues/1348) already filed for this, but as of Jan 2023 it's not yet supported. |
| 12 | + |
| 13 | +This document explores our options. |
| 14 | + |
| 15 | +## Maintain two files |
| 16 | + |
| 17 | +You might just check in both files as sources. |
| 18 | + |
| 19 | +Since both `tsconfig.json` and `.swcrc` are JSON files, we recommend adding an [`assert_json_matches`](https://docs.aspect.build/rules/aspect_bazel_lib/docs/testing#assert_json_matches) rule to guarantee that they don't accidentally diverge. |
| 20 | + |
| 21 | +A typical example looks like this: |
| 22 | + |
| 23 | +```python |
| 24 | +load("@aspect_bazel_lib//lib:testing.bzl", "assert_json_matches") |
| 25 | + |
| 26 | +# Verify that the "paths" entry is the same |
| 27 | +# between swc and TS language service (in the editor) |
| 28 | +assert_json_matches( |
| 29 | + name = "check_paths", |
| 30 | + file1 = "tsconfig.json", |
| 31 | + file2 = ".swcrc", |
| 32 | + filter1 = ".compilerOptions.paths", |
| 33 | + filter2 = ".jsc.paths", |
| 34 | +) |
| 35 | +``` |
| 36 | + |
| 37 | +## Generate the .swcrc |
| 38 | + |
| 39 | +Another option provided by the community is to convert the `tsconfig.json` file. Under Bazel we can model this as a codegen step that happens automatically. |
| 40 | + |
| 41 | +The relevant package is [tsconfig-to-swcconfig](https://www.npmjs.com/package/tsconfig-to-swcconfig). Let's see how to wire it up. |
| 42 | + |
| 43 | +There's a corresponding CLI tool, tswc, but it doesn't work well to just generate the `.swcrc` file, |
| 44 | +see <https://github.com/Songkeys/tswc/issues/1> |
| 45 | + |
| 46 | +So we'll make our own tiny CLI for the underlying package: |
| 47 | + |
| 48 | +```javascript |
| 49 | +const {convert} = require('tsconfig-to-swcconfig'); |
| 50 | +const [tsconfig] = process.argv.slice(2); |
| 51 | +console.log(JSON.stringify(convert(tsconfig), undefined, 2)); |
| 52 | +``` |
| 53 | + |
| 54 | +And a bit of BUILD file content to invoke it (you might wrap this in a macro for better developer experience): |
| 55 | + |
| 56 | +```python |
| 57 | +js_binary( |
| 58 | + name = "converter", |
| 59 | + entry_point = "write_swcrc.js", |
| 60 | + data = [":node_modules/tsconfig-to-swcconfig"], |
| 61 | +) |
| 62 | + |
| 63 | +js_run_binary( |
| 64 | + name = "write_swcrc", |
| 65 | + tool = "converter", |
| 66 | + chdir = package_name(), |
| 67 | + args = ["./tsconfig.json"], |
| 68 | + srcs = ["tsconfig.json"], |
| 69 | + stdout = ".swcrc", |
| 70 | +) |
| 71 | +``` |
| 72 | + |
| 73 | +Now you can just use the standard `swc` rule with the `swcrc` attribute. |
| 74 | +See `/examples/generate_swcrc` for a full example. |
0 commit comments