Skip to content

Commit

Permalink
feat(core): add sourcemap support (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Jul 14, 2024
1 parent 347f8be commit 3fe58d5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,25 @@ fn transform_output(url: String, output: LoadFnOutput) -> Result<LoadFnOutput> {
};
tracing::debug!(url = ?url, jsx = ?jsx, src_path = ?src_path, source_type = ?source_type, "running oxc transform");
let transform_output = oxc_transform(src_path, output.source.as_ref().unwrap())?;
let output_code = transform_output
.0
.source_map
.and_then(|sm| sm.to_data_url().ok())
.map(|sm| {
const SOURCEMAP_PREFIX: &str = "\n//# sourceMappingURL=";
let len = sm.len() + transform_output.0.source_text.len() + 22;
let mut output_code = String::with_capacity(len + 22);
output_code.push_str(&transform_output.0.source_text);
output_code.push_str(SOURCEMAP_PREFIX);
output_code.push_str(sm.as_str());
output_code
})
.unwrap_or_else(|| transform_output.0.source_text);

tracing::debug!("loaded {} format: {}", url, output.format);
Ok(LoadFnOutput {
format: output.format,
source: Some(Either4::B(Uint8Array::from_string(
transform_output.0.source_text,
))),
source: Some(Either4::B(Uint8Array::from_string(output_code))),
response_url: Some(url),
})
}
Expand Down

0 comments on commit 3fe58d5

Please sign in to comment.