Skip to content

Commit eaff417

Browse files
committed
Fix sourcemaps
1 parent 3107415 commit eaff417

File tree

2 files changed

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

2 files changed

+41
-16
lines changed

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

+34-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,40 @@ 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+
// This module will be placed at `some/path/to/data:28a9d2` where the original input file lives
3237+
// at `some/path/to/actions.js`. So we need to generate a relative path, usually `./actions.js`
3238+
impl SourceMapGenConfig for InlineSourcesContentConfig<'_> {
3239+
fn file_name_to_source(&self, file: &FileName) -> String {
3240+
let FileName::Custom(file) = file else {
3241+
// Turbopack uses FileName::Custom for the `[project]/...` paths
3242+
return file.to_string();
3243+
};
3244+
let Some(folder_path) = &self.folder_path else {
3245+
return file.to_string();
3246+
};
3247+
3248+
if let Some(rel_path) = diff_paths(file, folder_path) {
3249+
format!("./{}", rel_path.display())
3250+
} else {
3251+
file.to_string()
3252+
}
32343253
}
32353254

32363255
fn inline_sources_content(&self, _f: &FileName) -> bool {
32373256
true
32383257
}
32393258
}
32403259

3241-
let map = cm.build_source_map_with_config(&mappings, None, InlineSourcesContentConfig {});
3260+
let map = cm.build_source_map_with_config(
3261+
&mappings,
3262+
None,
3263+
InlineSourcesContentConfig {
3264+
folder_path: PathBuf::from(format!("[project]/{file_name}")).parent(),
3265+
},
3266+
);
32423267
let map = {
32433268
if map.get_token_count() > 0 {
32443269
let mut buf = vec![];
@@ -3254,7 +3279,7 @@ fn program_to_data_url(
32543279
if let Some(map) = map {
32553280
output.extend(
32563281
format!(
3257-
"\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,{}",
3282+
"\n//# sourceMappingURL=data:application/json;base64,{}",
32583283
Base64Display::new(&map, &BASE64_STANDARD)
32593284
)
32603285
.chars(),

0 commit comments

Comments
 (0)