Skip to content

Commit 0888886

Browse files
authored
Turbopack: correctly apply generate_source_map with scope hoisting (vercel#81060)
Don't generate source maps if the corresponding merged module specified that (i.e. have different values of `generate_source_map: bool` inside a single AST.
1 parent 52a761e commit 0888886

File tree

1 file changed

+51
-19
lines changed
  • turbopack/crates/turbopack-ecmascript/src

1 file changed

+51
-19
lines changed

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

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ impl EcmascriptModuleContent {
11231123
let (merged_ast, comments, source_maps, original_source_maps) =
11241124
merge_modules(contents, &entry_points, &globals_merged).await?;
11251125

1126-
// Use the options from an arbitrary module, since they should all be the same.
1126+
// Use the options from an arbitrary module, since they should all be the same with regards
1127+
// to minify_type and chunking_context.
11271128
let options = module_options.last().unwrap().await?;
11281129

11291130
let modules_header_width = modules.len().next_power_of_two().trailing_zeros();
@@ -1140,7 +1141,6 @@ impl EcmascriptModuleContent {
11401141
export_contexts: None,
11411142
is_esm: true,
11421143
strict: true,
1143-
generate_source_map: options.generate_source_map,
11441144
original_source_map: CodeGenResultOriginalSourceMap::ScopeHoisting(
11451145
original_source_maps,
11461146
),
@@ -1543,7 +1543,6 @@ struct CodeGenResult {
15431543
export_contexts: Option<FxHashMap<RcStr, Id>>,
15441544
is_esm: bool,
15451545
strict: bool,
1546-
generate_source_map: bool,
15471546
original_source_map: CodeGenResultOriginalSourceMap,
15481547
minify: MinifyType,
15491548
scope_hoisting_syntax_contexts:
@@ -1715,8 +1714,12 @@ async fn process_parse_result(
17151714

17161715
Ok(CodeGenResult {
17171716
program,
1718-
source_map: CodeGenResultSourceMap::Single {
1719-
source_map: source_map.clone(),
1717+
source_map: if generate_source_map {
1718+
CodeGenResultSourceMap::Single {
1719+
source_map: source_map.clone(),
1720+
}
1721+
} else {
1722+
CodeGenResultSourceMap::None
17201723
},
17211724
comments: CodeGenResultComments::Single {
17221725
comments,
@@ -1726,7 +1729,6 @@ async fn process_parse_result(
17261729
export_contexts: Some(export_contexts.into_owned()),
17271730
is_esm,
17281731
strict,
1729-
generate_source_map,
17301732
original_source_map: CodeGenResultOriginalSourceMap::Single(original_source_map),
17311733
minify,
17321734
scope_hoisting_syntax_contexts: retain_syntax_context.map(|(_, ctxts, _)| ctxts),
@@ -1757,12 +1759,11 @@ async fn process_parse_result(
17571759
body,
17581760
shebang: None,
17591761
}),
1760-
source_map: CodeGenResultSourceMap::default(),
1762+
source_map: CodeGenResultSourceMap::None,
17611763
comments: CodeGenResultComments::Empty,
17621764
export_contexts: None,
17631765
is_esm: false,
17641766
strict: false,
1765-
generate_source_map: false,
17661767
original_source_map: CodeGenResultOriginalSourceMap::Single(None),
17671768
minify: MinifyType::NoMinify,
17681769
scope_hoisting_syntax_contexts: None,
@@ -1785,12 +1786,11 @@ async fn process_parse_result(
17851786
body,
17861787
shebang: None,
17871788
}),
1788-
source_map: CodeGenResultSourceMap::default(),
1789+
source_map: CodeGenResultSourceMap::None,
17891790
comments: CodeGenResultComments::Empty,
17901791
export_contexts: None,
17911792
is_esm: false,
17921793
strict: false,
1793-
generate_source_map: false,
17941794
original_source_map: CodeGenResultOriginalSourceMap::Single(None),
17951795
minify: MinifyType::NoMinify,
17961796
scope_hoisting_syntax_contexts: None,
@@ -1881,13 +1881,14 @@ async fn emit_content(
18811881
comments,
18821882
is_esm,
18831883
strict,
1884-
generate_source_map,
18851884
original_source_map,
18861885
minify,
18871886
export_contexts: _,
18881887
scope_hoisting_syntax_contexts: _,
18891888
} = content;
18901889

1890+
let generate_source_map = source_map.is_some();
1891+
18911892
let mut bytes: Vec<u8> = vec![];
18921893
// TODO: Insert this as a sourceless segment so that sourcemaps aren't affected.
18931894
// = format!("/* {} */\n", self.module.path().to_string().await?).into_bytes();
@@ -2056,7 +2057,11 @@ fn hygiene_rename_only(
20562057
)
20572058
}
20582059

2060+
#[derive(Default)]
20592061
enum CodeGenResultSourceMap {
2062+
#[default]
2063+
/// No source map should be generated for this module
2064+
None,
20602065
Single {
20612066
source_map: Arc<SourceMap>,
20622067
},
@@ -2068,9 +2073,20 @@ enum CodeGenResultSourceMap {
20682073
},
20692074
}
20702075

2076+
impl CodeGenResultSourceMap {
2077+
fn is_some(&self) -> bool {
2078+
match self {
2079+
CodeGenResultSourceMap::None => false,
2080+
CodeGenResultSourceMap::Single { .. }
2081+
| CodeGenResultSourceMap::ScopeHoisting { .. } => true,
2082+
}
2083+
}
2084+
}
2085+
20712086
impl Debug for CodeGenResultSourceMap {
20722087
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20732088
match self {
2089+
CodeGenResultSourceMap::None => write!(f, "CodeGenResultSourceMap::None"),
20742090
CodeGenResultSourceMap::Single { source_map } => {
20752091
write!(
20762092
f,
@@ -2090,20 +2106,13 @@ impl Debug for CodeGenResultSourceMap {
20902106
}
20912107
}
20922108

2093-
impl Default for CodeGenResultSourceMap {
2094-
fn default() -> Self {
2095-
CodeGenResultSourceMap::Single {
2096-
source_map: Arc::new(SourceMap::default()),
2097-
}
2098-
}
2099-
}
2100-
21012109
impl Files for CodeGenResultSourceMap {
21022110
fn try_lookup_source_file(
21032111
&self,
21042112
pos: BytePos,
21052113
) -> Result<Option<Arc<SourceFile>>, SourceMapLookupError> {
21062114
match self {
2115+
CodeGenResultSourceMap::None => Ok(None),
21072116
CodeGenResultSourceMap::Single { source_map } => source_map.try_lookup_source_file(pos),
21082117
CodeGenResultSourceMap::ScopeHoisting {
21092118
modules_header_width,
@@ -2118,6 +2127,7 @@ impl Files for CodeGenResultSourceMap {
21182127

21192128
fn is_in_file(&self, f: &Arc<SourceFile>, raw_pos: BytePos) -> bool {
21202129
match self {
2130+
CodeGenResultSourceMap::None => false,
21212131
CodeGenResultSourceMap::Single { .. } => f.start_pos <= raw_pos && raw_pos < f.end_pos,
21222132
CodeGenResultSourceMap::ScopeHoisting { .. } => {
21232133
// let (module, pos) = CodeGenResultComments::decode_bytepos(*modules_header_width,
@@ -2132,6 +2142,7 @@ impl Files for CodeGenResultSourceMap {
21322142

21332143
fn map_raw_pos(&self, pos: BytePos) -> BytePos {
21342144
match self {
2145+
CodeGenResultSourceMap::None => BytePos::DUMMY,
21352146
CodeGenResultSourceMap::Single { .. } => pos,
21362147
CodeGenResultSourceMap::ScopeHoisting {
21372148
modules_header_width,
@@ -2144,6 +2155,9 @@ impl Files for CodeGenResultSourceMap {
21442155
impl SourceMapper for CodeGenResultSourceMap {
21452156
fn lookup_char_pos(&self, pos: BytePos) -> Loc {
21462157
match self {
2158+
CodeGenResultSourceMap::None => {
2159+
panic!("CodeGenResultSourceMap::None cannot lookup_char_pos")
2160+
}
21472161
CodeGenResultSourceMap::Single { source_map } => source_map.lookup_char_pos(pos),
21482162
CodeGenResultSourceMap::ScopeHoisting {
21492163
modules_header_width,
@@ -2157,6 +2171,9 @@ impl SourceMapper for CodeGenResultSourceMap {
21572171
}
21582172
fn span_to_lines(&self, sp: Span) -> FileLinesResult {
21592173
match self {
2174+
CodeGenResultSourceMap::None => {
2175+
panic!("CodeGenResultSourceMap::None cannot span_to_lines")
2176+
}
21602177
CodeGenResultSourceMap::Single { source_map } => source_map.span_to_lines(sp),
21612178
CodeGenResultSourceMap::ScopeHoisting {
21622179
modules_header_width,
@@ -2173,6 +2190,9 @@ impl SourceMapper for CodeGenResultSourceMap {
21732190
}
21742191
fn span_to_string(&self, sp: Span) -> String {
21752192
match self {
2193+
CodeGenResultSourceMap::None => {
2194+
panic!("CodeGenResultSourceMap::None cannot span_to_string")
2195+
}
21762196
CodeGenResultSourceMap::Single { source_map } => source_map.span_to_string(sp),
21772197
CodeGenResultSourceMap::ScopeHoisting {
21782198
modules_header_width,
@@ -2189,6 +2209,9 @@ impl SourceMapper for CodeGenResultSourceMap {
21892209
}
21902210
fn span_to_filename(&self, sp: Span) -> Arc<FileName> {
21912211
match self {
2212+
CodeGenResultSourceMap::None => {
2213+
panic!("CodeGenResultSourceMap::None cannot span_to_filename")
2214+
}
21922215
CodeGenResultSourceMap::Single { source_map } => source_map.span_to_filename(sp),
21932216
CodeGenResultSourceMap::ScopeHoisting {
21942217
modules_header_width,
@@ -2205,6 +2228,9 @@ impl SourceMapper for CodeGenResultSourceMap {
22052228
}
22062229
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span> {
22072230
match self {
2231+
CodeGenResultSourceMap::None => {
2232+
panic!("CodeGenResultSourceMap::None cannot merge_spans")
2233+
}
22082234
CodeGenResultSourceMap::Single { source_map } => source_map.merge_spans(sp_lhs, sp_rhs),
22092235
CodeGenResultSourceMap::ScopeHoisting {
22102236
modules_header_width,
@@ -2234,6 +2260,9 @@ impl SourceMapper for CodeGenResultSourceMap {
22342260
}
22352261
fn call_span_if_macro(&self, sp: Span) -> Span {
22362262
match self {
2263+
CodeGenResultSourceMap::None => {
2264+
panic!("CodeGenResultSourceMap::None cannot call_span_if_macro")
2265+
}
22372266
CodeGenResultSourceMap::Single { source_map } => source_map.call_span_if_macro(sp),
22382267
CodeGenResultSourceMap::ScopeHoisting {
22392268
modules_header_width,
@@ -2253,6 +2282,9 @@ impl SourceMapper for CodeGenResultSourceMap {
22532282
}
22542283
fn span_to_snippet(&self, sp: Span) -> Result<String, Box<SpanSnippetError>> {
22552284
match self {
2285+
CodeGenResultSourceMap::None => {
2286+
panic!("CodeGenResultSourceMap::None cannot span_to_snippet")
2287+
}
22562288
CodeGenResultSourceMap::Single { source_map } => source_map.span_to_snippet(sp),
22572289
CodeGenResultSourceMap::ScopeHoisting {
22582290
modules_header_width,

0 commit comments

Comments
 (0)