Skip to content

Commit c820418

Browse files
authored
Turbopack: improve sectioned source maps (#76627)
### What? * avoid single entry sectioned source maps * avoid empty generated source map sections
1 parent 8c33e48 commit c820418

File tree

90 files changed

+101
-320
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+101
-320
lines changed

turbopack/crates/turbopack-core/src/code_builder.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,15 @@ impl Code {
187187
}
188188
last_byte_pos = *byte_pos;
189189

190-
sections.push((pos, map.clone().unwrap_or_else(SourceMap::empty_rope)))
190+
if pos.column != 0 || map.is_some() {
191+
sections.push((pos, map.clone().unwrap_or_else(SourceMap::empty_rope)))
192+
}
191193
}
192194

193-
SourceMap::sections_to_rope(sections)
195+
if sections.len() == 1 && sections[0].0.line == 0 && sections[0].0.column == 0 {
196+
Ok(sections.into_iter().next().unwrap().1)
197+
} else {
198+
SourceMap::sections_to_rope(sections)
199+
}
194200
}
195201
}

turbopack/crates/turbopack-ecmascript/src/minify.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub fn minify(path: &FileSystemPath, code: &Code, source_maps: bool, mangle: boo
118118
builder,
119119
// findSourceMapURL assumes this co-located sourceMappingURL,
120120
// and needs to be adjusted in case this is ever changed.
121-
"\n\n//# sourceMappingURL={}.map",
121+
"\n//# sourceMappingURL={}.map",
122122
urlencoding::encode(path.file_name())
123123
)?;
124124
} else {
@@ -156,6 +156,10 @@ fn print_program(
156156
.emit_program(&program)
157157
.context("failed to emit module")?;
158158
}
159+
if source_maps {
160+
// end with a new line when we have a source map comment
161+
buf.push(b'\n');
162+
}
159163
// Invalid utf8 is valid in javascript world.
160164
// SAFETY: SWC generates valid utf8.
161165
unsafe { String::from_utf8_unchecked(buf) }

turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/4c35f_tests_snapshot_basic-tree-shake_dynamic-import_input_index_92a5f455.js.map

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/4c35f_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_8debd8f1.js.map

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/output/b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-named_input_8a727493._.js.map

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)