swc icon indicating copy to clipboard operation
swc copied to clipboard

API to collect Extra Data from Wasm plugins and custom transforms

Open kdy1 opened this issue 1 year ago • 1 comments

Describe the feature

I think it is worth the effort. I'll work on this.

It will return the collect Extra Data emitted by custom plugins and Wasm plugins as an array or a hashmap, to the caller.

Additional context

No response

kdy1 avatar Sep 19 '24 06:09 kdy1

cc @dferber90 for visibility

kdy1 avatar Sep 19 '24 06:09 kdy1

Would this make it possible to use an SWC plugin to extract metadata about a file? If so that would be incredibly useful.

To give one concrete example:

We're currently using @swc/plugin-formatjs as part of our internationalization system. The SWC plugin injects message ids and drops message descriptions during transpilation from TS -> JS. We end up having to re-process each TS file a second time to extract metadata about the messages in the file for translation. Unfortunately this uses @formatjs/ts-transformer under the hood which is significantly slower and potentially even inconsistent with @swc/plugin-formatjs.

I would love to be able to do this second pass using SWC + @swc/plugin-formatjs. Or even better to get rid of the second pass entirely and somehow output extracted metadata alongside the transpiled JS (maybe tangentially related to https://github.com/swc-project/swc/discussions/4997?)

walkerburgin avatar Mar 05 '25 17:03 walkerburgin

Yeah it would be possible but actually this is already possible using file system

kdy1 avatar Mar 06 '25 14:03 kdy1

Do wasm plugins already have enough information/access to write a file that's a sibling to the transpilation output file? Ie something like foo.ts -> foo.js + foo.js.meta.json? I've poked around a little but haven't been able to find the output file location from the plugin. Sorry for getting a little off topic from the original issue 😬

walkerburgin avatar Mar 06 '25 16:03 walkerburgin

IIRC /cwd would work. Can you confirm, @kwonoj ?

kdy1 avatar Mar 06 '25 16:03 kdy1

Yes, cwd mount is still supported.

Ie something like foo.ts -> foo.js + foo.js.meta.json

Given swc transform can run parallel and does not have shared context in between each transform, there is no way to collect whole transforms meta.

kwonoj avatar Mar 06 '25 16:03 kwonoj

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

swc-bot avatar Apr 30 '25 12:04 swc-bot