swc-project / swc

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

Getting plugin runtime environment variables within the swc plugin #9668

Open zhangtao25 opened 1 month ago

zhangtao25 commented 1 month ago

Describe the feature

We have developed a swc plugin that expects to detect the environment variables of the current ci environment when the plugin is running, but the swc plugin as of now only accepts explicitly passed parameters from outside, and we want to implement this directly inside the plugin.

Babel plugin or link to the feature description

https://github.com/canyon-project/canyon/blob/main/plugins/babel-plugin-canyon/src/index.js#L24

Additional context

No response

kdy1 commented 1 month ago

cc @kwonoj Any opinion about this?

zhangtao25 commented 1 month ago

Currently, with TransformPluginMetadataContextKind, I can only retrieve the NODE_ENV from Node.js.

kdy1 commented 1 month ago

We should allow env::var I guess, but I want to hear opinions of the core team about this. @swc-project/core-es

magic-akari commented 1 month ago

I know that the current WASM version of SWC doesn't support plugins. However, if this feature is added, would it complicate the implementation of WASM SWC with plugins?

kdy1 commented 1 month ago

I think it's only about allowing env::var from Wasm plugins, with wasm32-wasi runtime target.

kwonoj commented 1 month ago

I recall whole env varables for the process could be quite verbose and passing it all would cause some overload.

If we have to pass something, I'd consider to have sort of allowlist can be configured and pass it through metadatacontext.

kdy1 commented 1 month ago

We don’t need to explicitly pass them I think, I think we can support env::var for Wasi

kwonoj commented 1 month ago

Isn't env::var eventually need to be serialized / deserialized its values into wasm's memory space? that's the one I'm worrying about. It is easily possible to see some proc have more than 20-30 vars or even ~100 depends on the usecase.

kdy1 commented 1 month ago

Oops. I expected wasi to have a feature for it, but well... https://docs.rs/wasmer-wasix/latest/wasmer_wasix/capabilities/struct.Capabilities.html There's no capability to allow fetching the environment variables from os

syrusakbary commented 1 month ago

There should be @kdy1 , I believe we support this on the wasmer-cli via --forward-host-env, so it should be doable to have it on the WASIX api. If you want, feel free to open an issue in the Wasmer repo and we can follow-up there