Skip to content

Commit

Permalink
feat(es/transforms): Add an API for returning metadata to JS (#9022)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jun 5, 2024
1 parent 2b81d72 commit 6ce112c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/swc_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ version = "0.92.11"
"ecma_loader",
"ecma_ast",
"trace_macro",
"transform_common",
"plugin_transform",
]
rustdoc-args = ["--cfg", "docsrs"]
Expand All @@ -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"]
Expand Down Expand Up @@ -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 }
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))]
Expand Down
10 changes: 10 additions & 0 deletions crates/swc_transform_common/src/output.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());

Expand All @@ -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);
Expand Down

0 comments on commit 6ce112c

Please sign in to comment.