getsentry / rust-sourcemap

A library for rust that implements basic sourcemap handling
Other
224 stars 27 forks source link

Support `sourceRoot` from `SourceMapBuilder` #50

Closed kdy1 closed 2 years ago

kdy1 commented 2 years ago

Related: https://github.com/swc-project/swc/issues/2362

I want to generate sourcemap like

{
  "version": 3,
  "sourceRoot": "/Users/pbadenski/workspace/project/",
  "sources": [
    "test.ts"
  ],
}

but I found that SourceMapBuilder does not provide a way to generate a sourcemap with sourceRoot

kamilogorek commented 2 years ago

Processed SourceMap struct, has sourceRoot already resolved, and all sources are correctly mapped to appropriate paths.

We could potentially add the extract_source_root option to RewriteOptions, which could then be used like so:

let sm = SourceMap::from_reader(input)?;
let processed_sm = sm.rewrite(&RewriteOptions {
    extract_source_root: true,
    ..Default::default()
})?;
processed_sm.to_writer(output)?;

extract_source_root would use a simple find common path algorithm for that.

wdyt @Swatinem @loewenheim?

Swatinem commented 2 years ago

The raw sourcemaps should have support for parsing/serializing from raw json, and also read/write when constructing with minimal validation.

While resolving sourceRoot at parse time might be the right choice for sentry, I think the swc usecase would rather have the raw values.

Using a default "find common path prefix" sounds interesting, but I think having a more explicit option is the better idea.

kdy1 commented 2 years ago

Thank you!

kamilogorek commented 2 years ago

Published in 6.2.0.