Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .agents/RSPACK_SOURCES.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ strings owned by these inputs instead of cloning them:
- `from_bytes` retains the JSON buffer that parsed fields borrow from;
- computed maps borrow strings from the source graph, and `into_static(owner)` retains that owner;
- `as_borrowed` creates a cheap borrowed view.
- `sources_content` entries use `Option<Cow<str>>`: `None` is missing content (JSON `null`),
while `Some("")` is an explicitly empty source. Preserve this distinction through parsing,
streaming, serialization, and persistent cache. Omit `sourcesContent` only when all entries
are missing; map builders keep present arrays aligned with `sources`.

## 4. Source types and file map

Expand Down
9 changes: 5 additions & 4 deletions crates/rspack_core/src/utils/extract_source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,11 @@ pub async fn extract_source_map(
}

// Build the final SourceMap using setter methods - consume resolved_sources to avoid cloning
let (sources_vec, sources_content_vec): (Vec<String>, Vec<Cow<'_, str>>) = resolved_sources
.into_iter()
.map(|(url, content)| (url, Cow::Owned(content.unwrap_or_default())))
.unzip();
let (sources_vec, sources_content_vec): (Vec<String>, Vec<Option<Cow<'_, str>>>) =
resolved_sources
.into_iter()
.map(|(url, content)| (url, content.map(Cow::Owned)))
.unzip();

source_map.set_sources(sources_vec);
source_map.set_sources_content(sources_content_vec);
Expand Down
16 changes: 7 additions & 9 deletions crates/rspack_javascript_compiler/src/compiler/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ fn build_rspack_source_map(
#[derive(Default)]
struct RspackSourceMapBuilder<'a> {
mappings: Vec<Mapping>,
sources_content: Vec<Cow<'a, str>>,
sources_content: Vec<Option<Cow<'a, str>>>,
source_indices: FxHashMap<Cow<'a, str>, u32>,
name_indices: FxHashMap<Cow<'a, str>, u32>,
ignore_list: FxHashSet<u32>,
Expand All @@ -292,9 +292,7 @@ impl<'a> RspackSourceMapBuilder<'a> {
}

let index = self.source_indices.len() as u32;
self
.sources_content
.push(source_content.unwrap_or(Cow::Borrowed("")));
self.sources_content.push(source_content);
self.source_indices.insert(source, index);
index
}
Expand Down Expand Up @@ -324,7 +322,7 @@ impl<'a> RspackSourceMapBuilder<'a> {
self
.sources_content
.into_iter()
.map(|source_content| Cow::Owned(source_content.into_owned()))
.map(|source_content| source_content.map(|content| Cow::Owned(content.into_owned())))
.collect(),
ordered_cows(self.name_indices),
);
Expand Down Expand Up @@ -579,7 +577,7 @@ mod tests {
.into_iter(),
),
vec!["original.js".into()],
vec![source.into()],
vec![Some(source.into())],
vec![],
);

Expand Down Expand Up @@ -641,7 +639,7 @@ mod tests {
.into_iter(),
),
vec!["original.js".into()],
vec![source.into()],
vec![Some(source.into())],
vec![],
);

