feat(es/transforms): Add an API for returning metadata to JS (#9022)

This commit is contained in:
Donny/강동윤 2024-06-05 19:37:28 +09:00 committed by GitHub
parent 2b81d72333
commit 6ce112cfeb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 0 deletions

1
Cargo.lock generated
View File

@ -3982,6 +3982,7 @@ dependencies = [
"swc_plugin_proxy",
"swc_plugin_runner",
"swc_trace_macro",
"swc_transform_common",
"testing",
"vergen",
]

View File

@ -21,6 +21,7 @@ version = "0.92.11"
"ecma_loader",
"ecma_ast",
"trace_macro",
"transform_common",
"plugin_transform",
]
rustdoc-args = ["--cfg", "docsrs"]
@ -40,6 +41,8 @@ doctest = false
# swc_common/ahash
common_ahash = ["swc_common/ahash"]
transform_common = []
# swc_ecma_loader/cache*
ecma_loader_lru = ["swc_ecma_loader/lru"]
ecma_loader_parking_lot = ["swc_ecma_loader/parking_lot"]
@ -371,6 +374,7 @@ swc_plugin = { optional = true, version = "0.90.0", path =
swc_plugin_macro = { optional = true, version = "0.9.16", path = "../swc_plugin_macro" }
swc_plugin_proxy = { optional = true, version = "0.42.1", path = "../swc_plugin_proxy" }
swc_trace_macro = { optional = true, version = "0.1.3", path = "../swc_trace_macro" }
swc_transform_common = { optional = true, version = "0.1.0", path = "../swc_transform_common" }
testing = { optional = true, version = "0.35.24", path = "../testing" }
# TODO: eventually swc_plugin_runner needs to remove default features
swc_plugin_runner = { optional = true, version = "0.107.1", path = "../swc_plugin_runner", default-features = false }

View File

@ -184,6 +184,12 @@ pub mod trace_macro {
pub use swc_trace_macro::*;
}
#[cfg(feature = "transform_common")]
#[cfg_attr(docsrs, doc(cfg(feature = "transform_common")))]
pub mod transform_common {
pub use swc_transform_common::*;
}
// swc_bundler
#[cfg(feature = "__bundler")]
#[cfg_attr(docsrs, doc(cfg(feature = "__bundler")))]

View File

@ -1,3 +1,7 @@
//! (Experimental) Output capturing.
//!
//! This module provides a way to emit metadata to the JS caller.
use std::cell::RefCell;
use better_scoped_tls::scoped_tls;
@ -6,6 +10,9 @@ use serde_json::Value;
scoped_tls!(static OUTPUT: RefCell<FxHashMap<String, Value>>);
/// (Experimental) Captures output.
///
/// This is not stable and may be removed in the future.
pub fn capture<Ret>(f: impl FnOnce() -> Ret) -> (Ret, FxHashMap<String, Value>) {
let output = RefCell::new(FxHashMap::default());
@ -14,6 +21,9 @@ pub fn capture<Ret>(f: impl FnOnce() -> Ret) -> (Ret, FxHashMap<String, Value>)
(ret, output.into_inner())
}
/// (Experimental) Emits a value to the JS caller.
///
/// This is not stable and may be removed in the future.
pub fn emit(key: String, value: Value) {
OUTPUT.with(|output| {
let previous = output.borrow_mut().insert(key, value);