Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: adopt tsconfig-to-swcconfig CLI #206

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions docs/tsconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,19 @@ Another option provided by the community is to convert the `tsconfig.json` file.

The relevant package is [tsconfig-to-swcconfig](https://www.npmjs.com/package/tsconfig-to-swcconfig). Let's see how to wire it up.

There's a corresponding CLI tool, tswc, but it doesn't work well to just generate the `.swcrc` file,
see <https://github.com/Songkeys/tswc/issues/1>
First, add the package to your devDependencies as usual.

So we'll make our own tiny CLI for the underlying package, let's call it `write_swcrc.js`, containing:

```javascript
const {convert} = require('tsconfig-to-swcconfig');
const [tsconfig] = process.argv.slice(2);
console.log(JSON.stringify(convert(tsconfig), undefined, 2));
```

And a bit of BUILD file content to invoke it (you might wrap this in a macro for better developer experience):
Then, invoke it in your `BUILD` file, replacing `[my/pkg]` with the Bazel package where the dependency appears:

```python
js_binary(
name = "converter",
entry_point = "write_swcrc.js",
data = [":node_modules/tsconfig-to-swcconfig"],
)
load("@npm//[my/pkg]:tsconfig-to-swcconfig/package_json.bzl", tsconfig_to_swcconfig = "bin")

js_run_binary(
tsconfig_to_swcconfig.t2s(
name = "write_swcrc",
tool = "converter",
chdir = package_name(),
args = ["./tsconfig.json"],
srcs = ["tsconfig.json"],
args = ["--filename", "$(location tsconfig.json)"],
stdout = ".swcrc",
visibility = ["//:__subpackages__"],
)
```

Expand Down
27 changes: 7 additions & 20 deletions examples/generate_swcrc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_json_matches")
load("@aspect_rules_swc//swc:defs.bzl", "swc")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//examples/generate_swcrc:tsconfig-to-swcconfig/package_json.bzl", tsconfig_to_swcconfig = "bin")

npm_link_all_packages(name = "node_modules")

# Doesn't work: https://github.com/Songkeys/tswc/issues/1
# load("@npm//examples/generate_swcrc:tswc/package_json.bzl", "bin")
# bin.tswc(
# name = "convert",
# srcs = ["tsconfig.json"],
# stdout = ".swcrc0",
# args = ["--debug"],
# )

js_binary(
name = "converter",
data = [":node_modules/tsconfig-to-swcconfig"],
entry_point = "write_swcrc.js",
)

js_run_binary(
tsconfig_to_swcconfig.t2s(
name = "write_swcrc",
srcs = ["tsconfig.json"],
args = ["./tsconfig.json"],
chdir = package_name(),
args = [
"--filename",
"$(location tsconfig.json)",
],
stdout = ".swcrc",
tool = "converter",
visibility = ["//:__subpackages__"],
)

# Demonstrate that it works
Expand Down
2 changes: 1 addition & 1 deletion examples/generate_swcrc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"devDependencies": {
"tsconfig-to-swcconfig": "~2.0.1"
"tsconfig-to-swcconfig": "^2.4.0"
}
}
3 changes: 0 additions & 3 deletions examples/generate_swcrc/write_swcrc.js

This file was deleted.

98 changes: 68 additions & 30 deletions examples/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.