Skip to content

Commit 825749f

Browse files
authored
feat(es): Integrate experimental data API (#9027)
1 parent f1ca6a7 commit 825749f

File tree

7 files changed

+40
-16
lines changed

7 files changed

+40
-16
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/swc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ swc_node_comments = { version = "0.20.20", path = "../swc_node_comments" }
106106
swc_plugin_proxy = { version = "0.42.1", path = "../swc_plugin_proxy", optional = true }
107107
swc_plugin_runner = { version = "0.107.1", path = "../swc_plugin_runner", optional = true, default-features = false }
108108
swc_timer = { version = "0.21.22", path = "../swc_timer" }
109+
swc_transform_common = { version = "0.1.1", path = "../swc_transform_common" }
109110
swc_visit = { version = "0.5.14", path = "../swc_visit" }
110111

111112
[dependencies.tokio]

crates/swc/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ impl Compiler {
915915
.with_emit_assert_for_import_attributes(
916916
opts.format.emit_assert_for_import_attributes,
917917
),
918+
output: None,
918919
},
919920
)
920921
})
@@ -966,10 +967,12 @@ impl Compiler {
966967
};
967968

968969
let mut pass = config.pass;
969-
let program = helpers::HELPERS.set(&Helpers::new(config.external_helpers), || {
970-
HANDLER.set(handler, || {
971-
// Fold module
972-
program.fold_with(&mut pass)
970+
let (program, output) = swc_transform_common::output::capture(|| {
971+
helpers::HELPERS.set(&Helpers::new(config.external_helpers), || {
972+
HANDLER.set(handler, || {
973+
// Fold module
974+
program.fold_with(&mut pass)
975+
})
973976
})
974977
});
975978

@@ -1003,6 +1006,7 @@ impl Compiler {
10031006
.with_emit_assert_for_import_attributes(
10041007
config.emit_assert_for_import_attributes,
10051008
),
1009+
output: Some(output),
10061010
},
10071011
)
10081012
})

crates/swc/tests/projects.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,15 +757,14 @@ fn should_visit() {
757757
output_path: config.output_path,
758758
inline_sources_content: config.inline_sources_content,
759759
source_map: config.source_maps,
760-
source_map_names: &Default::default(),
761760
orig: None,
762761
// TODO: figure out sourcemaps
763762
comments: Some(&comments),
764763
emit_source_map_columns: config.emit_source_map_columns,
765-
preamble: Default::default(),
766764
codegen_config: swc_ecma_codegen::Config::default()
767765
.with_target(config.target)
768766
.with_minify(config.minify),
767+
..Default::default()
769768
},
770769
)
771770
.unwrap()

crates/swc_compiler_base/Cargo.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ version = "0.10.0"
1313
node = ["napi", "napi-derive"]
1414

1515
[dependencies]
16-
anyhow = { workspace = true }
17-
base64 = { workspace = true }
18-
once_cell = { workspace = true }
19-
pathdiff = { workspace = true }
20-
serde = { workspace = true, features = ["derive"] }
21-
sourcemap = { workspace = true }
16+
anyhow = { workspace = true }
17+
base64 = { workspace = true }
18+
once_cell = { workspace = true }
19+
pathdiff = { workspace = true }
20+
rustc-hash = { workspace = true }
21+
serde = { workspace = true, features = ["derive"] }
22+
serde_json = { workspace = true }
23+
sourcemap = { workspace = true }
2224

2325
swc_atoms = { version = "0.6.5", path = "../swc_atoms" }
2426
swc_common = { version = "0.33.26", path = "../swc_common", features = [

crates/swc_compiler_base/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::{
66
use anyhow::{Context, Error};
77
use base64::prelude::{Engine, BASE64_STANDARD};
88
use once_cell::sync::Lazy;
9+
use rustc_hash::FxHashMap;
10+
#[allow(unused)]
911
use serde::{Deserialize, Serialize};
1012
use swc_atoms::JsWord;
1113
use swc_common::{
@@ -32,6 +34,9 @@ pub struct TransformOutput {
3234
pub code: String,
3335
#[serde(skip_serializing_if = "Option::is_none")]
3436
pub map: Option<String>,
37+
38+
#[serde(skip_serializing_if = "Option::is_none")]
39+
pub output: Option<String>,
3540
}
3641

3742
#[cfg(not(feature = "node"))]
@@ -40,6 +45,9 @@ pub struct TransformOutput {
4045
pub code: String,
4146
#[serde(skip_serializing_if = "Option::is_none")]
4247
pub map: Option<String>,
48+
49+
#[serde(skip_serializing_if = "Option::is_none")]
50+
pub output: Option<String>,
4351
}
4452

4553
/// This method parses a javascript / typescript file
@@ -106,6 +114,7 @@ pub struct PrintArgs<'a> {
106114
pub emit_source_map_columns: bool,
107115
pub preamble: &'a str,
108116
pub codegen_config: swc_ecma_codegen::Config,
117+
pub output: Option<FxHashMap<String, serde_json::Value>>,
109118
}
110119

111120
impl Default for PrintArgs<'_> {
@@ -124,6 +133,7 @@ impl Default for PrintArgs<'_> {
124133
emit_source_map_columns: false,
125134
preamble: "",
126135
codegen_config: Default::default(),
136+
output: None,
127137
}
128138
}
129139
}
@@ -153,6 +163,7 @@ pub fn print<T>(
153163
emit_source_map_columns,
154164
preamble,
155165
codegen_config,
166+
output,
156167
}: PrintArgs,
157168
) -> Result<TransformOutput, Error>
158169
where
@@ -256,7 +267,11 @@ where
256267
}
257268
};
258269

259-
Ok(TransformOutput { code, map })
270+
Ok(TransformOutput {
271+
code,
272+
map,
273+
output: Some(serde_json::to_string(&output).context("failed to serilaize output")?),
274+
})
260275
}
261276

262277
struct SwcSourceMapConfig<'a> {

crates/swc_transform_common/src/output.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use better_scoped_tls::scoped_tls;
88
use rustc_hash::FxHashMap;
99
use serde_json::Value;
1010

11-
scoped_tls!(static OUTPUT: RefCell<FxHashMap<String, Value>>);
11+
scoped_tls!(static OUTPUT: RefCell<FxHashMap<String, serde_json::Value>>);
1212

1313
/// (Experimental) Captures output.
1414
///
1515
/// This is not stable and may be removed in the future.
16-
pub fn capture<Ret>(f: impl FnOnce() -> Ret) -> (Ret, FxHashMap<String, Value>) {
17-
let output = RefCell::new(FxHashMap::default());
16+
pub fn capture<Ret>(f: impl FnOnce() -> Ret) -> (Ret, FxHashMap<String, serde_json::Value>) {
17+
let output = RefCell::new(Default::default());
1818

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

0 commit comments

Comments
 (0)