From 6f98aadc7fb7071494fc459da65e7877dff55aa4 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Sat, 28 Sep 2024 04:24:05 +0000 Subject: [PATCH] fix(sourcemap): align sourcemap type with Rollup (#6133) closes #5578 --- crates/oxc_sourcemap/src/decode.rs | 55 +++++++++++++++++------------- crates/oxc_sourcemap/src/encode.rs | 6 ++-- napi/transform/index.d.ts | 8 ++--- napi/transform/src/sourcemap.rs | 12 ++++--- 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/crates/oxc_sourcemap/src/decode.rs b/crates/oxc_sourcemap/src/decode.rs index fa2d722d87b08..d23238a6c4f4f 100644 --- a/crates/oxc_sourcemap/src/decode.rs +++ b/crates/oxc_sourcemap/src/decode.rs @@ -1,39 +1,44 @@ /// Port from https://github.com/getsentry/rust-sourcemap/blob/master/src/decoder.rs /// It is a helper for decode vlq soucemap string to `SourceMap`. +use std::sync::Arc; + use crate::error::{Error, Result}; use crate::{SourceMap, Token}; +/// See . #[derive(serde::Deserialize, Default)] #[serde(rename_all = "camelCase")] pub struct JSONSourceMap { - // An optional name of the generated code that this source map is associated with. + /// An optional name of the generated code that this source map is associated with. pub file: Option, - // A string with the encoded mapping data. - pub mappings: Option, - // An optional source root, useful for relocating source files on a server or removing repeated values in the “sources” entry. This value is prepended to the individual entries in the “source” field. + /// A string with the encoded mapping data. + pub mappings: String, + /// An optional source root, useful for relocating source files on a server or removing repeated values in the “sources” entry. + /// This value is prepended to the individual entries in the “source” field. pub source_root: Option, - // A list of original sources used by the “mappings” entry. - pub sources: Option>>, - // An optional list of source content, useful when the “source” can’t be hosted. The contents are listed in the same order as the sources in line 5. “null” may be used if some original sources should be retrieved by name. + /// A list of original sources used by the “mappings” entry. + pub sources: Vec, + /// An optional list of source content, useful when the “source” can’t be hosted. + /// The contents are listed in the same order as the sources in line 5. “null” may be used if some original sources should be retrieved by name. pub sources_content: Option>>, - // A list of symbol names used by the “mappings” entry. - pub names: Option>, + /// A list of symbol names used by the “mappings” entry. + pub names: Vec, } pub fn decode(json: JSONSourceMap) -> Result { - let file = json.file.map(Into::into); - let names = - json.names.map(|v| v.into_iter().map(Into::into).collect::>()).unwrap_or_default(); - let source_root = json.source_root.map(Into::into); - let sources = json - .sources - .map(|v| v.into_iter().map(Option::unwrap_or_default).map(Into::into).collect::>()) - .unwrap_or_default(); - let source_contents = json - .sources_content - .map(|v| v.into_iter().map(Option::unwrap_or_default).map(Into::into).collect::>()); - let tokens = decode_mapping(&json.mappings.unwrap_or_default(), names.len(), sources.len())?; - Ok(SourceMap::new(file, names, source_root, sources, source_contents, tokens, None)) + let tokens = decode_mapping(&json.mappings, json.names.len(), json.sources.len())?; + Ok(SourceMap { + file: json.file.map(Arc::from), + names: json.names.into_iter().map(Arc::from).collect(), + source_root: json.source_root, + sources: json.sources.into_iter().map(Arc::from).collect(), + source_contents: json.sources_content.map(|content| { + content.into_iter().map(|c| c.map(Arc::from).unwrap_or_default()).collect() + }), + tokens, + token_chunks: None, + x_google_ignore_list: None, + }) } pub fn decode_from_string(value: &str) -> Result { @@ -162,8 +167,10 @@ fn test_decode_sourcemap() { #[test] fn test_decode_sourcemap_optional_filed() { let input = r#"{ - "sources": [null], - "sourcesContent": [null] + "names": [], + "sources": [], + "sourcesContent": [null], + "mappings": "" }"#; SourceMap::from_json_string(input).expect("should success"); } diff --git a/crates/oxc_sourcemap/src/encode.rs b/crates/oxc_sourcemap/src/encode.rs index a67a98ab749eb..d29f5912eb561 100644 --- a/crates/oxc_sourcemap/src/encode.rs +++ b/crates/oxc_sourcemap/src/encode.rs @@ -13,14 +13,14 @@ use crate::{token::TokenChunk, SourceMap, Token}; pub fn encode(sourcemap: &SourceMap) -> JSONSourceMap { JSONSourceMap { file: sourcemap.get_file().map(ToString::to_string), - mappings: Some(serialize_sourcemap_mappings(sourcemap)), + mappings: serialize_sourcemap_mappings(sourcemap), source_root: sourcemap.get_source_root().map(ToString::to_string), - sources: Some(sourcemap.sources.iter().map(ToString::to_string).map(Some).collect()), + sources: sourcemap.sources.iter().map(ToString::to_string).collect(), sources_content: sourcemap .source_contents .as_ref() .map(|x| x.iter().map(ToString::to_string).map(Some).collect()), - names: Some(sourcemap.names.iter().map(ToString::to_string).collect()), + names: sourcemap.names.iter().map(ToString::to_string).collect(), } } diff --git a/napi/transform/index.d.ts b/napi/transform/index.d.ts index 4f14759be5b6c..16a94a274ba1b 100644 --- a/napi/transform/index.d.ts +++ b/napi/transform/index.d.ts @@ -146,11 +146,11 @@ export interface ReactRefreshBindingOptions { export interface SourceMap { file?: string - mappings?: string - names?: Array + mappings: string + names: Array sourceRoot?: string - sources?: Array - sourcesContent?: Array + sources: Array + sourcesContent?: Array version: number x_google_ignoreList?: Array } diff --git a/napi/transform/src/sourcemap.rs b/napi/transform/src/sourcemap.rs index a9d3db830c1ad..0e7cb4d83c2db 100644 --- a/napi/transform/src/sourcemap.rs +++ b/napi/transform/src/sourcemap.rs @@ -19,11 +19,11 @@ use napi_derive::napi; #[napi(object)] pub struct SourceMap { pub file: Option, - pub mappings: Option, - pub names: Option>, + pub mappings: String, + pub names: Vec, pub source_root: Option, - pub sources: Option>>, - pub sources_content: Option>>, + pub sources: Vec, + pub sources_content: Option>, pub version: u8, #[napi(js_name = "x_google_ignoreList")] pub x_google_ignorelist: Option>, @@ -38,7 +38,9 @@ impl From for SourceMap { names: json.names, source_root: json.source_root, sources: json.sources, - sources_content: json.sources_content, + sources_content: json.sources_content.map(|content| { + content.into_iter().map(Option::unwrap_or_default).collect::>() + }), version: 3, x_google_ignorelist: None, }