Skip to content

Commit

Permalink
feat(es/transforms): Add experimental output API for transforms (#9000)
Browse files Browse the repository at this point in the history
**Description:**

This interface will provide a way to emit some outputs from transforms. This data will be emitted back to the JS caller.
  • Loading branch information
kdy1 committed Jun 5, 2024
1 parent 668a11d commit 6fa79be
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,18 @@ jobs:
- crate: swc_trace_macro
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_transform_common
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_visit
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_visit_macros
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_x_optimizer
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_xml
os: ubuntu-latest
runner: ubuntu-latest
Expand Down
11 changes: 11 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ members = [
"crates/swc_timer",
"crates/swc_xml",
"crates/swc_x_optimizer",
"crates/swc_transform_common",
]
resolver = "2"

[workspace.package]
repository = "https://github.com/swc-project/swc.git"

[workspace.dependencies]
# bytecheck version should be in sync with rkyv version. Do not bump individually.
bytecheck = "0.6.10"
Expand Down
14 changes: 7 additions & 7 deletions crates/dbg-swc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Debug utilities"
edition = "2021"
license = "Apache-2.0"
name = "dbg-swc"
repository = "https://github.com/kdy1/dbg-swc.git"
version = "0.93.5"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Debug utilities"
edition = "2021"
license = "Apache-2.0"
name = "dbg-swc"
repository.workspace = true
version = "0.93.5"

[[bin]]
bench = false
Expand Down
17 changes: 9 additions & 8 deletions crates/jsdoc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "JsDoc parser writen in rust"
documentation = "https://rustdoc.swc.rs/jsdoc/"
edition = "2021"
include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0"
name = "jsdoc"
version = "0.113.1"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "JsDoc parser writen in rust"
documentation = "https://rustdoc.swc.rs/jsdoc/"
edition = "2021"
include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0"
name = "jsdoc"
repository.workspace = true
version = "0.113.1"

[lib]
bench = false
Expand Down
15 changes: 8 additions & 7 deletions crates/preset_env_base/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Common logic for targetting vairous browsers"
documentation = "https://rustdoc.swc.rs/preset_env_base/"
edition = "2021"
license = "Apache-2.0"
name = "preset_env_base"
version = "0.4.13"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Common logic for targetting vairous browsers"
documentation = "https://rustdoc.swc.rs/preset_env_base/"
edition = "2021"
license = "Apache-2.0"
name = "preset_env_base"
repository.workspace = true
version = "0.4.13"

[lib]
bench = false
Expand Down
18 changes: 9 additions & 9 deletions crates/swc_ecma_preset_env/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "preset-env for the swc"
documentation = "https://rustdoc.swc.rs/swc_ecma_preset_env/"
edition = "2021"
include = ["Cargo.toml", "src/**/*.rs", "src/**/*.json", "data/**/*.json"]
license = "Apache-2.0"
name = "swc_ecma_preset_env"
repository = "https://github.com/swc-project/swc"
version = "0.207.1"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "preset-env for the swc"
documentation = "https://rustdoc.swc.rs/swc_ecma_preset_env/"
edition = "2021"
include = ["Cargo.toml", "src/**/*.rs", "src/**/*.json", "data/**/*.json"]
license = "Apache-2.0"
name = "swc_ecma_preset_env"
repository.workspace = true
version = "0.207.1"

[lib]
bench = false
Expand Down
21 changes: 21 additions & 0 deletions crates/swc_transform_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Common utilities for swc transforms for any language"
documentation = "https://rustdoc.swc.rs/swc_transform_common/"
edition = "2021"
license = "Apache-2.0"
name = "swc_transform_common"
repository = "https://github.com/swc-project/swc.git"
version = "0.1.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
once_cell = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

better_scoped_tls = { version = "0.1.1", path = "../better_scoped_tls" }
3 changes: 3 additions & 0 deletions crates/swc_transform_common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# swc_transform_common

Common utilities for the author of a swc transform.
1 change: 1 addition & 0 deletions crates/swc_transform_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod output;
25 changes: 25 additions & 0 deletions crates/swc_transform_common/src/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::cell::RefCell;

use better_scoped_tls::scoped_tls;
use rustc_hash::FxHashMap;
use serde_json::Value;

scoped_tls!(static OUTPUT: RefCell<FxHashMap<String, Value>>);

pub fn capture<Ret>(f: impl FnOnce() -> Ret) -> (Ret, FxHashMap<String, Value>) {
let output = RefCell::new(FxHashMap::default());

let ret = OUTPUT.set(&output, f);

(ret, output.into_inner())
}

pub fn emit(key: String, value: Value) {
OUTPUT.with(|output| {
let previous = output.borrow_mut().insert(key, value);

if let Some(previous) = previous {
panic!("Key already set. Previous value: {previous:?}");
}
});
}
6 changes: 2 additions & 4 deletions scripts/github/create-matrix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
function prepend() { while read line; do echo "${1}${line}"; done; }

crates=$(\
cargo metadata --format-version 1 \
| jq -r '.workspace_members[]' \
| cut -f1 -d" " \
| sort \
cargo metadata --format-version=1 --all-features \
| jq '.packages .[] | select(.repository == "https://github.com/swc-project/swc.git") | .name' -r\
)

# yq query syntax is weird, so we have to use jq
Expand Down

0 comments on commit 6fa79be

Please sign in to comment.