swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.13k stars 1.22k forks source link

Is there any way to make WASM Plugin output information to JS #8154

Open littleprincewdk opened 1 year ago

littleprincewdk commented 1 year ago

Describe the feature

I want to collect dependencies while transforming js to es5,with babel,i can write a plugin or just parse-traverse-transform,but with swc i will get error:

I tried, when code is long, swc transform api (js plugin too) will crash (old version like 1.3.0 won't). Then i find a issue: https://github.com/swc-project/swc/issues/6255#issuecomment-1603869565, it says transform(Module) will be removed. Ok, so i can only to use wasm plugin:https://swc.rs/docs/plugin/ecmascript/getting-started, but after i tried, i don't know how to output some information to js. Is ther some way,like: ### Babel plugin or link to the feature description _No response_ ### Additional context _No response_
kdy1 commented 1 year ago

Currently no but I definitely want to add such an API

zhaomenghuan commented 11 months ago

I also need such a feature, is there any progress on this issue now?

magic-akari commented 9 months ago

To support StyleX, this feature is also required.

kwonoj commented 9 months ago

This is not strictly limited to the plugin, since transform interface itself does not have any extension point other than codes and its source map. So hypothetically, this would be swc/core's feature for the any internal transform chain to return arbitrary data.

I am aware there are few plugins trying to do this, even including my work around coverage plugins but I am also not entirely convinced what interface would be best for the plugin & custom transforms. Specifically, unlike what babel does we can't allow duck-typeable object or functions from wasm to be bubbled up into js runtime, so either it could be very rough (stringified json type) or having another way to provide type-safe way with serialization. For my workaround in coverage plugin I ended up to use comment to include realizable object and to parse.

Side note, I've been tinkering around stylex and this is not the bigger blocker probably, like it have a custom forked version of babel/evaluate and also invoking stylex/shared runtime codes in the transform phase, which is much challenging to solve in swc as it doesn't have same evaluatable environments.

kwonoj commented 9 months ago

TL:DR I'm open to see RFCs on this topic can solve ergonomics + performances.