Skip to content

perf(turbopack): Decode sourcemap partially #80177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jun 6, 2025
Merged
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,5 @@ vergen = { version = "9.0.6", features = ["cargo"] }
vergen-gitcl = { version = "1.0.8", features = ["cargo"] }
webbrowser = "0.8.7"

[patch.crates-io]
sourcemap = { git = "https://github.com/kdy1/rust-sourcemap", branch = "main" }
3 changes: 2 additions & 1 deletion turbopack/crates/turbopack-ecmascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ petgraph = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_json = { workspace = true, features = ["raw_value"] }
sourcemap = { workspace = true }
smallvec = { workspace = true }
strsim = { workspace = true }
Expand Down Expand Up @@ -71,6 +71,7 @@ swc_core = { workspace = true, features = [
"testing",
"base",
] }
bitvec = "1.0.1"

[dev-dependencies]
criterion = { workspace = true, features = ["async_tokio"] }
Expand Down
43 changes: 25 additions & 18 deletions turbopack/crates/turbopack-ecmascript/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,14 @@ pub fn generate_js_source_map(
original_source_map: Option<&Rope>,
inline_sources_content: bool,
) -> Result<Rope> {
let input_map = if let Some(original_source_map) = original_source_map {
Some(match sourcemap::decode(original_source_map.read())? {
sourcemap::DecodedMap::Regular(source_map) => source_map,
// swc only accepts flattened sourcemaps as input
sourcemap::DecodedMap::Index(source_map_index) => source_map_index.flatten()?,
_ => return Err(sourcemap::Error::IncompatibleSourceMap.into()),
})
let original_source_map = original_source_map.map(|x| x.to_bytes());
let input_map = if let Some(original_source_map) = &original_source_map {
Some(sourcemap::lazy::decode(original_source_map)?.into_source_map()?)
} else {
None
};

let map = files_map.build_source_map(
let new_mappings = files_map.build_source_map(
&mappings,
None,
InlineSourcesContentConfig {
Expand All @@ -130,18 +126,29 @@ pub fn generate_js_source_map(
},
);

let mut map = match input_map {
Some(mut input_map) => {
input_map.adjust_mappings(&map);
input_map
match input_map {
Some(mut map) => {
// TODO: Make this more efficient
map.adjust_mappings(new_mappings);

// TODO: Enable this when we have a way to handle the ignore list
// add_default_ignore_list(&mut map);
let map = map.into_raw_sourcemap();
let result = serde_json::to_vec(&map)?;
Ok(Rope::from(result))
}
None => map,
};
add_default_ignore_list(&mut map);
None => {
// We don't convert sourcemap::SourceMap into raw_sourcemap::SourceMap because we don't
// need to adjust mappings
let mut map = new_mappings;

add_default_ignore_list(&mut map);

let mut result = vec![];
map.to_writer(&mut result)?;
Ok(Rope::from(result))
let mut result = vec![];
map.to_writer(&mut result)?;
Ok(Rope::from(result))
}
}
}

/// A config to generate a source map which includes the source content of every
Expand Down
Loading