Expand Down Expand Up @@ -710,8 +708,8 @@ mod tests {
),
vec!["/src/app.js".into(), "https://example.com/vendor.js".into()],
vec![
"const first = second;\n".into(),
"export const second = 1;\n".into(),
Some("const first = second;\n".into()),
Some("export const second = 1;\n".into()),
],
vec![],
);
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_loader_lightningcss/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl LightningCssLoader {
parcel_source_map
.get_sources_content()
.iter()
.map(|source_content| Cow::Owned(source_content.clone()))
.map(|source_content| Some(Cow::Owned(source_content.clone())))
.collect::<Vec<_>>(),
parcel_source_map
.get_names()
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_rsdoctor/src/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub fn collect_module_original_sources(
.and_then(|s| s.map(&MapOptions::default()))
.and_then(|s| {
let idx = s.sources().iter().position(|s| s.eq(&resource))?;
let source = s.sources_content().get(idx)?;
let source = s.get_source_content(idx)?;
Some(RsdoctorModuleOriginalSource {
module: *module_ukey,
source: source.to_string(),
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_sources/src/cacheable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct CacheableSourceMap {
version: u8,
file: Option<String>,
sources: Vec<String>,
sources_content: Vec<String>,
sources_content: Vec<Option<String>>,
names: Vec<String>,
mappings: String,
source_root: Option<String>,
Expand All @@ -31,7 +31,7 @@ impl From<&SourceMap<'_>> for CacheableSourceMap {
sources_content: fields
.sources_content
.iter()
.map(ToString::to_string)
.map(|content| content.as_deref().map(str::to_owned))
.collect(),
names: fields.names.iter().map(ToString::to_string).collect(),
mappings: fields.mappings.to_string(),
Expand All @@ -56,7 +56,7 @@ impl From<CacheableSourceMap> for SourceMap<'static> {
sources_content: map
.sources_content
.into_iter()
.map(Into::into)
.map(|content| content.map(Into::into))
.collect::<Vec<_>>()
.into(),
names: map
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_sources/src/cached_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ mod tests {
source_map: SourceMap::new(
";AACA",
vec!["index.js".into()],
vec!["// DELETE IT\nconsole.log(1)".into()],
vec![Some("// DELETE IT\nconsole.log(1)".into())],
vec![],
),
})
Expand Down
28 changes: 20 additions & 8 deletions crates/rspack_sources/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
pub fn get_map<'a>(chunks: &dyn Chunks<'a>, options: &MapOptions) -> Option<SourceMapFields<'a>> {
let mut mappings_encoder = create_encoder(options.columns);
let mut sources: Vec<Cow<'a, str>> = Vec::new();
let mut sources_content: Vec<Cow<'a, str>> = Vec::new();
let mut sources_content: Vec<Option<Cow<'a, str>>> = Vec::new();
let mut names: Vec<Cow<'a, str>> = Vec::new();

chunks.stream(
Expand All @@ -40,9 +40,9 @@ pub fn get_map<'a>(chunks: &dyn Chunks<'a>, options: &MapOptions) -> Option<Sour
sources[source_index] = source;
if let Some(source_content) = source_content {
if sources_content.len() <= source_index {
sources_content.resize(source_index + 1, Cow::Borrowed(""));
sources_content.resize(source_index + 1, None);
}
sources_content[source_index] = Cow::Borrowed(source_content);
sources_content[source_index] = Some(Cow::Borrowed(source_content));
}
},
// on_name
Expand All @@ -54,6 +54,9 @@ pub fn get_map<'a>(chunks: &dyn Chunks<'a>, options: &MapOptions) -> Option<Sour
names[name_index] = name;
},
);
if !sources_content.is_empty() {
sources_content.resize(sources.len(), None);
}
let mappings = mappings_encoder.drain();
(!mappings.is_empty()).then_some(SourceMapFields {
version: 3,
Expand Down Expand Up @@ -1291,12 +1294,18 @@ pub fn stream_chunks_of_combined_source_map<'chunk, 'source>(
*inner_source = source_content;
}
source_index_mapping.borrow_mut().insert(i, -2);
// Without intermediate text we cannot stream the inner map. Leave its
// mappings empty so the outer mapping follows the existing fallback,
// retaining missing content or dropping it when remove_inner_source is set.
let Some(source_content) = source_content else {
return;
};
stream_chunks_of_source_map(
&MapOptions {
columns: options.columns,
final_source: false,
},
TextSpan::new(source_content.unwrap()),
TextSpan::new(source_content),
inner_source_map,
&mut |chunk, mapping| {
let mut inner_source_map_line_data = inner_source_map_line_data.borrow_mut();
Expand Down Expand Up @@ -1390,7 +1399,7 @@ pub fn stream_and_get_source_and_map<'source, 'chunk>(
) -> (GeneratedInfo, Option<SourceMapFields<'source>>) {
let mut mappings_encoder = create_encoder(options.columns);
let mut sources: Vec<Cow<'source, str>> = Vec::new();
let mut sources_content: Vec<Cow<'source, str>> = Vec::new();
let mut sources_content: Vec<Option<Cow<'source, str>>> = Vec::new();
let mut names: Vec<Cow<'source, str>> = Vec::new();

let generated_info = chunks.stream(
Expand All @@ -1406,10 +1415,10 @@ pub fn stream_and_get_source_and_map<'source, 'chunk>(
}
sources[source_index2] = source.clone();
if let Some(source_content) = source_content {
while sources_content.len() <= source_index2 {
sources_content.push(Cow::Borrowed(""));
if sources_content.len() <= source_index2 {
sources_content.resize(source_index2 + 1, None);
}
sources_content[source_index2] = Cow::Borrowed(source_content);
sources_content[source_index2] = Some(Cow::Borrowed(source_content));
}
on_source(source_index, source, source_content);
},
Expand All @@ -1423,6 +1432,9 @@ pub fn stream_and_get_source_and_map<'source, 'chunk>(
},
);

if !sources_content.is_empty() {
sources_content.resize(sources.len(), None);
}
let mappings = mappings_encoder.drain();
let map = if mappings.is_empty() {
None
Expand Down
11 changes: 7 additions & 4 deletions crates/rspack_sources/src/mapping_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use crate::{
MapOptions, RawBufferSource, RawStringSource, Source, SourceMap, SourceValue,
helpers::{Chunks, GeneratedInfo, StreamChunks, stream_chunks_of_raw_rope},
source::is_all_empty,
source::is_all_none,
};

#[cfg_attr(feature = "allocative", derive(rspack_allocative::Allocative))]
Expand Down Expand Up @@ -161,7 +161,7 @@ impl MappingSource {
.fields()
.sources_content
.iter()
.map(AsRef::as_ref)
.filter_map(Option::as_deref)
}

fn original_map(&self) -> &SourceMap<'static> {
Expand Down Expand Up @@ -425,13 +425,16 @@ impl MapData {
visit(Part::String(source));
}
visit(Part::Raw("]"));
if !is_all_empty(&fields.sources_content) {
if !is_all_none(&fields.sources_content) {
visit(Part::Raw(",\"sourcesContent\":["));
for (i, content) in fields.sources_content.iter().enumerate() {
if i != 0 {
visit(Part::Raw(","));
}
visit(Part::String(content));
match content {
Some(content) => visit(Part::String(content)),
None => visit(Part::Raw("null")),
}
}
visit(Part::Raw("]"));
}
Expand Down
Loading