@@ -3,13 +3,15 @@ use std::{
3
3
collections:: { hash_map, BTreeMap } ,
4
4
convert:: { TryFrom , TryInto } ,
5
5
mem:: { replace, take} ,
6
+ path:: { Path , PathBuf } ,
6
7
rc:: Rc ,
7
8
sync:: Arc ,
8
9
} ;
9
10
10
11
use base64:: { display:: Base64Display , prelude:: BASE64_STANDARD } ;
11
12
use hex:: encode as hex_encode;
12
13
use indoc:: formatdoc;
14
+ use pathdiff:: diff_paths;
13
15
use rustc_hash:: { FxHashMap , FxHashSet } ;
14
16
use serde:: Deserialize ;
15
17
use sha1:: { Digest , Sha1 } ;
@@ -2168,6 +2170,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
2168
2170
) ] ,
2169
2171
src : Some ( Box :: new (
2170
2172
program_to_data_url (
2173
+ & self . file_name ,
2171
2174
& self . cm ,
2172
2175
vec ! [
2173
2176
ModuleItem :: Stmt ( Stmt :: Expr ( ExprStmt {
@@ -3195,6 +3198,7 @@ fn emit_error(error_kind: ServerActionsErrorKind) {
3195
3198
}
3196
3199
3197
3200
fn program_to_data_url (
3201
+ file_name : & str ,
3198
3202
cm : & Arc < SourceMap > ,
3199
3203
body : Vec < ModuleItem > ,
3200
3204
prepend_comment : Comment ,
@@ -3211,12 +3215,11 @@ fn program_to_data_url(
3211
3215
3212
3216
let mut output = vec ! [ ] ;
3213
3217
let mut mappings = vec ! [ ] ;
3214
- let sourcemap = Arc :: new ( SourceMap :: default ( ) ) ;
3215
3218
let mut emitter = Emitter {
3216
3219
cfg : codegen:: Config :: default ( ) . with_minify ( true ) ,
3217
- cm : sourcemap . clone ( ) ,
3220
+ cm : cm . clone ( ) ,
3218
3221
wr : Box :: new ( JsWriter :: new (
3219
- sourcemap . clone ( ) ,
3222
+ cm . clone ( ) ,
3220
3223
" " ,
3221
3224
& mut output,
3222
3225
Some ( & mut mappings) ,
@@ -3227,18 +3230,40 @@ fn program_to_data_url(
3227
3230
emitter. emit_program ( program) . unwrap ( ) ;
3228
3231
drop ( emitter) ;
3229
3232
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
+ }
3234
3253
}
3235
3254
3236
3255
fn inline_sources_content ( & self , _f : & FileName ) -> bool {
3237
3256
true
3238
3257
}
3239
3258
}
3240
3259
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
+ ) ;
3242
3267
let map = {
3243
3268
if map. get_token_count ( ) > 0 {
3244
3269
let mut buf = vec ! [ ] ;
@@ -3254,7 +3279,7 @@ fn program_to_data_url(
3254
3279
if let Some ( map) = map {
3255
3280
output. extend (
3256
3281
format ! (
3257
- "\n //# sourceMappingURL=data:application/json;charset=utf-8; base64,{}" ,
3282
+ "\n //# sourceMappingURL=data:application/json;base64,{}" ,
3258
3283
Base64Display :: new( & map, & BASE64_STANDARD )
3259
3284
)
3260
3285
. chars ( ) ,
0 commit comments