feat(sourcemap): add x_google_ignoreList (#2928)

This commit is contained in:
underfin 2024-04-09 20:55:04 +08:00 committed by GitHub
parent 5cb3991b67
commit 8662f4f613
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 3 deletions

View file

@ -47,6 +47,12 @@ pub fn encode(sourcemap: &SourceMap) -> Result<String> {
.map_err(Error::from)?; .map_err(Error::from)?;
buf.push_str(&quote_source_contents.join(",")); buf.push_str(&quote_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::<Vec<_>>();
buf.push_str(&x_google_ignore_list.join(","));
}
buf.push_str("],\"mappings\":\""); buf.push_str("],\"mappings\":\"");
buf.push_str(&serialize_sourcemap_mappings(sourcemap)); buf.push_str(&serialize_sourcemap_mappings(sourcemap));
buf.push_str("\"}"); buf.push_str("\"}");
@ -164,7 +170,7 @@ fn test_encode() {
#[test] #[test]
fn test_encode_escape_string() { fn test_encode_escape_string() {
// '\0' should be escaped. // '\0' should be escaped.
let sm = SourceMap::new( let mut sm = SourceMap::new(
None, None,
vec!["\0".into()], vec!["\0".into()],
None, None,
@ -173,8 +179,9 @@ fn test_encode_escape_string() {
vec![], vec![],
None, None,
); );
sm.set_x_google_ignore_list(vec![0]);
assert_eq!( assert_eq!(
sm.to_json_string().unwrap(), 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":""}"#
); );
} }

View file

@ -16,6 +16,10 @@ pub struct SourceMap {
pub(crate) source_contents: Option<Vec<Arc<str>>>, pub(crate) source_contents: Option<Vec<Arc<str>>>,
pub(crate) tokens: Vec<Token>, pub(crate) tokens: Vec<Token>,
pub(crate) token_chunks: Option<Vec<TokenChunk>>, pub(crate) token_chunks: Option<Vec<TokenChunk>>,
/// 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<Vec<u32>>,
} }
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
@ -29,7 +33,16 @@ impl SourceMap {
tokens: Vec<Token>, tokens: Vec<Token>,
token_chunks: Option<Vec<TokenChunk>>, token_chunks: Option<Vec<TokenChunk>>,
) -> Self { ) -> 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. /// Convert `SourceMap` to vlq sourcemap string.
@ -69,6 +82,11 @@ impl SourceMap {
self.source_root.as_deref() self.source_root.as_deref()
} }
/// Set `x_google_ignoreList`.
pub fn set_x_google_ignore_list(&mut self, x_google_ignore_list: Vec<u32>) {
self.x_google_ignore_list = Some(x_google_ignore_list);
}
pub fn get_names(&self) -> impl Iterator<Item = &str> { pub fn get_names(&self) -> impl Iterator<Item = &str> {
self.names.iter().map(AsRef::as_ref) self.names.iter().map(AsRef::as_ref)
} }