Skip to content

Commit

Permalink
fix(sourcemap): align sourcemap type with Rollup (#6133)
Browse files Browse the repository at this point in the history
closes #5578
  • Loading branch information
Boshen committed Sep 28, 2024
1 parent db751f0 commit 6f98aad
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 36 deletions.
55 changes: 31 additions & 24 deletions crates/oxc_sourcemap/src/decode.rs
Original file line number Diff line number Diff line change
@@ -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” 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<String>,
/// 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<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> {
Expand Down Expand Up @@ -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");
}
6 changes: 3 additions & 3 deletions crates/oxc_sourcemap/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
}
Expand Down
12 changes: 7 additions & 5 deletions napi/transform/src/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand All @@ -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,
}
Expand Down

0 comments on commit 6f98aad

Please sign in to comment.