Skip to content

Commit

Permalink
feat(es): Integrate experimental data API (#9027)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jun 7, 2024
1 parent f1ca6a7 commit 825749f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ swc_node_comments = { version = "0.20.20", path = "../swc_node_comments" }
swc_plugin_proxy = { version = "0.42.1", path = "../swc_plugin_proxy", optional = true }
swc_plugin_runner = { version = "0.107.1", path = "../swc_plugin_runner", optional = true, default-features = false }
swc_timer = { version = "0.21.22", path = "../swc_timer" }
swc_transform_common = { version = "0.1.1", path = "../swc_transform_common" }
swc_visit = { version = "0.5.14", path = "../swc_visit" }

[dependencies.tokio]
Expand Down
12 changes: 8 additions & 4 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ impl Compiler {
.with_emit_assert_for_import_attributes(
opts.format.emit_assert_for_import_attributes,
),
output: None,
},
)
})
Expand Down Expand Up @@ -966,10 +967,12 @@ impl Compiler {
};

let mut pass = config.pass;
let program = helpers::HELPERS.set(&Helpers::new(config.external_helpers), || {
HANDLER.set(handler, || {
// Fold module
program.fold_with(&mut pass)
let (program, output) = swc_transform_common::output::capture(|| {
helpers::HELPERS.set(&Helpers::new(config.external_helpers), || {
HANDLER.set(handler, || {
// Fold module
program.fold_with(&mut pass)
})
})
});

Expand Down Expand Up @@ -1003,6 +1006,7 @@ impl Compiler {
.with_emit_assert_for_import_attributes(
config.emit_assert_for_import_attributes,
),
output: Some(output),
},
)
})
Expand Down
3 changes: 1 addition & 2 deletions crates/swc/tests/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,15 +757,14 @@ fn should_visit() {
output_path: config.output_path,
inline_sources_content: config.inline_sources_content,
source_map: config.source_maps,
source_map_names: &Default::default(),
orig: None,
// TODO: figure out sourcemaps
comments: Some(&comments),
emit_source_map_columns: config.emit_source_map_columns,
preamble: Default::default(),
codegen_config: swc_ecma_codegen::Config::default()
.with_target(config.target)
.with_minify(config.minify),
..Default::default()
},
)
.unwrap()
Expand Down
14 changes: 8 additions & 6 deletions crates/swc_compiler_base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ version = "0.10.0"
node = ["napi", "napi-derive"]

[dependencies]
anyhow = { workspace = true }
base64 = { workspace = true }
once_cell = { workspace = true }
pathdiff = { workspace = true }
serde = { workspace = true, features = ["derive"] }
sourcemap = { workspace = true }
anyhow = { workspace = true }
base64 = { workspace = true }
once_cell = { workspace = true }
pathdiff = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sourcemap = { workspace = true }

swc_atoms = { version = "0.6.5", path = "../swc_atoms" }
swc_common = { version = "0.33.26", path = "../swc_common", features = [
Expand Down
17 changes: 16 additions & 1 deletion crates/swc_compiler_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
use anyhow::{Context, Error};
use base64::prelude::{Engine, BASE64_STANDARD};
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
#[allow(unused)]
use serde::{Deserialize, Serialize};
use swc_atoms::JsWord;
use swc_common::{
Expand All @@ -32,6 +34,9 @@ pub struct TransformOutput {
pub code: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub map: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub output: Option<String>,
}

#[cfg(not(feature = "node"))]
Expand All @@ -40,6 +45,9 @@ pub struct TransformOutput {
pub code: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub map: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub output: Option<String>,
}

/// This method parses a javascript / typescript file
Expand Down Expand Up @@ -106,6 +114,7 @@ pub struct PrintArgs<'a> {
pub emit_source_map_columns: bool,
pub preamble: &'a str,
pub codegen_config: swc_ecma_codegen::Config,
pub output: Option<FxHashMap<String, serde_json::Value>>,
}

impl Default for PrintArgs<'_> {
Expand All @@ -124,6 +133,7 @@ impl Default for PrintArgs<'_> {
emit_source_map_columns: false,
preamble: "",
codegen_config: Default::default(),
output: None,
}
}
}
Expand Down Expand Up @@ -153,6 +163,7 @@ pub fn print<T>(
emit_source_map_columns,
preamble,
codegen_config,
output,
}: PrintArgs,
) -> Result<TransformOutput, Error>
where
Expand Down Expand Up @@ -256,7 +267,11 @@ where
}
};

Ok(TransformOutput { code, map })
Ok(TransformOutput {
code,
map,
output: Some(serde_json::to_string(&output).context("failed to serilaize output")?),
})
}

struct SwcSourceMapConfig<'a> {
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_transform_common/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use better_scoped_tls::scoped_tls;
use rustc_hash::FxHashMap;
use serde_json::Value;

scoped_tls!(static OUTPUT: RefCell<FxHashMap<String, Value>>);
scoped_tls!(static OUTPUT: RefCell<FxHashMap<String, serde_json::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());
pub fn capture<Ret>(f: impl FnOnce() -> Ret) -> (Ret, FxHashMap<String, serde_json::Value>) {
let output = RefCell::new(Default::default());

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

Expand Down

0 comments on commit 825749f

Please sign in to comment.