Unverified Commit f20f4548 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

turbopack: avoid reading the whole filesystem in source map rewriting

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 2598c776
...@@ -144,7 +144,6 @@ describe('turbopack-portable-build-cache', () => { ...@@ -144,7 +144,6 @@ describe('turbopack-portable-build-cache', () => {
"<SourceMapReference as GenerateSourceMap>::generate_source_map", "<SourceMapReference as GenerateSourceMap>::generate_source_map",
"AssetContent::hash", "AssetContent::hash",
"DiskFileSystem::new_internal", "DiskFileSystem::new_internal",
"EcmascriptChunkItemContent::new",
"EcmascriptClientReferenceModule::proxy_module", "EcmascriptClientReferenceModule::proxy_module",
"EcmascriptModuleContent::new", "EcmascriptModuleContent::new",
"EcmascriptModuleContent::new_merged", "EcmascriptModuleContent::new_merged",
...@@ -270,7 +269,6 @@ describe('turbopack-portable-build-cache', () => { ...@@ -270,7 +269,6 @@ describe('turbopack-portable-build-cache', () => {
"inner_entrypoints_without_collectibles_operation", "inner_entrypoints_without_collectibles_operation",
"lint_transform_plugin", "lint_transform_plugin",
"modularize_imports_transform_plugin", "modularize_imports_transform_plugin",
"module_factory_with_code_generation_issue",
"next_cjs_optimizer_transform_plugin", "next_cjs_optimizer_transform_plugin",
"next_debug_instant_stack_transform_plugin", "next_debug_instant_stack_transform_plugin",
"next_dynamic_transform_plugin", "next_dynamic_transform_plugin",
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
//! identically to what `swc_sourcemap::SourceMap::to_writer` would have produced (pinned by the //! identically to what `swc_sourcemap::SourceMap::to_writer` would have produced (pinned by the
//! golden tests below). //! golden tests below).
use std::sync::LazyLock; use std::{borrow::Cow, sync::LazyLock};
use anyhow::Result; use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
...@@ -330,24 +330,41 @@ impl StructuredSourceMap { ...@@ -330,24 +330,41 @@ impl StructuredSourceMap {
Ok(result) Ok(result)
} }
/// Returns whether any `sources` entry satisfies `predicate`. Returns `false` if there are no
/// `sources` or they cannot be decoded (the same cases in which [`Self::rewrite_sources`]
/// leaves the map unchanged).
pub fn any_source(&self, mut predicate: impl FnMut(&str) -> bool) -> bool {
self.parsed_sources()
.is_some_and(|sources| sources.iter().flatten().any(|source| predicate(source)))
}
/// Decodes the `sources` field, or returns `None` if it is missing or cannot be decoded (e.g.
/// non-string entries in an external map).
fn parsed_sources(&self) -> Option<Cow<'_, [Option<String>]>> {
match self.sources.as_ref()? {
SourcesField::Rewritten(sources) => Some(Cow::Borrowed(sources)),
SourcesField::Raw(raw) => {
serde_json::from_slice::<Vec<Option<String>>>(&raw.to_bytes())
.ok()
.map(Cow::Owned)
}
}
}
/// Rewrites each `sources` entry, keeping every other field (including the shared /// Rewrites each `sources` entry, keeping every other field (including the shared
/// `sourcesContent` ropes) intact. `rewrite` returns `None` to leave an entry unchanged. /// `sourcesContent` ropes) intact. `rewrite` returns `None` to leave an entry unchanged.
/// ///
/// `sources` is only decoded if a rewrite actually changes an entry; if it cannot be decoded /// `sources` is only re-encoded if a rewrite actually changes an entry; if it cannot be decoded
/// (e.g. non-string entries in an external map) the map is returned unchanged. /// (e.g. non-string entries in an external map) the map is returned unchanged.
pub fn rewrite_sources( pub fn rewrite_sources(
&self, &self,
mut rewrite: impl FnMut(&str) -> Result<Option<String>>, mut rewrite: impl FnMut(&str) -> Result<Option<String>>,
) -> Result<Self> { ) -> Result<Self> {
let mut result = self.clone(); let mut result = self.clone();
let mut sources: Vec<Option<String>> = match &self.sources { let Some(sources) = self.parsed_sources() else {
None => return Ok(result), return Ok(result);
Some(SourcesField::Rewritten(sources)) => sources.clone(),
Some(SourcesField::Raw(raw)) => match serde_json::from_slice(&raw.to_bytes()) {
Ok(sources) => sources,
Err(_) => return Ok(result),
},
}; };
let mut sources = sources.into_owned();
let mut changed = false; let mut changed = false;
for source in sources.iter_mut().flatten() { for source in sources.iter_mut().flatten() {
if let Some(new_source) = rewrite(source)? { if let Some(new_source) = rewrite(source)? {
...@@ -610,6 +627,18 @@ mod tests { ...@@ -610,6 +627,18 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn any_source() -> Result<()> {
let map = |json: &'static str| StructuredSourceMap::from_json(&Rope::from(json));
let is_a = |source: &str| source == "a.js";
assert!(map(r#"{"version":3,"sources":[null,"a.js"],"mappings":""}"#)?.any_source(is_a));
assert!(!map(r#"{"version":3,"sources":["b.js"],"mappings":""}"#)?.any_source(is_a));
assert!(!map(r#"{"version":3,"mappings":""}"#)?.any_source(is_a));
// Undecodable `sources` are treated like missing ones.
assert!(!map(r#"{"version":3,"sources":[1,"a.js"],"mappings":""}"#)?.any_source(is_a));
Ok(())
}
#[test] #[test]
fn rewrite_sources_keeps_contents_shared() -> Result<()> { fn rewrite_sources_keeps_contents_shared() -> Result<()> {
let input = r#"{"version":3,"sources":["turbopack:///[project]/a.js"],"sourcesContent":["text"],"mappings":"AAAA"}"#; let input = r#"{"version":3,"sources":["turbopack:///[project]/a.js"],"sourcesContent":["text"],"mappings":"AAAA"}"#;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment