-
-
Notifications
You must be signed in to change notification settings - Fork 36
docs: tell users how to run swc using their tsconfig settings #152
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
e2e/ | ||
e2e/ | ||
examples/node_modules | ||
examples/generate_swcrc/node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
docs/*.md linguist-generated=true | ||
docs/repositories.md linguist-generated=true | ||
docs/swc.md linguist-generated=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
bazel-* | ||
node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Synchronizing settings with tsconfig.json | ||
|
||
TypeScript and SWC both need to be configured in their own files, | ||
typically `tsconfig.json` and `.swcrc`, respectively. Some settings are meant to be common between the two, such as | ||
how dependencies are resolved on the disk. | ||
|
||
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. | ||
|
||
Ideally, we'd like SWC to simply read from `tsconfig.json`, as it | ||
is the "source-of-truth" for how editors understand `.ts` files. | ||
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. | ||
|
||
This document explores our options. | ||
|
||
## Maintain two files | ||
|
||
You might just check in both files as sources. | ||
|
||
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. | ||
|
||
A typical example looks like this: | ||
|
||
```python | ||
load("@aspect_bazel_lib//lib:testing.bzl", "assert_json_matches") | ||
|
||
# Verify that the "paths" entry is the same | ||
# between swc and TS language service (in the editor) | ||
assert_json_matches( | ||
name = "check_paths", | ||
file1 = "tsconfig.json", | ||
file2 = ".swcrc", | ||
filter1 = ".compilerOptions.paths", | ||
filter2 = ".jsc.paths", | ||
) | ||
``` | ||
|
||
## Generate the .swcrc | ||
|
||
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. | ||
|
||
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> | ||
|
||
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): | ||
|
||
```python | ||
js_binary( | ||
name = "converter", | ||
entry_point = "write_swcrc.js", | ||
data = [":node_modules/tsconfig-to-swcconfig"], | ||
) | ||
|
||
js_run_binary( | ||
name = "write_swcrc", | ||
tool = "converter", | ||
chdir = package_name(), | ||
args = ["./tsconfig.json"], | ||
srcs = ["tsconfig.json"], | ||
stdout = ".swcrc", | ||
) | ||
``` | ||
|
||
Now you can just use the standard `swc` rule with the `swcrc` attribute. | ||
See `/examples/generate_swcrc` for a full example. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
load("@npm//:defs.bzl", "npm_link_all_packages") | ||
|
||
# Required to create the virtual store at the root of the pnpm workspace | ||
# We don't have any node_modules to lay out in this directory. | ||
npm_link_all_packages() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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") | ||
|
||
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( | ||
name = "write_swcrc", | ||
srcs = ["tsconfig.json"], | ||
args = ["./tsconfig.json"], | ||
chdir = package_name(), | ||
stdout = ".swcrc", | ||
tool = "converter", | ||
) | ||
|
||
# Demonstrate that it works | ||
swc( | ||
name = "compile", | ||
swcrc = ".swcrc", | ||
) | ||
|
||
# Verify that our options got passed through | ||
assert_json_matches( | ||
name = "test", | ||
file1 = ".swcrc", | ||
file2 = "tsconfig.json", | ||
filter1 = ".jsc.target", | ||
filter2 = ".compilerOptions.target", | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"private": true, | ||
"devDependencies": { | ||
"tsconfig-to-swcconfig": "~2.0.1" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const a = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2017", | ||
"module": "AMD" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { convert } = require("tsconfig-to-swcconfig"); | ||
const [tsconfig] = process.argv.slice(2); | ||
console.log(JSON.stringify(convert(tsconfig), undefined, 2)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
packages: | ||
- generate_swcrc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be a link?