Skip to content

Commit 13cd392

Browse files
committed
Fix sourcemaps
1 parent 3107415 commit 13cd392

File tree

2 files changed

+39
-16
lines changed
  • crates/next-custom-transforms

2 files changed

+39
-16
lines changed

crates/next-custom-transforms/src/transforms/server_actions.rs

+32-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ use std::{
33
collections::{hash_map, BTreeMap},
44
convert::{TryFrom, TryInto},
55
mem::{replace, take},
6+
path::{Path, PathBuf},
67
rc::Rc,
78
sync::Arc,
89
};
910

1011
use base64::{display::Base64Display, prelude::BASE64_STANDARD};
1112
use hex::encode as hex_encode;
1213
use indoc::formatdoc;
14+
use pathdiff::diff_paths;
1315
use rustc_hash::{FxHashMap, FxHashSet};
1416
use serde::Deserialize;
1517
use sha1::{Digest, Sha1};
@@ -2168,6 +2170,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
21682170
)],
21692171
src: Some(Box::new(
21702172
program_to_data_url(
2173+
&self.file_name,
21712174
&self.cm,
21722175
vec![
21732176
ModuleItem::Stmt(Stmt::Expr(ExprStmt {
@@ -3195,6 +3198,7 @@ fn emit_error(error_kind: ServerActionsErrorKind) {
31953198
}
31963199

31973200
fn program_to_data_url(
3201+
file_name: &str,
31983202
cm: &Arc<SourceMap>,
31993203
body: Vec<ModuleItem>,
32003204
prepend_comment: Comment,
@@ -3211,12 +3215,11 @@ fn program_to_data_url(
32113215

32123216
let mut output = vec![];
32133217
let mut mappings = vec![];
3214-
let sourcemap = Arc::new(SourceMap::default());
32153218
let mut emitter = Emitter {
32163219
cfg: codegen::Config::default().with_minify(true),
3217-
cm: sourcemap.clone(),
3220+
cm: cm.clone(),
32183221
wr: Box::new(JsWriter::new(
3219-
sourcemap.clone(),
3222+
cm.clone(),
32203223
" ",
32213224
&mut output,
32223225
Some(&mut mappings),
@@ -3227,18 +3230,38 @@ fn program_to_data_url(
32273230
emitter.emit_program(program).unwrap();
32283231
drop(emitter);
32293232

3230-
pub struct InlineSourcesContentConfig {}
3231-
impl SourceMapGenConfig for InlineSourcesContentConfig {
3232-
fn file_name_to_source(&self, f: &FileName) -> String {
3233-
f.to_string()
3233+
pub struct InlineSourcesContentConfig<'a> {
3234+
folder_path: Option<&'a Path>,
3235+
}
3236+
impl SourceMapGenConfig for InlineSourcesContentConfig<'_> {
3237+
fn file_name_to_source(&self, file: &FileName) -> String {
3238+
let FileName::Custom(file) = file else {
3239+
// Turbopack uses FileName::Custom for the `[project]/...` paths
3240+
return file.to_string();
3241+
};
3242+
let Some(folder_path) = &self.folder_path else {
3243+
return file.to_string();
3244+
};
3245+
3246+
if let Some(rel_path) = diff_paths(file, folder_path) {
3247+
format!("./{}", rel_path.display())
3248+
} else {
3249+
file.to_string()
3250+
}
32343251
}
32353252

32363253
fn inline_sources_content(&self, _f: &FileName) -> bool {
32373254
true
32383255
}
32393256
}
32403257

3241-
let map = cm.build_source_map_with_config(&mappings, None, InlineSourcesContentConfig {});
3258+
let map = cm.build_source_map_with_config(
3259+
&mappings,
3260+
None,
3261+
InlineSourcesContentConfig {
3262+
folder_path: PathBuf::from(format!("[project]/{file_name}")).parent(),
3263+
},
3264+
);
32423265
let map = {
32433266
if map.get_token_count() > 0 {
32443267
let mut buf = vec![];
@@ -3254,7 +3277,7 @@ fn program_to_data_url(
32543277
if let Some(map) = map {
32553278
output.extend(
32563279
format!(
3257-
"\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,{}",
3280+
"\n//# sourceMappingURL=data:application/json;base64,{}",
32583281
Base64Display::new(&map, &BASE64_STANDARD)
32593282
)
32603283
.chars(),

0 commit comments

Comments
 (0)