fix(sourcemap): align sourcemap type with Rollup (#6133)

closes #5578
This commit is contained in:
Boshen 2024-09-28 04:24:05 +00:00
parent db751f0f8f
commit 6f98aadc7f
4 changed files with 45 additions and 36 deletions

View file

@ -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 <https://github.com/tc39/source-map/blob/main/source-map-rev3.md>.
#[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<String>,
// A string with the encoded mapping data.
pub mappings: Option<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.
/// 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<String>,
// A list of original sources used by the “mappings” entry.
pub sources: Option<Vec<Option<String>>>,
// An optional list of source content, useful when the “source” cant 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<String>,
/// An optional list of source content, useful when the “source” cant 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<Vec<Option<String>>>,
// A list of symbol names used by the “mappings” entry.
pub names: Option<Vec<String>>,
/// A list of symbol names used by the “mappings” entry.
pub names: Vec<String>,
}
pub fn decode(json: JSONSourceMap) -> Result<SourceMap> {
let file = json.file.map(Into::into);
let names =
json.names.map(|v| v.into_iter().map(Into::into).collect::<Vec<_>>()).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::<Vec<_>>())
.unwrap_or_default();
let source_contents = json
.sources_content
.map(|v| v.into_iter().map(Option::unwrap_or_default).map(Into::into).collect::<Vec<_>>());
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<SourceMap> {
@ -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");
}

View file

@ -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(),
}
}

View file

@ -146,11 +146,11 @@ export interface ReactRefreshBindingOptions {
export interface SourceMap {
file?: string
mappings?: string
names?: Array<string>
mappings: string
names: Array<string>
sourceRoot?: string
sources?: Array<string | undefined | null>
sourcesContent?: Array<string | undefined | null>
sources: Array<string>
sourcesContent?: Array<string>
version: number
x_google_ignoreList?: Array<number>
}

View file

@ -19,11 +19,11 @@ use napi_derive::napi;
#[napi(object)]
pub struct SourceMap {
pub file: Option<String>,
pub mappings: Option<String>,
pub names: Option<Vec<String>>,
pub mappings: String,
pub names: Vec<String>,
pub source_root: Option<String>,
pub sources: Option<Vec<Option<String>>>,
pub sources_content: Option<Vec<Option<String>>>,
pub sources: Vec<String>,
pub sources_content: Option<Vec<String>>,
pub version: u8,
#[napi(js_name = "x_google_ignoreList")]
pub x_google_ignorelist: Option<Vec<u32>>,
@ -38,7 +38,9 @@ impl From<oxc_sourcemap::SourceMap> 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::<Vec<_>>()
}),
version: 3,
x_google_ignorelist: None,
}