diff --git a/crates/oxc_sourcemap/src/encode.rs b/crates/oxc_sourcemap/src/encode.rs index 8feaad889..3ddd29c55 100644 --- a/crates/oxc_sourcemap/src/encode.rs +++ b/crates/oxc_sourcemap/src/encode.rs @@ -47,6 +47,12 @@ pub fn encode(sourcemap: &SourceMap) -> Result { .map_err(Error::from)?; buf.push_str("e_source_contents.join(",")); } + if let Some(x_google_ignore_list) = &sourcemap.x_google_ignore_list { + buf.push_str("],\"x_google_ignoreList\":["); + let x_google_ignore_list = + x_google_ignore_list.iter().map(ToString::to_string).collect::>(); + buf.push_str(&x_google_ignore_list.join(",")); + } buf.push_str("],\"mappings\":\""); buf.push_str(&serialize_sourcemap_mappings(sourcemap)); buf.push_str("\"}"); @@ -164,7 +170,7 @@ fn test_encode() { #[test] fn test_encode_escape_string() { // '\0' should be escaped. - let sm = SourceMap::new( + let mut sm = SourceMap::new( None, vec!["\0".into()], None, @@ -173,8 +179,9 @@ fn test_encode_escape_string() { vec![], None, ); + sm.set_x_google_ignore_list(vec![0]); assert_eq!( sm.to_json_string().unwrap(), - r#"{"version":3,"names":["\u0000"],"sources":["\u0000"],"sourcesContent":["\u0000"],"mappings":""}"# + r#"{"version":3,"names":["\u0000"],"sources":["\u0000"],"sourcesContent":["\u0000"],"x_google_ignoreList":[0],"mappings":""}"# ); } diff --git a/crates/oxc_sourcemap/src/sourcemap.rs b/crates/oxc_sourcemap/src/sourcemap.rs index 9e9797b04..652e39063 100644 --- a/crates/oxc_sourcemap/src/sourcemap.rs +++ b/crates/oxc_sourcemap/src/sourcemap.rs @@ -16,6 +16,10 @@ pub struct SourceMap { pub(crate) source_contents: Option>>, pub(crate) tokens: Vec, pub(crate) token_chunks: Option>, + /// Identifies third-party sources (such as framework code or bundler-generated code), allowing developers to avoid code that they don't want to see or step through, without having to configure this beforehand. + /// The `x_google_ignoreList` field refers to the `sources` array, and lists the indices of all the known third-party sources in that source map. + /// When parsing the source map, developer tools can use this to determine sections of the code that the browser loads and runs that could be automatically ignore-listed. + pub(crate) x_google_ignore_list: Option>, } #[allow(clippy::cast_possible_truncation)] @@ -29,7 +33,16 @@ impl SourceMap { tokens: Vec, token_chunks: Option>, ) -> Self { - Self { file, names, source_root, sources, source_contents, tokens, token_chunks } + Self { + file, + names, + source_root, + sources, + source_contents, + tokens, + token_chunks, + x_google_ignore_list: None, + } } /// Convert `SourceMap` to vlq sourcemap string. @@ -69,6 +82,11 @@ impl SourceMap { self.source_root.as_deref() } + /// Set `x_google_ignoreList`. + pub fn set_x_google_ignore_list(&mut self, x_google_ignore_list: Vec) { + self.x_google_ignore_list = Some(x_google_ignore_list); + } + pub fn get_names(&self) -> impl Iterator { self.names.iter().map(AsRef::as_ref) }