@@ -18,6 +18,7 @@ use turbo_tasks_fs::{FileContent, FileJsonContent, glob::Glob};
1818use turbopack_core:: {
1919 asset:: { Asset , AssetContent } ,
2020 chunk:: { ChunkItem , ChunkType , ChunkableModule , ChunkingContext } ,
21+ code_builder:: CodeBuilder ,
2122 ident:: AssetIdent ,
2223 module:: Module ,
2324 module_graph:: ModuleGraph ,
@@ -130,16 +131,41 @@ impl EcmascriptChunkItem for JsonChunkItem {
130131 match & * data {
131132 FileJsonContent :: Content ( data) => {
132133 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 {
134138 // Only use JSON.parse if the content is larger than 10kb
135139 // https://v8.dev/blog/cost-of-javascript-2019#json
136140 let js_str_content = serde_json:: to_string ( & data_str) ?;
137141 format ! ( "{TURBOPACK_EXPORT_VALUE}(JSON.parse({js_str_content}));" )
138142 } else {
139143 format ! ( "{TURBOPACK_EXPORT_VALUE}({data_str});" )
140144 } ;
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 ( ) ;
141166 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 ( ) ,
143169 ..Default :: default ( )
144170 }
145171 . into ( ) )
0 commit comments