Skip to content

Commit

Permalink
fix: binding
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Jan 31, 2024
1 parent c371ec0 commit 0ea2df4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 58 deletions.
15 changes: 2 additions & 13 deletions crates/rolldown_binding/src/options/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,8 @@ pub struct SourceMap {
// pub x_google_ignore_list: Option<Vec<u32>>,
}

impl From<SourceMap> for napi::Result<rolldown_sourcemap::SourceMap> {
impl From<SourceMap> for rolldown_sourcemap::SourceMap {
fn from(value: SourceMap) -> Self {
let mut map = rolldown_sourcemap::SourceMap::new(value.source_root.as_deref().unwrap_or(""));
if let Err(e) = map.add_vlq_map(
value.mappings.as_bytes(),
value.sources,
value.sources_content,
value.names,
0,
0,
) {
return Err(napi::Error::from_reason(format!("{e}")));
}
Ok(map)
Self::new(value.mappings, value.names, value.source_root, value.sources, value.sources_content)
}
}
128 changes: 83 additions & 45 deletions crates/rolldown_sourcemap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,71 @@
pub use parcel_sourcemap::SourceMap;
use parcel_sourcemap::SourceMap as ParcelSourcemap;

pub fn collapse_sourcemaps(mut sourcemaps: Vec<SourceMap>) -> Result<SourceMap, String> {
sourcemaps.reverse();
#[derive(Debug, Default, Clone)]
pub struct SourceMap {
pub mappings: String,
pub names: Vec<String>,
pub source_root: Option<String>,
pub sources: Vec<String>,
pub sources_content: Vec<String>,
inner: Option<ParcelSourcemap>,
}

impl SourceMap {
pub fn new(
mappings: String,
names: Vec<String>,
source_root: Option<String>,
sources: Vec<String>,
sources_content: Vec<String>,
) -> Self {
Self { mappings, names, source_root, sources, sources_content, inner: None }
}

pub fn to_json(&mut self) -> String {
self.inner.as_mut().expect("should have inner").to_json(None).expect("should success")
}

pub fn to_data_url(&mut self) -> String {
self.inner.as_mut().expect("should have inner").to_data_url(None).expect("should success")
}
}

impl From<ParcelSourcemap> for SourceMap {
fn from(value: ParcelSourcemap) -> Self {
Self { inner: Some(value), ..Default::default() }
}
}

let Some(mut result) = sourcemaps.pop() else { return Ok(SourceMap::new("")) };
pub fn collapse_sourcemaps(sourcemap_chain: Vec<SourceMap>) -> Result<Option<SourceMap>, String> {
let mut parcel_sourcemap_chain = sourcemap_chain
.into_iter()
.map(|sourcemap| {
let mut map = ParcelSourcemap::new(sourcemap.source_root.as_deref().unwrap_or(""));
if let Err(e) = map.add_vlq_map(
sourcemap.mappings.as_bytes(),
sourcemap.sources,
sourcemap.sources_content,
sourcemap.names,
0,
0,
) {
return Err(format!("{e}"));
}
Ok(map)
})
.rev()
.collect::<Result<Vec<_>, String>>()?;

for mut sourcemap in sourcemaps.into_iter().rev() {
let Some(mut result) = parcel_sourcemap_chain.pop() else { return Ok(None) };

for mut sourcemap in parcel_sourcemap_chain.into_iter().rev() {
if let Err(e) = sourcemap.extends(&mut result) {
return Err(format!("{e}"));
};
result = sourcemap;
}

Ok(result)
Ok(Some(result.into()))
}

#[cfg(test)]
Expand All @@ -22,48 +75,33 @@ mod tests {
#[test]
fn it_works() {
let sourcemaps = vec![
SourceMap::from_json(
"/",
r#"{
"version": 3,
"file": "index.js",
"sourceRoot": "",
"sources": [
"index.ts"
],
"names": [],
"mappings": "AAAA,SAAS,QAAQ,CAAC,IAAY;IAC5B,OAAO,CAAC,GAAG,CAAC,iBAAU,IAAI,CAAE,CAAC,CAAC;AAChC,CAAC",
"sourcesContent": [
"function sayHello(name: string) {\n console.log(`Hello, ${name}`);\n}\n"
]
}"#,
).unwrap(),
SourceMap::from_json(
"/",
r#"{
"version": 3,
"file": "minify.js",
"sourceRoot": "",
"sources": [
"index.ts"
],
"names": [
"sayHello",
"name",
"console",
"log",
"concat"
],
"mappings": "AAAA,SAASA,SAASC,CAAI,EAClBC,QAAQC,GAAG,CAAC,UAAUC,MAAM,CAACH,GACjC",
"sourcesContent": [
"function sayHello(name) {\n console.log(\"Hello, \".concat(name));\n}\n"
]
}"#,
).unwrap(),
SourceMap::new(
"AAAA,SAAS,QAAQ,CAAC,IAAY;IAC5B,OAAO,CAAC,GAAG,CAAC,iBAAU,IAAI,CAAE,CAAC,CAAC;AAChC,CAAC"
.to_string(),
vec![],
None,
vec!["index.ts".to_string()],
vec!["function sayHello(name: string) {\n console.log(`Hello, ${name}`);\n}\n".to_string()],
),
SourceMap::new(
"AAAA,SAASA,SAASC,CAAI,EAClBC,QAAQC,GAAG,CAAC,UAAUC,MAAM,CAACH,GACjC".to_string(),
vec![
"sayHello".to_string(),
"name".to_string(),
"console".to_string(),
"log".to_string(),
"concat".to_string(),
],
None,
vec!["index.ts".to_string()],
vec![
"function sayHello(name) {\n console.log(\"Hello, \".concat(name));\n}\n".to_string()
],
),
];

let result =
super::collapse_sourcemaps(sourcemaps).expect("should not fail").to_json(None).unwrap();
super::collapse_sourcemaps(sourcemaps).expect("should not fail").unwrap().to_json();

let expected = r#"{
"version": 3,
Expand Down

0 comments on commit 0ea2df4

Please sign in to comment.