Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a1242d

Browse files
committed
feat: support sourcemapPathTransform option
1 parent 96bcdfb commit 1a1242d

14 files changed

Lines changed: 69 additions & 11 deletions

File tree

crates/rolldown/src/stages/generate_stage/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ impl<'a> GenerateStage<'a> {
115115
}
116116
}
117117

118+
if let Some(sourcemap_path_transform) = &self.options.sourcemap_path_transform {
119+
let mut sources = Vec::with_capacity(map.get_sources().count());
120+
for source in map.get_sources() {
121+
sources.push(sourcemap_path_transform.call(source, map_file_name.as_str()).await?);
122+
}
123+
map.set_sources(sources.iter().map(std::convert::AsRef::as_ref).collect::<Vec<_>>());
124+
}
125+
118126
match self.options.sourcemap {
119127
SourceMapType::File => {
120128
let source = match map.to_json_string().map_err(BuildError::sourcemap_error) {

crates/rolldown/src/utils/normalize_options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub fn normalize_options(mut raw_options: crate::BundlerOptions) -> NormalizeOpt
3333
format: raw_options.format.unwrap_or(crate::OutputFormat::Esm),
3434
sourcemap: raw_options.sourcemap.unwrap_or(SourceMapType::Hidden),
3535
sourcemap_ignore_list: raw_options.sourcemap_ignore_list,
36+
sourcemap_path_transform: raw_options.sourcemap_path_transform,
3637
shim_missing_exports: raw_options.shim_missing_exports.unwrap_or(false),
3738
};
3839

crates/rolldown_binding/src/options/binding_output_options/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ pub struct BindingOutputOptions {
7171
#[serde(skip_deserializing)]
7272
#[napi(ts_type = "(source: string, sourcemapPath: string) => boolean")]
7373
pub sourcemap_ignore_list: Option<ThreadsafeFunction<(String, String), bool, false>>,
74+
#[derivative(Debug = "ignore")]
75+
#[serde(skip_deserializing)]
76+
#[napi(ts_type = "(source: string, sourcemapPath: string) => string")]
77+
pub sourcemap_path_transform: Option<ThreadsafeFunction<(String, String), String, false>>,
7478
// sourcemapExcludeSources: boolean;
7579
// sourcemapFile: string | undefined;
76-
// sourcemapPathTransform: SourcemapPathTransformOption | undefined;
7780
// strict: boolean;
7881
// systemNullSetters: boolean;
7982
// validate: boolean;

crates/rolldown_binding/src/utils/normalize_binding_options.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ pub fn normalize_binding_options(
6363
}))
6464
});
6565

66+
let sourcemap_path_transform = output_options.sourcemap_path_transform.map(|ts_fn| {
67+
rolldown::SourceMapPathTransform::new(Box::new(move |source, sourcemap_path| {
68+
let ts_fn = ts_fn.clone();
69+
let source = source.to_string();
70+
let sourcemap_path = sourcemap_path.to_string();
71+
Box::pin(async move {
72+
ts_fn.call_async((source, sourcemap_path)).await.map_err(anyhow::Error::from)
73+
})
74+
}))
75+
});
76+
6677
let bundler_options = BundlerOptions {
6778
input: Some(input_options.input.into_iter().map(Into::into).collect()),
6879
cwd: cwd.into(),
@@ -83,6 +94,7 @@ pub fn normalize_binding_options(
8394
banner: normalize_addon_option(output_options.banner),
8495
footer: normalize_addon_option(output_options.footer),
8596
sourcemap_ignore_list,
97+
sourcemap_path_transform,
8698
// TODO(hyf0): remove this line, all options should set explicitly
8799
..Default::default()
88100
};

crates/rolldown_common/src/inner_bundler_options/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::SourceMapIgnoreList;
1010
use self::types::{
1111
external::External, input_item::InputItem, output_format::OutputFormat,
1212
output_option::AddonOutputOption, platform::Platform, resolve_options::ResolveOptions,
13-
source_map_type::SourceMapType,
13+
source_map_type::SourceMapType, sourcemap_path_transform::SourceMapPathTransform,
1414
};
1515

1616
pub mod types;
@@ -58,6 +58,12 @@ pub struct BundlerOptions {
5858
schemars(skip)
5959
)]
6060
pub sourcemap_ignore_list: Option<SourceMapIgnoreList>,
61+
#[cfg_attr(
62+
feature = "deserialize_bundler_options",
63+
serde(default, skip_deserializing),
64+
schemars(skip)
65+
)]
66+
pub sourcemap_path_transform: Option<SourceMapPathTransform>,
6167
// --- options for resolve
6268
pub resolve: Option<ResolveOptions>,
6369
}

crates/rolldown_common/src/inner_bundler_options/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ pub mod platform;
88
pub mod resolve_options;
99
pub mod source_map_type;
1010
pub mod sourcemap_ignore_list;
11+
pub mod sourcemap_path_transform;

crates/rolldown_common/src/inner_bundler_options/types/normalized_bundler_options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::{
77
external::External, file_name_template::FileNameTemplate, input_item::InputItem,
88
output_format::OutputFormat, output_option::AddonOutputOption, platform::Platform,
99
source_map_type::SourceMapType, sourcemap_ignore_list::SourceMapIgnoreList,
10+
sourcemap_path_transform::SourceMapPathTransform,
1011
};
1112

1213
#[derive(Debug)]
@@ -27,4 +28,5 @@ pub struct NormalizedBundlerOptions {
2728
pub banner: Option<AddonOutputOption>,
2829
pub footer: Option<AddonOutputOption>,
2930
pub sourcemap_ignore_list: Option<SourceMapIgnoreList>,
31+
pub sourcemap_path_transform: Option<SourceMapPathTransform>,
3032
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::fmt::Debug;
2+
use std::{future::Future, pin::Pin};
3+
4+
type SourceMapPathTransformFn = dyn Fn(&str, &str) -> Pin<Box<(dyn Future<Output = anyhow::Result<String>> + Send + 'static)>>
5+
+ Send
6+
+ Sync;
7+
8+
pub struct SourceMapPathTransform(Box<SourceMapPathTransformFn>);
9+
10+
impl Debug for SourceMapPathTransform {
11+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12+
write!(f, "SourceMapPathTransform::Fn(...)")
13+
}
14+
}
15+
16+
impl SourceMapPathTransform {
17+
pub fn new(f: Box<SourceMapPathTransformFn>) -> Self {
18+
Self(f)
19+
}
20+
21+
pub async fn call(&self, source: &str, sourcemap_path: &str) -> anyhow::Result<String> {
22+
self.0(source, sourcemap_path).await
23+
}
24+
}

crates/rolldown_common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mod bundler_options {
1919
resolve_options::ResolveOptions,
2020
source_map_type::SourceMapType,
2121
sourcemap_ignore_list::SourceMapIgnoreList,
22+
sourcemap_path_transform::SourceMapPathTransform,
2223
},
2324
BundlerOptions,
2425
};

packages/rolldown/src/binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export interface BindingOutputOptions {
9898
plugins: Array<BindingPluginOrParallelJsPluginPlaceholder>
9999
sourcemap?: 'file' | 'inline' | 'hidden'
100100
sourcemapIgnoreList?: (source: string, sourcemapPath: string) => boolean
101+
sourcemapPathTransform?: (source: string, sourcemapPath: string) => string
101102
}
102103

103104
export interface BindingPluginContextResolveOptions {

0 commit comments

Comments
 (0)