@@ -18,6 +18,7 @@ use turbo_tasks_fs::{FileContent, FileJsonContent, glob::Glob};
18
18
use turbopack_core:: {
19
19
asset:: { Asset , AssetContent } ,
20
20
chunk:: { ChunkItem , ChunkType , ChunkableModule , ChunkingContext } ,
21
+ code_builder:: CodeBuilder ,
21
22
ident:: AssetIdent ,
22
23
module:: Module ,
23
24
module_graph:: ModuleGraph ,
@@ -130,16 +131,41 @@ impl EcmascriptChunkItem for JsonChunkItem {
130
131
match & * data {
131
132
FileJsonContent :: Content ( data) => {
132
133
let data_str = data. to_string ( ) ;
133
- let inner_code = if data_str. len ( ) > 10_000 {
134
+
135
+ let mut code = CodeBuilder :: default ( ) ;
136
+
137
+ let source_code = if data_str. len ( ) > 10_000 {
134
138
// Only use JSON.parse if the content is larger than 10kb
135
139
// https://v8.dev/blog/cost-of-javascript-2019#json
136
140
let js_str_content = serde_json:: to_string ( & data_str) ?;
137
141
format ! ( "{TURBOPACK_EXPORT_VALUE}(JSON.parse({js_str_content}));" )
138
142
} else {
139
143
format ! ( "{TURBOPACK_EXPORT_VALUE}({data_str});" )
140
144
} ;
145
+
146
+ let source_code = source_code. into ( ) ;
147
+ let source_map = serde_json:: json!( {
148
+ "version" : 3 ,
149
+ // TODO: Encode using `urlencoding`, so that these
150
+ // are valid URLs. However, `project_trace_source_operation` (and
151
+ // `uri_from_file`) need to handle percent encoding correctly first.
152
+ //
153
+ // See turbopack/crates/turbopack-core/src/source_map/utils.rs as well
154
+ "sources" : [ format!( "turbopack:///{}" , self . module. ident( ) . path( ) . to_string( ) . await ?) ] ,
155
+ "sourcesContent" : [ & data_str] ,
156
+ "names" : [ ] ,
157
+ // Maps 0:0 in the output code to 0:0 in the `source_code`. Sufficient for
158
+ // bundle analyzers to attribute the bytes in the output chunks
159
+ "mappings" : "AAAA" ,
160
+ } )
161
+ . to_string ( )
162
+ . into ( ) ;
163
+ code. push_source ( & source_code, Some ( source_map) ) ;
164
+
165
+ let code = code. build ( ) ;
141
166
Ok ( EcmascriptChunkItemContent {
142
- inner_code : inner_code. into ( ) ,
167
+ source_map : Some ( code. generate_source_map_ref ( None ) ) ,
168
+ inner_code : code. into_source_code ( ) ,
143
169
..Default :: default ( )
144
170
}
145
171
. into ( ) )
0 commit comments