feat(es/transforms): Add experimental output API for transforms (#9000)

**Description:**

This interface will provide a way to emit some outputs from transforms. This data will be emitted back to the JS caller.
This commit is contained in:
Donny/강동윤 2024-06-05 15:38:28 +09:00 committed by GitHub
parent 668a11d50d
commit 6fa79be081
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 106 additions and 35 deletions

View File

@ -551,12 +551,18 @@ jobs:
- crate: swc_trace_macro - crate: swc_trace_macro
os: ubuntu-latest os: ubuntu-latest
runner: ubuntu-latest runner: ubuntu-latest
- crate: swc_transform_common
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_visit - crate: swc_visit
os: ubuntu-latest os: ubuntu-latest
runner: ubuntu-latest runner: ubuntu-latest
- crate: swc_visit_macros - crate: swc_visit_macros
os: ubuntu-latest os: ubuntu-latest
runner: ubuntu-latest runner: ubuntu-latest
- crate: swc_x_optimizer
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_xml - crate: swc_xml
os: ubuntu-latest os: ubuntu-latest
runner: ubuntu-latest runner: ubuntu-latest

11
Cargo.lock generated
View File

@ -5281,6 +5281,17 @@ dependencies = [
"syn 2.0.55", "syn 2.0.55",
] ]
[[package]]
name = "swc_transform_common"
version = "0.1.0"
dependencies = [
"better_scoped_tls",
"once_cell",
"rustc-hash",
"serde",
"serde_json",
]
[[package]] [[package]]
name = "swc_visit" name = "swc_visit"
version = "0.5.14" version = "0.5.14"

View File

@ -22,9 +22,13 @@ members = [
"crates/swc_timer", "crates/swc_timer",
"crates/swc_xml", "crates/swc_xml",
"crates/swc_x_optimizer", "crates/swc_x_optimizer",
"crates/swc_transform_common",
] ]
resolver = "2" resolver = "2"
[workspace.package]
repository = "https://github.com/swc-project/swc.git"
[workspace.dependencies] [workspace.dependencies]
# bytecheck version should be in sync with rkyv version. Do not bump individually. # bytecheck version should be in sync with rkyv version. Do not bump individually.
bytecheck = "0.6.10" bytecheck = "0.6.10"

View File

@ -1,11 +1,11 @@
[package] [package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"] authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Debug utilities" description = "Debug utilities"
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
name = "dbg-swc" name = "dbg-swc"
repository = "https://github.com/kdy1/dbg-swc.git" repository.workspace = true
version = "0.93.5" version = "0.93.5"
[[bin]] [[bin]]
bench = false bench = false

View File

@ -1,12 +1,13 @@
[package] [package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"] authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "JsDoc parser writen in rust" description = "JsDoc parser writen in rust"
documentation = "https://rustdoc.swc.rs/jsdoc/" documentation = "https://rustdoc.swc.rs/jsdoc/"
edition = "2021" edition = "2021"
include = ["Cargo.toml", "src/**/*.rs"] include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0" license = "Apache-2.0"
name = "jsdoc" name = "jsdoc"
version = "0.113.1" repository.workspace = true
version = "0.113.1"
[lib] [lib]
bench = false bench = false

View File

@ -1,11 +1,12 @@
[package] [package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"] authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Common logic for targetting vairous browsers" description = "Common logic for targetting vairous browsers"
documentation = "https://rustdoc.swc.rs/preset_env_base/" documentation = "https://rustdoc.swc.rs/preset_env_base/"
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
name = "preset_env_base" name = "preset_env_base"
version = "0.4.13" repository.workspace = true
version = "0.4.13"
[lib] [lib]
bench = false bench = false

View File

@ -1,13 +1,13 @@
[package] [package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"] authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "preset-env for the swc" description = "preset-env for the swc"
documentation = "https://rustdoc.swc.rs/swc_ecma_preset_env/" documentation = "https://rustdoc.swc.rs/swc_ecma_preset_env/"
edition = "2021" edition = "2021"
include = ["Cargo.toml", "src/**/*.rs", "src/**/*.json", "data/**/*.json"] include = ["Cargo.toml", "src/**/*.rs", "src/**/*.json", "data/**/*.json"]
license = "Apache-2.0" license = "Apache-2.0"
name = "swc_ecma_preset_env" name = "swc_ecma_preset_env"
repository = "https://github.com/swc-project/swc" repository.workspace = true
version = "0.207.1" version = "0.207.1"
[lib] [lib]
bench = false bench = false

View File

@ -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" }

View File

@ -0,0 +1,3 @@
# swc_transform_common
Common utilities for the author of a swc transform.

View File

@ -0,0 +1 @@
pub mod output;

View File

@ -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:?}");
}
});
}

View File

@ -6,10 +6,8 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
function prepend() { while read line; do echo "${1}${line}"; done; } function prepend() { while read line; do echo "${1}${line}"; done; }
crates=$(\ crates=$(\
cargo metadata --format-version 1 \ cargo metadata --format-version=1 --all-features \
| jq -r '.workspace_members[]' \ | jq '.packages .[] | select(.repository == "https://github.com/swc-project/swc.git") | .name' -r\
| cut -f1 -d" " \
| sort \
) )
# yq query syntax is weird, so we have to use jq # yq query syntax is weird, so we have to use jq