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

feat: add emitIsolatedDts support #270

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

feat: add emitIsolatedDts support #270

wants to merge 1 commit into from

Conversation

jbedard
Copy link
Member

@jbedard jbedard commented Aug 26, 2024

Add experimental support for the jsc.experimental.emitIsolatedDts feature to output .d.ts instead of .js.


Changes are visible to end-users: no

Test plan

  • Covered by existing test cases
  • New test cases added

@jbedard jbedard force-pushed the dts_outs branch 3 times, most recently from 002ac69 to 84b06f8 Compare August 27, 2024 00:14
@jbedard jbedard requested a review from alexeagle August 27, 2024 00:16
@jbedard jbedard marked this pull request as ready for review August 27, 2024 00:17
Copy link
Member

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, seems like an obvious next step

swc/defs.bzl Outdated
js_outs = _swc_lib.calculate_js_outs(srcs, out_dir, root_dir)
map_outs = _swc_lib.calculate_map_outs(srcs, source_maps, out_dir, root_dir)
if kwargs.get("experimental_emit_isolated_dts", False):
dts_outs = _swc_lib.calculate_dts_outs(srcs, out_dir, root_dir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it produce d.ts.map files too, like tsc does?

Copy link
Member Author

@jbedard jbedard Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can find it seems like swc can do one or the other right now, not both. If you pass --out-file more then once it's an error, if you don't pass it at all it outputs only the .js to stdout instead...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I read that wrong... I meant it can't produce .js and .d.ts, I'll look into .d.ts.map more

@@ -66,6 +66,10 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#data for more
"root_dir": attr.string(
doc = "a subdirectory under the input package which should be consider the root directory of all the input files",
),
"experimental_emit_isolated_dts": attr.bool(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider whether or not this ought to have the experimental_ prefix? When swc changes their flag, will we also change our attribute name? That will be breaking, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to be able to make breaking changes to this still so I figured putting experimental_ right in the API is a good way to do that for now. Then it will be breaking when we rename to make it stable and we'll have to keep it non-breaking from that point forward.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've never had an attribute prefixed with experimental_ before, and never wrote that it's not public API. I guess it's a fine convention, just surprised that we invent a new one so many years in.

Perhaps it should be exposed as a separate rule instead? Doesn't need to mean copy-paste of code, probably 95% reused in a lib.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also put EXPERIMENTAL in the docs I guess, but I don't want this to be considered a stable public API at this point. I guess I was partially just following the jsc.experimental.emitIsolatedDts swc property 🤷

@@ -66,6 +66,10 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#data for more
"root_dir": attr.string(
doc = "a subdirectory under the input package which should be consider the root directory of all the input files",
),
"experimental_emit_isolated_dts": attr.bool(
doc = "Emit .d.ts files for TypeScript sources",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add "instead of .js outputs" ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please link to SWC docs or discussion threads about the feature to save the reader some effort in researching this new capability

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There really is no documentation at the moment other then the commit in the changelog: https://github.com/swc-project/swc/blob/main/CHANGELOG.md#164---2024-06-22

@@ -248,6 +278,15 @@ def _swc_impl(ctx):
inputs.extend(ctx.files.plugins)
args.add_all(plugin_args)

if ctx.attr.experimental_emit_isolated_dts:
args.add_all(["--config-json", json.encode({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you tested that --config-file foo --config-json '{"some": "object"}' has the desired behavior of overriding the file?

What happens if the user also gives a --config-json in the args they provide?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also would be good to comment if there's an ordering constraint between these two flags

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are a few things that I think need more testing, both here and our other --config-json cases (see plugins).

Note that we could also go the route where if experimental_emit_isolated_dts = True then we assume you set this emitIsolatedDts flag yourself, then maybe add some "validation" in the future like tsc does.

IMO if --config-json merges+overwrites the existing config then I prefer this instead of requiring validation, but both have trade-offs.

@jbedard
Copy link
Member Author

jbedard commented Aug 29, 2024

I think we're blocked for now on swc-project/swc#9512 if I'm understanding correctly. The way we invoke the cli using --out-file doesn't work with the swc cli atm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants